Running a bash noscript function as an alias
pzserver () {
if "$1" == "-backup" ; then
# run backup function
elif "$1 == "-start" ; then
# run start function
elif "$1" == "-quit" ; then
# run quit function
else
# no options set
fi
}
^(Obligatory: I'm new at bash noscripting so forgive me if I'm overlooking a simpler solution. Also, any tips and hints in general are much appreciated!)
I have the function above defined in my .bashrc and would like to use this function from a terminal. For example, I'd like to run
Of course I've done some searching and found that I'll need to use
Any hints would be massively appreciated! Thanks!
https://redd.it/15opx5k
@r_bash
pzserver () {
if "$1" == "-backup" ; then
# run backup function
elif "$1 == "-start" ; then
# run start function
elif "$1" == "-quit" ; then
# run quit function
else
# no options set
fi
}
^(Obligatory: I'm new at bash noscripting so forgive me if I'm overlooking a simpler solution. Also, any tips and hints in general are much appreciated!)
I have the function above defined in my .bashrc and would like to use this function from a terminal. For example, I'd like to run
pzserver -backup from a terminal to start the backup process. However, I'm seeing the following error when running that command: -bash: pzserver: command not foundOf course I've done some searching and found that I'll need to use
bash -c to be able to do this, however I'm not sure how to get an alias to do this for me.alias pzserver = "bash -c source pzserver.sh; pzserver $1" doesn't work, it returns -bash: alias: pzserver: not foundAny hints would be massively appreciated! Thanks!
https://redd.it/15opx5k
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
grep 'echo -' vs grep 'echo -.'
what am i missing here, the results for the second version all appear in red font? what's the difference the dot is making? i always thought .* refered to filenames?
https://redd.it/15ose0y
@r_bash
what am i missing here, the results for the second version all appear in red font? what's the difference the dot is making? i always thought .* refered to filenames?
https://redd.it/15ose0y
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
How do I output the new lines onto my terminal.
If I do
But if we do
If I forget the -e option in the initial echo step, then is there a way to still have the newlines output onto my terminal?
https://redd.it/15pfkp1
@r_bash
If I do
echo "dog\ncow\ncat" > file1.txt and then cat file1.txt, it will output dog\ncow\ncat. But if we do
echo -e "dog\ncow\ncat" > file1.txtand then cat it, then it does output the newlines correctly. If I forget the -e option in the initial echo step, then is there a way to still have the newlines output onto my terminal?
https://redd.it/15pfkp1
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
a little wallpaper noscript written by a noob
This is my noscript here: https://github.com/jpatzy/whaven
I was looking for a project idea to help me learn and I came across this wallpaper noscript by /u/wick3dr0se that grabs random wallpapers from unsplash, so I decided to make own noscript to get random wallpapers from Wallhaven.cc. I originally started writing this in bash (I thought) but found out that most of it wasn't bash, and so with the help of shellcheck I tried to make it POSIX compliant instead. I am not 100% that it is since shell noscripting is very new to me.
If anybody is willing to take a look, I'd appreciate any feedback, tips, or criticisms. It only works with utilities that I have personally used which are, sway, feh, and gnome (gsettings).
https://redd.it/15ph8o1
@r_bash
This is my noscript here: https://github.com/jpatzy/whaven
I was looking for a project idea to help me learn and I came across this wallpaper noscript by /u/wick3dr0se that grabs random wallpapers from unsplash, so I decided to make own noscript to get random wallpapers from Wallhaven.cc. I originally started writing this in bash (I thought) but found out that most of it wasn't bash, and so with the help of shellcheck I tried to make it POSIX compliant instead. I am not 100% that it is since shell noscripting is very new to me.
If anybody is willing to take a look, I'd appreciate any feedback, tips, or criticisms. It only works with utilities that I have personally used which are, sway, feh, and gnome (gsettings).
https://redd.it/15ph8o1
@r_bash
GitHub
GitHub - jpatzy/whaven: Get random wallpapers from wallhaven.cc
Get random wallpapers from wallhaven.cc. Contribute to jpatzy/whaven development by creating an account on GitHub.
Isolated environment for iteratively developing dev machine set up noscript?
I want to write a noscript that will set up my machine (MacOS). Nothing too crazy - install brew, brew install a bunch of things I use, prompt me for certain logins so it can auth for me, etc.
Hypothetically, if I test a bash function to install something, then make a change, I'll need to uninstall that thing to re-test.
Is there a better way to do this? Some sort of isolated environment that I can tear down with each run?
Thanks
https://redd.it/15pkanw
@r_bash
I want to write a noscript that will set up my machine (MacOS). Nothing too crazy - install brew, brew install a bunch of things I use, prompt me for certain logins so it can auth for me, etc.
Hypothetically, if I test a bash function to install something, then make a change, I'll need to uninstall that thing to re-test.
Is there a better way to do this? Some sort of isolated environment that I can tear down with each run?
Thanks
https://redd.it/15pkanw
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
awk - how to wrap printf for readability?
writing a really long printf, how to separate it into multiple lines? ie this doesnt work:
{printf(" stuff stuff stuff"
printf "stuff stuff");}
https://redd.it/15puett
@r_bash
writing a really long printf, how to separate it into multiple lines? ie this doesnt work:
{printf(" stuff stuff stuff"
printf "stuff stuff");}
https://redd.it/15puett
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
text in e-mail subject 'corrupts' e-mail body
I have a slightly modified version of this noscript which monitors a zfs volume. It e-mails me after it runs with this command
echo -e "$emailMessage \n\n\n <pre>
Content-Type: text/html" my.address@gmail.com
Its output looks like this:
https://i.imgur.com/JWgOAUg.png
One part of the noscript does this:
if ${problems} -eq 0 ; then
capacity=$(/sbin/zpool list -H -o capacity)
for line in ${capacity//%/}
do
if $line -ge $maxCapacity ; then
emailSubject="$emailSubject - Capacity Exceeded"
problems=1
fi
done
fi
If this condition is true, the e-mail looks like this:
https://i.imgur.com/IhESPce.png
through some trial and error, I've found I can change this to:
emailSubject="$emailSubject - Capac"
and it will still format correctly.
However, adding an i (or any other letter) after "Capac" causes the e-mail formatting error.
I seem to be running into some kind of length limit, but I don't understand why. Is it because of the way the echo command is formatted?
For what its worth, the output of
"$emailMessage \n\n\n <pre>
is 2303 characters long.
Another oddity is that if I were to do say:
echo -e "$emailMessage \n\n\n <pre>
i.e. I don't put a new line between $emailSubject and Content-Type
instead of
echo -e "$emailMessage \n\n\n <pre>
Content-Type: text/html" my.address@gmail.com
I also get the same formatting issue
https://redd.it/15q07kx
@r_bash
I have a slightly modified version of this noscript which monitors a zfs volume. It e-mails me after it runs with this command
echo -e "$emailMessage \n\n\n <pre>
/sbin/zpool list \n\n\n /sbin/zpool status</pre>" | mailx -v -s "$emailSubjectContent-Type: text/html" my.address@gmail.com
Its output looks like this:
https://i.imgur.com/JWgOAUg.png
One part of the noscript does this:
if ${problems} -eq 0 ; then
capacity=$(/sbin/zpool list -H -o capacity)
for line in ${capacity//%/}
do
if $line -ge $maxCapacity ; then
emailSubject="$emailSubject - Capacity Exceeded"
problems=1
fi
done
fi
If this condition is true, the e-mail looks like this:
https://i.imgur.com/IhESPce.png
through some trial and error, I've found I can change this to:
emailSubject="$emailSubject - Capac"
and it will still format correctly.
However, adding an i (or any other letter) after "Capac" causes the e-mail formatting error.
I seem to be running into some kind of length limit, but I don't understand why. Is it because of the way the echo command is formatted?
For what its worth, the output of
"$emailMessage \n\n\n <pre>
/sbin/zpool list \n\n\n /sbin/zpool status</pre>"is 2303 characters long.
Another oddity is that if I were to do say:
echo -e "$emailMessage \n\n\n <pre>
/sbin/zpool list \n\n\n /sbin/zpool status</pre>" | mailx -v -s "$emailSubject Content-Type: text/html" my.address@gmail.comi.e. I don't put a new line between $emailSubject and Content-Type
instead of
echo -e "$emailMessage \n\n\n <pre>
/sbin/zpool list \n\n\n /sbin/zpool status</pre>" | mailx -v -s "$emailSubjectContent-Type: text/html" my.address@gmail.com
I also get the same formatting issue
https://redd.it/15q07kx
@r_bash
Why does /*.py correctly interpret my wildcard but not \*.py?
This command works correctly to output all files ending with .py `ls | grep /*.py`
but this doesn't `ls | grep \*.py`. This doesn't output anything
I can't figure out why as isn't the backslash required to escape the *? In all of the internet resources, the backslash escapes the next character but I'm not sure why the forward slash version is working for me.
https://redd.it/15q2okr
@r_bash
This command works correctly to output all files ending with .py `ls | grep /*.py`
but this doesn't `ls | grep \*.py`. This doesn't output anything
I can't figure out why as isn't the backslash required to escape the *? In all of the internet resources, the backslash escapes the next character but I'm not sure why the forward slash version is working for me.
https://redd.it/15q2okr
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Problem with: Compound Commands, Process Groups and SIGINT interaction
Hello!
I am diving into some more nuanced things about Bash I have noticed and trying to fill in a deeper grasp of some stuff. I have run into an issue I cannot find a resolution to and I wonder if I am missing something...need some pointers from some more knowledgeable folk!!!
If I have a loop - say a
Problem: When I hit ctrl - C the bash loop quits immediately, this is standard behaviour however, I cannot quite explain this using what i have been reading....
Upon
The only thing I can find appears to be:
"When Bash receives a
..but it shouldn't be receiving any signals since it is sent directly to the child whether the signal originates from the terminal driver or the
How can I account for this behaviour? Is there somewhere I verify this in the documentation?
Thanks for any help in getting me out of this conundrum!
https://redd.it/15q3f7d
@r_bash
Hello!
I am diving into some more nuanced things about Bash I have noticed and trying to fill in a deeper grasp of some stuff. I have run into an issue I cannot find a resolution to and I wonder if I am missing something...need some pointers from some more knowledgeable folk!!!
If I have a loop - say a
for loop with a simple long sleep in it- running in an interactive bash instance, then by running ps -p BASH_PID -o pgid,tpgid from another terminal I can see that the loop isn't in the foreground process group; however, by doing a bit of pgrep -P BASH_PID and some similar ps stuff I can see that the child process (the sleep ) is running in the foreground group. I am assuming that the for loop takes place in the interactive bash session itself (instead of a child process) so that variable assignments survive outside the loop etc. So far so good...Problem: When I hit ctrl - C the bash loop quits immediately, this is standard behaviour however, I cannot quite explain this using what i have been reading....
Upon
Ctrl -C, the driver sends SIGINT to the foreground process group...which in this case is the sleep and NOT the interactive bash instance that is housing the for loop itself. Have been reading relevant sections of the bash manual to explain this but I think I have missed something (sorry if obvious). This can be replicated by doing something like kill -INT -$(pgrep -P BASH_ID) from another terminal whilst the first runs and we get much the same result with the whole for group terminating.The only thing I can find appears to be:
"When Bash receives a
SIGINT, it breaks out of any executing loops" - GNU manual..but it shouldn't be receiving any signals since it is sent directly to the child whether the signal originates from the terminal driver or the
kill command I provided...unless bash is using some kinda system call to work out what killed the child process? Then I am just guessing. This behaviour is not seen when the for loop is backgrounded; quitting the sleep with the kill -INT PID_OF_CURRENT_SLEEP_ITERATION just sends it to the next iteration....How can I account for this behaviour? Is there somewhere I verify this in the documentation?
Thanks for any help in getting me out of this conundrum!
https://redd.it/15q3f7d
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Please Help! (bad substitution error)
hello, seeing a bad substitution error when executing my noscript? I have messed around with the spacing but to no avail it errors out with?: ${1,,}: bad substitution (line 3)? thank you!
https://preview.redd.it/ud8t7ezqhwhb1.png?width=2880&format=png&auto=webp&s=2c664d5641ecd61977800150ebc1c1c6ba93cb10
https://redd.it/15q2lqv
@r_bash
hello, seeing a bad substitution error when executing my noscript? I have messed around with the spacing but to no avail it errors out with?: ${1,,}: bad substitution (line 3)? thank you!
https://preview.redd.it/ud8t7ezqhwhb1.png?width=2880&format=png&auto=webp&s=2c664d5641ecd61977800150ebc1c1c6ba93cb10
https://redd.it/15q2lqv
@r_bash
Does it suck?
I've been coding in bash for a while now, and generated the code in the comments with ai (Yes, I know). This is a test for the users of the subreddit, so give me your honest opinions on whether it sucks or not, and how YOU how would improve it. 😉
https://redd.it/15qc092
@r_bash
I've been coding in bash for a while now, and generated the code in the comments with ai (Yes, I know). This is a test for the users of the subreddit, so give me your honest opinions on whether it sucks or not, and how YOU how would improve it. 😉
https://redd.it/15qc092
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
venting! why are the bash manuals so difficult to read
i comprehend things by seeing examples/doing, not by abstract concepts. i find it really difficult to grasp the manual information. why doesn't the manual include some examples?
https://redd.it/15qe2oh
@r_bash
i comprehend things by seeing examples/doing, not by abstract concepts. i find it really difficult to grasp the manual information. why doesn't the manual include some examples?
https://redd.it/15qe2oh
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
I need help with a function.
You might have seen my post a few days ago, if not, it's fine, I just need help again.
​
I have the function:
​
​
Now, whenever I type "music x" it plays my playlist at x volume. However, whenever I type "mpv" it also plays my playlist, prohibiting me from using mpv ever again. Not even "\\mpv works" like an alias. How do i fix this?
https://redd.it/15qjhd5
@r_bash
You might have seen my post a few days ago, if not, it's fine, I just need help again.
​
I have the function:
​
music () { /usr/bin/mpv ~/Music/[playlist]/* --volume="$1" --shuffle --no-audio-display; }​
Now, whenever I type "music x" it plays my playlist at x volume. However, whenever I type "mpv" it also plays my playlist, prohibiting me from using mpv ever again. Not even "\\mpv works" like an alias. How do i fix this?
https://redd.it/15qjhd5
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Questions about finding one or more files and renaming them
I am BRAND NEW to Bash and have a few questions.
Here’s what I’m trying to do:
Find one or more files that could be located anywhere on a Macbook. If files found, rename them.
File will be named like this:
[filename (always the same)\] - [a number that will vary but will be of the form #.#.# or #.#.##\].[file extension (always the same)\].
So something like “foo-1.2.3.abc”.
Based on what I know, I’m guessing the “find” command is the way to go because it’s recursive by default, and also because I won’t necessarily know the location of the files.
Question 1: how do I write the command to denote that I want to search the entire Macbook? I’ll be deploying this noscript to our corporate environment via JAMF (our device management solution for Apple devices.
Question 2: once the noscript finds files on the Macbook, how do I take further action on the results?
I’m planning to tackle this task in two phases. First phase: if files found, rename them (I don’t think removing the files is going to cause an issue, as I’ve already done a similar thing on the Windows side of our environment, but I want an easy way to revert if things are unexpectedly impacted). Second phase: delete all the renamed files.
I’m guessing the rename will be handled by the “mv” command. Something like
mv -v “/location/foo-1.2.3.abc” “/location/foo-1.2.3.abc.DONOTUSE”
But I’m not sure how to couple the rename command with the results of the find command, beyond thinking it’s probably going to involve a pipe character.
Could someone provide just a general example? Thanks!
https://redd.it/15r47q3
@r_bash
I am BRAND NEW to Bash and have a few questions.
Here’s what I’m trying to do:
Find one or more files that could be located anywhere on a Macbook. If files found, rename them.
File will be named like this:
[filename (always the same)\] - [a number that will vary but will be of the form #.#.# or #.#.##\].[file extension (always the same)\].
So something like “foo-1.2.3.abc”.
Based on what I know, I’m guessing the “find” command is the way to go because it’s recursive by default, and also because I won’t necessarily know the location of the files.
Question 1: how do I write the command to denote that I want to search the entire Macbook? I’ll be deploying this noscript to our corporate environment via JAMF (our device management solution for Apple devices.
Question 2: once the noscript finds files on the Macbook, how do I take further action on the results?
I’m planning to tackle this task in two phases. First phase: if files found, rename them (I don’t think removing the files is going to cause an issue, as I’ve already done a similar thing on the Windows side of our environment, but I want an easy way to revert if things are unexpectedly impacted). Second phase: delete all the renamed files.
I’m guessing the rename will be handled by the “mv” command. Something like
mv -v “/location/foo-1.2.3.abc” “/location/foo-1.2.3.abc.DONOTUSE”
But I’m not sure how to couple the rename command with the results of the find command, beyond thinking it’s probably going to involve a pipe character.
Could someone provide just a general example? Thanks!
https://redd.it/15r47q3
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Recommended Video: A break down on the top 50 terminal commands
I came across this video on YT. I have been playing it in the background while I work and already I have picked up a few terminal commands I didn't know, but already have potential use cases...
https://www.youtube.com/watch?v=ZtqBQ68cfJc
https://redd.it/15rksrs
@r_bash
I came across this video on YT. I have been playing it in the background while I work and already I have picked up a few terminal commands I didn't know, but already have potential use cases...
https://www.youtube.com/watch?v=ZtqBQ68cfJc
https://redd.it/15rksrs
@r_bash
YouTube
The 50 Most Popular Linux & Terminal Commands - Full Course for Beginners
Learn the 50 most popular Linux commands from Colt Steele. All these commands work on Linux, macOS, WSL, and anywhere you have a UNIX environment. 🐱
✏️ Colt Steele developed this course.
🔗 The Linux Command Handbook by Flavio Copes: https://www.freecod…
✏️ Colt Steele developed this course.
🔗 The Linux Command Handbook by Flavio Copes: https://www.freecod…
Why did teacher use address 192.168.1.0 for -sn and syn scan despite the default being 1.1? won't we be able to detect more through 1.1 because it is a gateway and all systems are connected to it?
https://redd.it/15rn8y9
@r_bash
https://redd.it/15rn8y9
@r_bash
Reddit
From the bash community on Reddit: Why did teacher use address 192.168.1.0 for -sn and syn scan despite the default being 1.1?…
Explore this post and more from the bash community
How to verify if a redis dump was successfully restored?
I have a bash noscript that
- downloads a redis dump from S3
- stops any running docker containers with the name "myrediscontainer"
- starts a new docker container "myrediscontainer" and copies the downloaded dump file into the data directory of this container
- waits for this docker container to be ready
When I manually jump into the container and run redis-cli, I can see all the keys
But how do I verify if the restore was successful or not inside the bash noscript. Would it be safe to say that dbsize > 0 means restore was successful? Is there a more appropriate way?
#!/usr/bin/env bash
# https://www.shellcheck.net/ VERIFIED
set -e
set -E
set -o pipefail
set -u
# set -x
log="/tmp/error.txt"
exec 2>>"$log"
handleerror() {
# ...handle error
exit 1
}
handleexit() {
# ...handle exit
exit 0
}
trap 'handleexit $?' EXIT
trap 'handleerror $?' ERR
bucket='something-something'
containername="myrediscontainer"
dumpfiledirectory="/tmp"
host="localhost"
port="6379"
dumpfilename=$(aws s3 ls $bucket --recursive | sort | tail -n 1 | awk '{print $4}')
aws s3 cp "s3://${bucket}/${dumpfilename}" "/tmp/${dumpfilename}"
docker ps -aq --filter "name=${containername}" | grep -q . && docker stop "${containername}" && docker rm -fv "${containername}"
docker run --detach --name "${containername}" --publish "${port}:${port}" --volume "/tmp/${dumpfilename}:/data/dump.rdb" redis
# Wait for the Docker container to be ready
until docker exec -it "${containername}" redis-cli -h "${host}" -p "${port}" ping; do
sleep 1
done
# HOW TO VERIFY IF REDIS WAS RESTORED SUCCESSFULLY HERE???
https://redd.it/15rmhbi
@r_bash
I have a bash noscript that
- downloads a redis dump from S3
- stops any running docker containers with the name "myrediscontainer"
- starts a new docker container "myrediscontainer" and copies the downloaded dump file into the data directory of this container
- waits for this docker container to be ready
When I manually jump into the container and run redis-cli, I can see all the keys
But how do I verify if the restore was successful or not inside the bash noscript. Would it be safe to say that dbsize > 0 means restore was successful? Is there a more appropriate way?
#!/usr/bin/env bash
# https://www.shellcheck.net/ VERIFIED
set -e
set -E
set -o pipefail
set -u
# set -x
log="/tmp/error.txt"
exec 2>>"$log"
handleerror() {
# ...handle error
exit 1
}
handleexit() {
# ...handle exit
exit 0
}
trap 'handleexit $?' EXIT
trap 'handleerror $?' ERR
bucket='something-something'
containername="myrediscontainer"
dumpfiledirectory="/tmp"
host="localhost"
port="6379"
dumpfilename=$(aws s3 ls $bucket --recursive | sort | tail -n 1 | awk '{print $4}')
aws s3 cp "s3://${bucket}/${dumpfilename}" "/tmp/${dumpfilename}"
docker ps -aq --filter "name=${containername}" | grep -q . && docker stop "${containername}" && docker rm -fv "${containername}"
docker run --detach --name "${containername}" --publish "${port}:${port}" --volume "/tmp/${dumpfilename}:/data/dump.rdb" redis
# Wait for the Docker container to be ready
until docker exec -it "${containername}" redis-cli -h "${host}" -p "${port}" ping; do
sleep 1
done
# HOW TO VERIFY IF REDIS WAS RESTORED SUCCESSFULLY HERE???
https://redd.it/15rmhbi
@r_bash
www.shellcheck.net
ShellCheck – shell noscript analysis tool
ShellCheck finds bugs in your shell noscripts
how to do rg-fzf-vi function/pipeline?
Hiiiii!!! I need to do something like
I don't know bash (string parsing/expansion/etc, and other aspects of the language) too well. I struggle back and forth between
struggle #1: in lieu of a file object how to parse the filename from a string like
2022-01-02.mkd:looking for a loved rabbi... Hillel the Elder, alive around...
Uh...
struggle #2: what's the best way?
ow. this hurts (sorry for whining, i feel very whiny). Any of you kindhearted folks able to help please? thankyou <3
https://redd.it/15rxc6l
@r_bash
Hiiiii!!! I need to do something like
rg rabbi | fzf | rg ".*.mkd" | xargs vi real easily but this is not at all easy for me.I don't know bash (string parsing/expansion/etc, and other aspects of the language) too well. I struggle back and forth between
grep/ack/rg without knowing either well, moving towards rg. And I don't know fzf well, but it's flags look --magical (and <oof> I fall down).struggle #1: in lieu of a file object how to parse the filename from a string like
2022-01-02.mkd:looking for a loved rabbi... Hillel the Elder, alive around...
Uh...
cut? tr? grep? bash string expansion or globbing or something?struggle #2: what's the best way?
fzf looks like it's designed to be used in pipelines perhaps more efficiently than what I've shown here... how do you do that?ow. this hurts (sorry for whining, i feel very whiny). Any of you kindhearted folks able to help please? thankyou <3
https://redd.it/15rxc6l
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community