Where can I read about CLI-colors for understand and learn about it?
Hi, my CLI has 16 colors using neofetch command,
screenshot 1 https://imgbox.com/PEfXpQZ4
where can I read about it?
If I do vim :xtermcolor(a plugin) I have a palette with more colors...
screenshot 2 https://imgbox.com/TugiCQy6
what are the colors I have?
THank you and regards!!!
https://redd.it/1jql3ov
@r_bash
Hi, my CLI has 16 colors using neofetch command,
screenshot 1 https://imgbox.com/PEfXpQZ4
where can I read about it?
If I do vim :xtermcolor(a plugin) I have a palette with more colors...
screenshot 2 https://imgbox.com/TugiCQy6
what are the colors I have?
THank you and regards!!!
https://redd.it/1jql3ov
@r_bash
Imgbox
imgbox - fast, simple image host
Use imgbox to upload, host and share all your images. It's simple, free and blazing fast!
Command substitution, piping
If the following is written in with pipes instead of command substitutions, how would they compare, particularly at the lower level (e.g. do they involve the same # of forks and execs)? And differences in performance in general or other implications.
It's a very simple example, normally I would just use external commands and pipe if it's a one-off to be run on the command line, whereas for noscripts I would like to be more a little more conscious about how to write better bash (beyond simple general optimizations like avoiding excessive unnecessary external commands).
filename="$(realpath "$1")"
dir="${filename%/}"
size="$(du -b "$filename")"
size=$(numfmt --to=iec --format='%0.5f' "${size%% }")
...
https://redd.it/1jqyeg8
@r_bash
If the following is written in with pipes instead of command substitutions, how would they compare, particularly at the lower level (e.g. do they involve the same # of forks and execs)? And differences in performance in general or other implications.
It's a very simple example, normally I would just use external commands and pipe if it's a one-off to be run on the command line, whereas for noscripts I would like to be more a little more conscious about how to write better bash (beyond simple general optimizations like avoiding excessive unnecessary external commands).
filename="$(realpath "$1")"
dir="${filename%/}"
size="$(du -b "$filename")"
size=$(numfmt --to=iec --format='%0.5f' "${size%% }")
...
https://redd.it/1jqyeg8
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Digital footprint and website testing tool recommendations
I'm cybersecurity student and getting into bash noscripting. I want to make my own universal tool to do Digital footprint checks, website vulnerabilitie check network scans and more. I have the website vulnerabilitie check partly done using, curl, nmap, testssl, webanalyse and ffuf. And I am working on retire js and npmjs to find old Java noscripts. What more could I add to this?
Secondly I want to make a Digital footprint check. What tools / FOSS that can be used in bash noscript to do such a scan? are there any api's I need to get? I know that people sometimes use GB's worth of leaked credentials files is there any legal(open to dm's) way to obtain this.
Any more recommendation or other tools someone uses or likes to be made. when most of my tools work I'm thinking to open source everything on a Github
https://redd.it/1jr815b
@r_bash
I'm cybersecurity student and getting into bash noscripting. I want to make my own universal tool to do Digital footprint checks, website vulnerabilitie check network scans and more. I have the website vulnerabilitie check partly done using, curl, nmap, testssl, webanalyse and ffuf. And I am working on retire js and npmjs to find old Java noscripts. What more could I add to this?
Secondly I want to make a Digital footprint check. What tools / FOSS that can be used in bash noscript to do such a scan? are there any api's I need to get? I know that people sometimes use GB's worth of leaked credentials files is there any legal(open to dm's) way to obtain this.
Any more recommendation or other tools someone uses or likes to be made. when most of my tools work I'm thinking to open source everything on a Github
https://redd.it/1jr815b
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
noob in bash, need learn
Hey guys, I’m a student and getting into sysadmin stuff. I heard knowing Bash noscripting is kinda essential, and I really wanna learn it but I’m a total Linux noob and have no clue where to start. Any tips?
And sry for my english, im trying my best haha
https://redd.it/1jrmtqu
@r_bash
Hey guys, I’m a student and getting into sysadmin stuff. I heard knowing Bash noscripting is kinda essential, and I really wanna learn it but I’m a total Linux noob and have no clue where to start. Any tips?
And sry for my english, im trying my best haha
https://redd.it/1jrmtqu
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Need Help for bash noscript
I'm trying to prepare a noscript in bash that books a seat in a library in my city via Affluences but i can't find any API on the web page, my idea was to use the cURL library and send a request to the server of the app, is there any advice or sub you could suggest?
https://redd.it/1jsc5s4
@r_bash
I'm trying to prepare a noscript in bash that books a seat in a library in my city via Affluences but i can't find any API on the web page, my idea was to use the cURL library and send a request to the server of the app, is there any advice or sub you could suggest?
https://redd.it/1jsc5s4
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
ShellCheck problem with sourcing a noscript
I'm using ShellCheck for the first time and I'm getting an error with a line in the noscript being checked which is sourcing another noscript.
My understanding of the ShellCheck documentation is that I should be able to embed a shellcheck directive telling it what to use for a source path.
It's not working.
The relevant lines in my noscript are:
SCRIPT_DIR=$(dirname "$0")
\# shellcheck source-path=SCRIPTDIR
source "$SCRIPT_DIR/bash_env.sh"
I get the following error:
In _setenv.sh line 45:
source "$SCRIPT_DIR/bash_env.sh"
\^-----------------------\^ SC1090: Can't follow non-constant source. Use a directive to specify location.
What am I doing wrong?
Thanks in advance for any help you can give me.
https://redd.it/1jtekj9
@r_bash
I'm using ShellCheck for the first time and I'm getting an error with a line in the noscript being checked which is sourcing another noscript.
My understanding of the ShellCheck documentation is that I should be able to embed a shellcheck directive telling it what to use for a source path.
It's not working.
The relevant lines in my noscript are:
SCRIPT_DIR=$(dirname "$0")
\# shellcheck source-path=SCRIPTDIR
source "$SCRIPT_DIR/bash_env.sh"
I get the following error:
In _setenv.sh line 45:
source "$SCRIPT_DIR/bash_env.sh"
\^-----------------------\^ SC1090: Can't follow non-constant source. Use a directive to specify location.
What am I doing wrong?
Thanks in advance for any help you can give me.
https://redd.it/1jtekj9
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Can anyone suggest me good Bash book filled with small examples only?
Hi everyone,
Can anyone suggest me good Bash book filled with lots of small examples with explanation? . I'm already going on with Advanced Bash Scripting By Mandel sir, and would like to get a book/online resource that has plenty of Bash Examples with explanation to compliment it with Mandel sir's book.
Thanks and Regards
https://redd.it/1jtsqhr
@r_bash
Hi everyone,
Can anyone suggest me good Bash book filled with lots of small examples with explanation? . I'm already going on with Advanced Bash Scripting By Mandel sir, and would like to get a book/online resource that has plenty of Bash Examples with explanation to compliment it with Mandel sir's book.
Thanks and Regards
https://redd.it/1jtsqhr
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
how to combine find and identify? pipe or &&
Hi, I was trying to use these 2 commands together but I fail.
I used find . -type f -name "3434.jpg fine
I used identify ./ fine
how do you combine then?
¿ find -name ###.jpg | identify ??
Thank you and regards!
https://redd.it/1jutpsq
@r_bash
Hi, I was trying to use these 2 commands together but I fail.
I used find . -type f -name "3434.jpg fine
I used identify ./ fine
how do you combine then?
¿ find -name ###.jpg | identify ??
Thank you and regards!
https://redd.it/1jutpsq
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Shell noscript confounding me ...
I've been working on a shell noscript that automates file movements. I'm using the Mac Automator with a Folder action. Drop a file on a folder and the noscript disperses the file to specific folders based on the file extension {or other string the file name}. All works fine except it does not work with image files [.jpg, .jpeg, .png, .dng, .bmp, .gif, .heic files.\] Pages files, txt files, doc files, and most others work, Below is the opening snippet of the noscript, Can anyone see my blunders? Will this tool NOT work with image fiiles?
Even when I isolate this to one type of image file and repeat the block for aaeh type of file, it still fails,
\#start
for f in "$@"
do
DEST="" # Image files NOTE: "bmp" does not work
if [[ $f == *".png"* || $f == *".jpg"* || $f == *".jpeg"* || $f == *".dng"* || $f == *".gif"* || $f == *".heic"* \]\]
then
DEST="Users/dparcher/Documents/Images"
\# text files:
elif [[ $f == *".txt"* \]\]
then
DEST="/Users/dparcher/Documents/TXTFiles"
\# ... etc, (,csv files also do no process?)
\# and finally:
fi
if [[ $DEST != "" \]\]
then
osanoscript -e "display notification \\"Moved $f to $DEST\\""
\# now move the files accordingly
mv $f $DEST
elif
osanoscript -e "display notification \\"$f was NOT moved.\\""
done
{Bang Head Here}
Thanks for any help offered ...
https://redd.it/1jv605d
@r_bash
I've been working on a shell noscript that automates file movements. I'm using the Mac Automator with a Folder action. Drop a file on a folder and the noscript disperses the file to specific folders based on the file extension {or other string the file name}. All works fine except it does not work with image files [.jpg, .jpeg, .png, .dng, .bmp, .gif, .heic files.\] Pages files, txt files, doc files, and most others work, Below is the opening snippet of the noscript, Can anyone see my blunders? Will this tool NOT work with image fiiles?
Even when I isolate this to one type of image file and repeat the block for aaeh type of file, it still fails,
\#start
for f in "$@"
do
DEST="" # Image files NOTE: "bmp" does not work
if [[ $f == *".png"* || $f == *".jpg"* || $f == *".jpeg"* || $f == *".dng"* || $f == *".gif"* || $f == *".heic"* \]\]
then
DEST="Users/dparcher/Documents/Images"
\# text files:
elif [[ $f == *".txt"* \]\]
then
DEST="/Users/dparcher/Documents/TXTFiles"
\# ... etc, (,csv files also do no process?)
\# and finally:
fi
if [[ $DEST != "" \]\]
then
osanoscript -e "display notification \\"Moved $f to $DEST\\""
\# now move the files accordingly
mv $f $DEST
elif
osanoscript -e "display notification \\"$f was NOT moved.\\""
done
{Bang Head Here}
Thanks for any help offered ...
https://redd.it/1jv605d
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Replace partial lines of a file with another file's lines
I have a noscript that has the output e.g. (keeping the examples very short):
# date: 2025-04-01T16:31:24-0400
firejail-git 0.9.73.r10.gc66588df8
yt-dlp 2025.03.21
syncthing 1.23.4
libvirt 4.2.1
I want to replace all these lines (comments excluded) with lines in a file whose first field (delimited by space) matches the file's lines' first field and and potentially its second field (if it starts with a number), else add the version number as the second field. Such a file looks like this before replacement:
# date: 2024-04-01T16:31:24-0400
# blah blah blaah
abc 1.234
firejail-git 0.9.72.r10.gc66588df8
yt-dlp
# blah blah blaah
syncthing 1.20.4 # blah blah blah
libvirt
After replacement:
# date: 2025-04-01T16:31:24-0400
# blah blah blaah
abc 1.234
firejail-git 0.9.73.r10.gc66588df8
yt-dlp 2025.03.21
# blah blah blaah
syncthing 1.23.4 # blah blah blah
libvirt 4.2.1 # blah blah
The changes that were made:
The date line was changed (in this case, it should match 2 fields `# date:`
The file was updated to the following accordingly:
I want only the relevant lines changed, with the rest of the file preserving their contents (comment lines and line order).
How to go about this? I had a solution with awk that was pretty close but realized arrays are ordered so couldn't preserve order of the lines. I'm not sure if there's a more efficient way that doesn't running involve running e.g. sed commands (not familiar with sed) on each line which seems to require writing the file for each replacement.
I want comments and line ordering to be preserved. I can't guarantee that the second field must be the version number--it could just be an arbitrary string and in that case I don't want to replace it--only insert the version number as the new second field.
https://redd.it/1juum98
@r_bash
I have a noscript that has the output e.g. (keeping the examples very short):
# date: 2025-04-01T16:31:24-0400
firejail-git 0.9.73.r10.gc66588df8
yt-dlp 2025.03.21
syncthing 1.23.4
libvirt 4.2.1
I want to replace all these lines (comments excluded) with lines in a file whose first field (delimited by space) matches the file's lines' first field and and potentially its second field (if it starts with a number), else add the version number as the second field. Such a file looks like this before replacement:
# date: 2024-04-01T16:31:24-0400
# blah blah blaah
abc 1.234
firejail-git 0.9.72.r10.gc66588df8
yt-dlp
# blah blah blaah
syncthing 1.20.4 # blah blah blah
libvirt
After replacement:
# date: 2025-04-01T16:31:24-0400
# blah blah blaah
abc 1.234
firejail-git 0.9.73.r10.gc66588df8
yt-dlp 2025.03.21
# blah blah blaah
syncthing 1.23.4 # blah blah blah
libvirt 4.2.1 # blah blah
The changes that were made:
The date line was changed (in this case, it should match 2 fields `# date:`
The file was updated to the following accordingly:
firejail-git 0.9.72.r10.gc66588df8, yt-dlp 2025.03.21, syncthing 1.23.4 # blah blah blah (note that the comment is preserved, only the version was updated with field 2 changed), libvirt 4.2.1 # blah blah has the version added as second field)I want only the relevant lines changed, with the rest of the file preserving their contents (comment lines and line order).
How to go about this? I had a solution with awk that was pretty close but realized arrays are ordered so couldn't preserve order of the lines. I'm not sure if there's a more efficient way that doesn't running involve running e.g. sed commands (not familiar with sed) on each line which seems to require writing the file for each replacement.
I want comments and line ordering to be preserved. I can't guarantee that the second field must be the version number--it could just be an arbitrary string and in that case I don't want to replace it--only insert the version number as the new second field.
https://redd.it/1juum98
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Function to generate images with Gemini
https://preview.redd.it/ysghltkzxvte1.png?width=1408&format=png&auto=webp&s=fdbeb494d3dcc063f2314e8c93aaa2f0c25be10b
$ declare -f imagen
imagen ()
{
curl -s https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate-002:predict?key=$GEMINIAPIKEY -H 'Content-Type: application/json' -X POST -d '{"instances": {"prompt": "'"$1"'"},"parameters": {"sampleCount": 1, "aspectRatio": "16:9"}}' | jq -r '.predictions0.bytesBase64Encoded' | base64 -d > "$2.png"
}
$ imagen "A gorilla surfing near the beach" gorilla
$ file gorilla.png
gorilla.png: PNG image data, 1408 x 768, 8-bit/color RGB, non-interlaced
https://redd.it/1jvinxe
@r_bash
https://preview.redd.it/ysghltkzxvte1.png?width=1408&format=png&auto=webp&s=fdbeb494d3dcc063f2314e8c93aaa2f0c25be10b
$ declare -f imagen
imagen ()
{
curl -s https://generativelanguage.googleapis.com/v1beta/models/imagen-3.0-generate-002:predict?key=$GEMINIAPIKEY -H 'Content-Type: application/json' -X POST -d '{"instances": {"prompt": "'"$1"'"},"parameters": {"sampleCount": 1, "aspectRatio": "16:9"}}' | jq -r '.predictions0.bytesBase64Encoded' | base64 -d > "$2.png"
}
$ imagen "A gorilla surfing near the beach" gorilla
$ file gorilla.png
gorilla.png: PNG image data, 1408 x 768, 8-bit/color RGB, non-interlaced
https://redd.it/1jvinxe
@r_bash
Reading then writing the same file empties it
I have a noscript and when running it
If I
The noscript reads
https://redd.it/1jvgvg5
@r_bash
I have a noscript and when running it
./noscript >~/.config/chkupdates/chkupdates.conf ($conf in the noscript), I'm not sure why the output isn't overwriting the file with what shows in stdout--the file is instead cleared with a newline. If I
./noscript >/tmp/test, I can see the expected output is saved to this arbitrary file, so something appears to be wrong with streaming the data and overwriting to $conf.The noscript reads
$conf, so I already made efforts to have the noscript reads this file in a loop which then outputs to a $tmpfile which the noscript print the $tmpfile's contents so that the user can pipe it out to $conf safely.https://redd.it/1jvgvg5
@r_bash
Can't seem to find decent commenting style
I want first comment (first line) to describe the entire group of code, second comment (second line) to describe only first line of code starts with `tracked=`. How to best make this more obvious? The second comment is too long to fit on the same line as the code.
# skip parsing to print full line when a line doesn't start with
# trim leading whitespaces. Ref:
# https://web.archive.org/web/20121022051228/http://codesnippets.joyent.com/posts/show/1816
tracked="${tracked#"${tracked%%[![:space:]]*}"}"
if [[ "$tracked" =~ ^[^[:alnum:]] ]]; then
echo "$tracked"
continue
fi
And in general, I'm not sure there's much decent logic at all to have a comment represent more than one block of code (it might imply multiple blocks, but how do you know when it should end)? Having an end marker comment seems excessive considering I never really come across it.
Probably more of a general coding question, looking for a solution that can work across multiple languages.
https://redd.it/1jvdhmr
@r_bash
I want first comment (first line) to describe the entire group of code, second comment (second line) to describe only first line of code starts with `tracked=`. How to best make this more obvious? The second comment is too long to fit on the same line as the code.
# skip parsing to print full line when a line doesn't start with
# trim leading whitespaces. Ref:
# https://web.archive.org/web/20121022051228/http://codesnippets.joyent.com/posts/show/1816
tracked="${tracked#"${tracked%%[![:space:]]*}"}"
if [[ "$tracked" =~ ^[^[:alnum:]] ]]; then
echo "$tracked"
continue
fi
And in general, I'm not sure there's much decent logic at all to have a comment represent more than one block of code (it might imply multiple blocks, but how do you know when it should end)? Having an end marker comment seems excessive considering I never really come across it.
Probably more of a general coding question, looking for a solution that can work across multiple languages.
https://redd.it/1jvdhmr
@r_bash
fuzpad - A minimalistic note management solution. Powered by fzf.
https://terminaltrove.com/fuzpad/
https://redd.it/1jvxzh1
@r_bash
https://terminaltrove.com/fuzpad/
https://redd.it/1jvxzh1
@r_bash
Terminaltrove
fuzpad - A minimalistic note management solution. Powered by fzf.
A minimalistic note management solution. Powered by fzf. - Discover more CLI and TUI tools like fuzpad and much more on Terminal Trove.
Actual Vim Motions in bash?
I only found like two posts on reddit and another on StackOverFlow where the user is expressing frustration of
The problem though is that there's noticeable latency that I can't wrap my head around, so if someone uses this and it doesn't have latency please tell me what terminal emulator you use.
https://redd.it/1jwrevg
@r_bash
I only found like two posts on reddit and another on StackOverFlow where the user is expressing frustration of
set -o vi and seeking an alternative approach to get vim motions in his shell, which is very shocking to me but maybe I just suck at googling right? or people know their shell keybinings unlike me - lazy to learn something new. Anyway, I found this project which pulls it off: https://github.com/akinomyoga/ble.sh The problem though is that there's noticeable latency that I can't wrap my head around, so if someone uses this and it doesn't have latency please tell me what terminal emulator you use.
https://redd.it/1jwrevg
@r_bash
GitHub
GitHub - akinomyoga/ble.sh: Bash Line Editor―a line editor written in pure Bash with syntax highlighting, auto suggestions, vim…
Bash Line Editor―a line editor written in pure Bash with syntax highlighting, auto suggestions, vim modes, etc. for Bash interactive sessions. - akinomyoga/ble.sh
I need to know why this works.
Why does this function preserve the arg escaping correctly? I sorta get it, and I sorta don't. Is there a better way to do this that works in posix sh like this does?
All the explanations written in the PR are by me, they represent my current understanding, as are the explanations underneath the shellcheck disables.
Is my understanding correct?
Used in this function, which generates C code to stdout
$1 and $2 are a space separated string, of all things passed in to the noscript with --add-flags theval concatenated with spaces
Context
https://github.com/NixOS/nixpkgs/pull/397604
I have tried a ton of ways to do this.
I have tried for arg in "$@"; do for example, but was unable to get that to work.
So why does this work? Can it be improved? This is the only approach I have succeeded with so far.
https://redd.it/1jx8dmm
@r_bash
Why does this function preserve the arg escaping correctly? I sorta get it, and I sorta don't. Is there a better way to do this that works in posix sh like this does?
All the explanations written in the PR are by me, they represent my current understanding, as are the explanations underneath the shellcheck disables.
Is my understanding correct?
arg2list() {
local toset=$1
shift 1
# shellcheck disable=SC2145
# we actually want to eval on structured data.
# so mixing strings with arrays is the point
# shellcheck disable=SC2294
# and yes eval on a string negates the benefits of arrays,
# thats why we leave it an array.
eval "$toset=($@)"
}
Used in this function, which generates C code to stdout
$1 and $2 are a space separated string, of all things passed in to the noscript with --add-flags theval concatenated with spaces
addFlags() {
local n flag before after var
# Disable file globbing, since bash will otherwise try to find
# filenames matching the the value to be prefixed/suffixed if
# it contains characters considered wildcards, such as `?` and
# `*`. We want the value as is, except we also want to split
# it on on the separator; hence we can't quote it.
local reenableGlob=0
if [[ ! -o noglob ]]; then
reenableGlob=1
fi
set -o noglob
# shellcheck disable=SC2086
arg2list before $1
# shellcheck disable=SC2086
arg2list after $2
if (( reenableGlob )); then
set +o noglob
fi
var="argv_tmp"
printf '%s\n' "char **$var = calloc(${#before[@]} + argc + ${#after[@]} + 1, sizeof(*$var));"
printf '%s\n' "assert($var != NULL);"
printf '%s\n' "${var}[0] = argv[0];"
for ((n = 0; n < ${#before[@]}; n += 1)); do
flag=$(escapeStringLiteral "${before[n]}")
printf '%s\n' "${var}[$((n + 1))] = \"$flag\";"
done
printf '%s\n' "for (int i = 1; i < argc; ++i) {"
printf '%s\n' " ${var}[${#before[@]} + i] = argv[i];"
printf '%s\n' "}"
for ((n = 0; n < ${#after[@]}; n += 1)); do
flag=$(escapeStringLiteral "${after[n]}")
printf '%s\n' "${var}[${#before[@]} + argc + $n] = \"$flag\";"
done
printf '%s\n' "${var}[${#before[@]} + argc + ${#after[@]}] = NULL;"
printf '%s\n' "argv = $var;"
}
Context
https://github.com/NixOS/nixpkgs/pull/397604
I have tried a ton of ways to do this.
I have tried for arg in "$@"; do for example, but was unable to get that to work.
So why does this work? Can it be improved? This is the only approach I have succeeded with so far.
https://redd.it/1jx8dmm
@r_bash
GitHub
makeBinaryWrapper: --add-flags arguments now properly escaped by BirdeeHub · Pull Request #397604 · NixOS/nixpkgs
arguments passed to --add-flags were ALWAYS split by spaces regardless of how you passed them. This PR also applies to --append-flags as well
This utilizes normal shell argument parsing to create t...
This utilizes normal shell argument parsing to create t...
Replacing echo with printf broke my noscripts
Taking the advice in https://www.reddit.com/r/bash/comments/1519wby/why\_printf\_over\_echo\_noob\_question/ and elsewhere, I proceeded to do
Whereas echo had worked perfectly, many strings now mysteriously got truncated. I reverted back to echo and all is working well, again, but I'm intrigued why this happened. I tried replacing %s with %b but it made no difference.
Does printf %s not handle utf-8 correctly or something?
https://redd.it/1jxi95b
@r_bash
Taking the advice in https://www.reddit.com/r/bash/comments/1519wby/why\_printf\_over\_echo\_noob\_question/ and elsewhere, I proceeded to do
sed -i 's/echo /printf \x27%s\\n\x27 /' bin/*.shWhereas echo had worked perfectly, many strings now mysteriously got truncated. I reverted back to echo and all is working well, again, but I'm intrigued why this happened. I tried replacing %s with %b but it made no difference.
Does printf %s not handle utf-8 correctly or something?
https://redd.it/1jxi95b
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Why does this work?
Used in this function, which generates C code to stdout
Context
https://github.com/NixOS/nixpkgs/pull/397604
https://redd.it/1jx8bta
@r_bash
arg2list() {
local toset=$1
shift 1
# shellcheck disable=SC2145
# we actually want to eval on structured data.
# so mixing strings with arrays is the point
# shellcheck disable=SC2294
# and yes eval on a string negates the benefits of arrays,
# thats why we leave it an array.
eval "$toset=($@)"
}
Used in this function, which generates C code to stdout
addFlags() {
local n flag before after var
# Disable file globbing, since bash will otherwise try to find
# filenames matching the the value to be prefixed/suffixed if
# it contains characters considered wildcards, such as `?` and
# `*`. We want the value as is, except we also want to split
# it on on the separator; hence we can't quote it.
local reenableGlob=0
if [[ ! -o noglob ]]; then
reenableGlob=1
fi
set -o noglob
# shellcheck disable=SC2086
arg2list before $1
# shellcheck disable=SC2086
arg2list after $2
if (( reenableGlob )); then
set +o noglob
fi
var="argv_tmp"
printf '%s\n' "char **$var = calloc(${#before[@]} + argc + ${#after[@]} + 1, sizeof(*$var));"
printf '%s\n' "assert($var != NULL);"
printf '%s\n' "${var}[0] = argv[0];"
for ((n = 0; n < ${#before[@]}; n += 1)); do
flag=$(escapeStringLiteral "${before[n]}")
printf '%s\n' "${var}[$((n + 1))] = \"$flag\";"
done
printf '%s\n' "for (int i = 1; i < argc; ++i) {"
printf '%s\n' " ${var}[${#before[@]} + i] = argv[i];"
printf '%s\n' "}"
for ((n = 0; n < ${#after[@]}; n += 1)); do
flag=$(escapeStringLiteral "${after[n]}")
printf '%s\n' "${var}[${#before[@]} + argc + $n] = \"$flag\";"
done
printf '%s\n' "${var}[${#before[@]} + argc + ${#after[@]}] = NULL;"
printf '%s\n' "argv = $var;"
}
Context
https://github.com/NixOS/nixpkgs/pull/397604
https://redd.it/1jx8bta
@r_bash
GitHub
makeBinaryWrapper: --add-flags arguments now properly escaped by BirdeeHub · Pull Request #397604 · NixOS/nixpkgs
arguments passed to --add-flags were ALWAYS split by spaces regardless of how you passed them. This PR also applies to --append-flags as well
This utilizes normal shell argument parsing to create t...
This utilizes normal shell argument parsing to create t...