Output, Assign, Pipe and Redirect a Heredoc in a Shell Script

  Рет қаралды 1,137

Nick Janetakis

Nick Janetakis

Күн бұрын

Пікірлер: 12
@lindhe
@lindhe 9 ай бұрын
Thank you for showing how to avoid variable expansion! 👍 That often trips me up.
@__gazal__
@__gazal__ 9 ай бұрын
Thanks!
@NickJanetakis
@NickJanetakis 9 ай бұрын
Anytime, thanks a lot for the tip, I really appreciate it!
@ssd6010
@ssd6010 9 ай бұрын
I love your shell scripts contents
@pavelzadorski
@pavelzadorski 5 ай бұрын
Great content, thanks! Have you stumbled upon one more use-case: To have a self-contained script creating a file with JSON values interpolated using shell variables. You explained great how to assign a variable and how to save a file. Thanks again! :) What I found not obvious is how to make use of both, so that JSON indentation won't clash with script's own indentation. I found your video on a quest for the following goal: - A here-doc is assigned to a variable outside of bash function (with the heredoc ingesting other shell variables). - A here-doc with variable expansions gets saved to a file inside of a function (while preserving JSON indentation). - Just echoing multi-line variable has its own peculiarities, so the possible solution might be usage of cat/tee and eval/envsubst.
@NickJanetakis
@NickJanetakis 5 ай бұрын
Hi, if you find yourself fighting indentation you can always pipe it to jq to format the JSON however you see fit. Would that work?
@pavelzadorski
@pavelzadorski 5 ай бұрын
Hey @NickJanetakis, thanks for the response! Yesterday, I encountered a similar situation with KQL. This is a broader question. Using jq for formatting at runtime is more of a workaround. Instead, having a script with multi-line values enriched via shell variables would be best for readability. The simplest approach I found was using a function that employs cat to direct a heredoc into a file, which another function then reads. My initial aim was to store a multi-line variable (e.g., a lengthy KQL query) separately from the main function. Then we could use multi-line values with proper parameter substitutions directly (e.g. az graph), without creation of yet another temporary file :)
@nnutipa
@nnutipa 9 ай бұрын
Thanks for the video. Why didn't you mention
@NickJanetakis
@NickJanetakis 9 ай бұрын
I have an awareness of it but it has never come up in anything I've done. It's only for stripping leading tab characters right? Most set ups and code formatting tools will convert tabs to spaces. Am I missing something?
@nnutipa
@nnutipa 9 ай бұрын
@@NickJanetakis Yes, you must use tabs, but I am not sure about tab conversion to spaces. Afaik, if you don't want this to happen you can turn this functionality off in almost every text editor.
@NickJanetakis
@NickJanetakis 9 ай бұрын
@@nnutipa Right, I mean in almost all cases I've seen while working for and with folks, there has been a general census to replace tabs with spaces at the code editor level and it's also sometimes enforced by code formatting tools for specific languages. The only exception has been for files that require hard tabs like a Makefile.
@bhaveshverma8629
@bhaveshverma8629 9 ай бұрын
Awesome
All Rust string types explained
22:13
Let's Get Rusty
Рет қаралды 199 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН
Using Nested Default Variable Values in a Shell Script
7:14
Nick Janetakis
Рет қаралды 292
Arenas, strings and Scuffed Templates in C
12:28
VoxelRifts
Рет қаралды 105 М.
Inside the V3 Nazi Super Gun
19:52
Blue Paw Print
Рет қаралды 2,8 МЛН
Vim Tips I Wish I Knew Earlier
23:00
Sebastian Daschner
Рет қаралды 89 М.
10 Signs Your Software Project Is Heading For FAILURE
17:59
Continuous Delivery
Рет қаралды 30 М.
Fast Inverse Square Root - A Quake III Algorithm
20:08
Nemean
Рет қаралды 5 МЛН
Premature Optimization
12:39
CodeAesthetic
Рет қаралды 854 М.
Мясо вегана? 🧐 @Whatthefshow
01:01
История одного вокалиста
Рет қаралды 7 МЛН