Here document detail - poorly written docs?
According to the Bash docs:
> This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen.
Since it explicitly called out no trailing blanks, I read that as allowing leading blanks but that does not seem to work. I'm just curious if other people agree with my reading and think the docs would be clearer it they read with no leading or trailing blanks or would this be clear if only I understood X (in which case, what is that X).
https://redd.it/12bog6k
@r_bash
According to the Bash docs:
> This type of redirection instructs the shell to read input from the current source until a line containing only word (with no trailing blanks) is seen.
Since it explicitly called out no trailing blanks, I read that as allowing leading blanks but that does not seem to work. I'm just curious if other people agree with my reading and think the docs would be clearer it they read with no leading or trailing blanks or would this be clear if only I understood X (in which case, what is that X).
https://redd.it/12bog6k
@r_bash
www.gnu.org
Redirections (Bash Reference Manual)
Next: Executing Commands, Previous: Shell Expansions, Up: Basic Shell Features [Contents][Index]
Get all arguments between named arguments
I want to run a function such as
so it recognizes 1 2 3 as arguments to be appended to -e case in the switch-like statement i have at present:
The -f is just a dummy to serve as example for elements to stop being appended to -e case.
Any ideas on how to do this?
https://redd.it/12brmia
@r_bash
I want to run a function such as
foo -e 1 2 3 -f 4
so it recognizes 1 2 3 as arguments to be appended to -e case in the switch-like statement i have at present:
foo() {
exclude=()
while test $# -gt 0; do
case $1 in
-e) exclude+=$2; shift 2;;
*) break;;
esac
done
testlist=(1 2 3 4)
for e in ${exclude[@]}; do
testlist=(${testlist[@]/$e/})
done
echo ${testlist[@]}
}
The -f is just a dummy to serve as example for elements to stop being appended to -e case.
Any ideas on how to do this?
https://redd.it/12brmia
@r_bash
Reddit
r/bash on Reddit: Get all arguments between named arguments
Posted by u/newguywastaken - No votes and no comments
Is there a recommended alternative to getopts?
Hi everyone, I’m a beginner in Bash noscripting and I need some advice on how to parse options.
I know there are two common ways to do it:
Writing a custom "while loop" in Bash, but this can get complicated if you want to handle short-form flags that can be grouped together (so that it detects that “-a -b -c” is the same as “-abc”)
Using getopts, but this doesn’t support long-form options (like “–help”)
I’m looking for a solution that can handle both short-form grouping and long-form, like most noscripting languages and the Fish shell have (argparse). Is there a recommended alternative to getopts that can do this?
Thanks!
https://redd.it/12bqmyi
@r_bash
Hi everyone, I’m a beginner in Bash noscripting and I need some advice on how to parse options.
I know there are two common ways to do it:
Writing a custom "while loop" in Bash, but this can get complicated if you want to handle short-form flags that can be grouped together (so that it detects that “-a -b -c” is the same as “-abc”)
Using getopts, but this doesn’t support long-form options (like “–help”)
I’m looking for a solution that can handle both short-form grouping and long-form, like most noscripting languages and the Fish shell have (argparse). Is there a recommended alternative to getopts that can do this?
Thanks!
https://redd.it/12bqmyi
@r_bash
Are there any quality of life improvements that you add to bash?
I have installed bash-autocompletion and that's pretty awesome! I've also tried to use
https://redd.it/12bptfj
@r_bash
I have installed bash-autocompletion and that's pretty awesome! I've also tried to use
autojump and it's finnicky and I couldn't get it to work for bash, despite it claiming first class support for bash?https://redd.it/12bptfj
@r_bash
Reddit
r/bash on Reddit: Are there any quality of life improvements that you add to bash?
Posted by u/hungry_squared_hippo - 3 votes and 4 comments
Question regarding Logrotate
Hi all, I am trying to understand logrotate on my linux servers. I have read all the docs on it and how we use individual config files to control other app logs. My question is does the logrotate.conf set a precedence over the files in logrotate.d directory? I would also like to know can you do away with the files in the logrotate.d directory and just put all the info in the logrotate.conf file? Thanks any other helpful info would be appreciated as well.
https://redd.it/12cp7q1
@r_bash
Hi all, I am trying to understand logrotate on my linux servers. I have read all the docs on it and how we use individual config files to control other app logs. My question is does the logrotate.conf set a precedence over the files in logrotate.d directory? I would also like to know can you do away with the files in the logrotate.d directory and just put all the info in the logrotate.conf file? Thanks any other helpful info would be appreciated as well.
https://redd.it/12cp7q1
@r_bash
Reddit
r/bash on Reddit: Question regarding Logrotate
Posted by u/Individual-Self-9533 - No votes and no comments
zsh-style menu completion in bash?
Is there any way to get zsh-style tab menu completion in bash? Specifically, where the list of completions appears, you can tab through them, and once you've selected it the menu disappears? https://asciinema.org/a/2q4M7y9cu476xrlK6TLfCZlJg
https://redd.it/12cqxxl
@r_bash
Is there any way to get zsh-style tab menu completion in bash? Specifically, where the list of completions appears, you can tab through them, and once you've selected it the menu disappears? https://asciinema.org/a/2q4M7y9cu476xrlK6TLfCZlJg
https://redd.it/12cqxxl
@r_bash
asciinema.org
unnoscriptd
Recorded by dmd3eorg
Automating unzipping files in multiple sub folders
Hello Everyone,
I'm planning on how to write this bash noscript.
I currently have a parent directory called 'logs' containing sub-folders that populate once daily with the current dates. In these sub-folders are .csv.gzip files that I need to unzip. I want to automate the process by creating a cron job to run this bash noscript. I'm new to bash noscripting, so please bare with me.
Can anyone point me to resources that best explain how to accomplish this?
I think iterating using a for loop into each sub-folder and then having an if statement to check if it contains the .csv.gzip file extension and if the value is true to unzip the files. Then, move to the next sub-folder.
All the sub-folders have this format YYYY-MM-DD.
Any help would be greatly appreciated.
https://redd.it/12cxwba
@r_bash
Hello Everyone,
I'm planning on how to write this bash noscript.
I currently have a parent directory called 'logs' containing sub-folders that populate once daily with the current dates. In these sub-folders are .csv.gzip files that I need to unzip. I want to automate the process by creating a cron job to run this bash noscript. I'm new to bash noscripting, so please bare with me.
Can anyone point me to resources that best explain how to accomplish this?
I think iterating using a for loop into each sub-folder and then having an if statement to check if it contains the .csv.gzip file extension and if the value is true to unzip the files. Then, move to the next sub-folder.
All the sub-folders have this format YYYY-MM-DD.
Any help would be greatly appreciated.
https://redd.it/12cxwba
@r_bash
Reddit
r/bash on Reddit: Automating unzipping files in multiple sub folders
Posted by u/No_B0dyCares - No votes and no comments
YAD noscript help.....A progress bar without error window
I'm having issues implementing an error window that stops the progress and shows what failed. currently, the window will continue regardless of the error.
code is :
\#!/bin/bash -f
\#log not working
log="/home/pi/yad/logs/Rut_Check_error.log > $(date +'%FT%T%Z')"
(
\# =================================================================
echo "5"
echo "# Varifying Data Is correct." ; sleep 3
\## Calling exp. noscript
./Print_Qr_exp.sh && tail
\# =================================================================
echo "25"
echo "# Collecting Rut Infomation." ; sleep 2
grep "Mac," Rut-Info.log | tr ',' '\\n' | tail -n1 > mac.log
\# =================================================================
echo "55"
echo "# Getting the printer ready." ; sleep 3
\# Script runs Python env and prints
./activ.sh
\# =================================================================
echo "85"
echo "# Storing data logs" ; sleep 4
\## mv rut-info.txt (rename it to date) ; mv inst.log (rename with date) into new folder
\# =================================================================
echo "100"
echo "# Successful" ; sleep 1
) |
yad --progress --center --borders=70 \\
\--width=850 --height=650 \\
\--noscript="#### Progress Status ####" \\
\--text="Processing Data In Session." \\
\--text-align=center \\
\--percentage=0 \\
\--auto-kill \\
\#--auto-close \\
(( $? != 0)) && yad --error --text="Error in yad command." --width="400" --height="400" --noscript="Error Data"\\
exit 1
https://redd.it/12czvv6
@r_bash
I'm having issues implementing an error window that stops the progress and shows what failed. currently, the window will continue regardless of the error.
code is :
\#!/bin/bash -f
\#log not working
log="/home/pi/yad/logs/Rut_Check_error.log > $(date +'%FT%T%Z')"
(
\# =================================================================
echo "5"
echo "# Varifying Data Is correct." ; sleep 3
\## Calling exp. noscript
./Print_Qr_exp.sh && tail
\# =================================================================
echo "25"
echo "# Collecting Rut Infomation." ; sleep 2
grep "Mac," Rut-Info.log | tr ',' '\\n' | tail -n1 > mac.log
\# =================================================================
echo "55"
echo "# Getting the printer ready." ; sleep 3
\# Script runs Python env and prints
./activ.sh
\# =================================================================
echo "85"
echo "# Storing data logs" ; sleep 4
\## mv rut-info.txt (rename it to date) ; mv inst.log (rename with date) into new folder
\# =================================================================
echo "100"
echo "# Successful" ; sleep 1
) |
yad --progress --center --borders=70 \\
\--width=850 --height=650 \\
\--noscript="#### Progress Status ####" \\
\--text="Processing Data In Session." \\
\--text-align=center \\
\--percentage=0 \\
\--auto-kill \\
\#--auto-close \\
(( $? != 0)) && yad --error --text="Error in yad command." --width="400" --height="400" --noscript="Error Data"\\
exit 1
https://redd.it/12czvv6
@r_bash
Reddit
r/bash on Reddit: YAD noscript help.....A progress bar without error window
Posted by u/Queasy_Piece5446 - No votes and 2 comments
smarter way to test bash noscripts and list all external commands
I was wondering if there were smart ways to find all uses of external commands in a bash noscript ?
i.e. is grep using built in bash or external grep etc (( can't think of a perfect command where its in both places at this time of night :P )).
The only solution in my head is strace or something but was wondering if there were a better way?
https://redd.it/12d2yhm
@r_bash
I was wondering if there were smart ways to find all uses of external commands in a bash noscript ?
i.e. is grep using built in bash or external grep etc (( can't think of a perfect command where its in both places at this time of night :P )).
The only solution in my head is strace or something but was wondering if there were a better way?
https://redd.it/12d2yhm
@r_bash
Reddit
r/bash on Reddit: smarter way to test bash noscripts and list all external commands
Posted by u/daz_007 - No votes and no comments
quote help
I am trying to build information for my bash prompt. The following command works in the terminal.
git worktree list 2>/dev/null | grep -v "^${HOME} " | sed -e 's/^.\[//' -e 's/\]$//'
When I add this to .bashrc, I get an error stating the following:
grep: ": No such file or directory
This is the line in the .bashrc file
PS1="${PS1}$(git worktree list 2>/dev/null | grep -v \"^${HOME} \" | sed -e 's/^.[//' -e 's/\]$//')"
https://redd.it/12dgc05
@r_bash
I am trying to build information for my bash prompt. The following command works in the terminal.
git worktree list 2>/dev/null | grep -v "^${HOME} " | sed -e 's/^.\[//' -e 's/\]$//'
When I add this to .bashrc, I get an error stating the following:
grep: ": No such file or directory
This is the line in the .bashrc file
PS1="${PS1}$(git worktree list 2>/dev/null | grep -v \"^${HOME} \" | sed -e 's/^.[//' -e 's/\]$//')"
https://redd.it/12dgc05
@r_bash
Reddit
r/bash on Reddit: quote help
Posted by u/linux478 - No votes and 3 comments
Optimizing bash noscripts?
How? I've read somewhere over the Internet that the *sh family is inherently slow. How could we reduce the impact of this nature so that bash noscripts can perform faster? Are there recommended habits to follow? Hints? Is there any primordial advice?
https://redd.it/12drpy9
@r_bash
How? I've read somewhere over the Internet that the *sh family is inherently slow. How could we reduce the impact of this nature so that bash noscripts can perform faster? Are there recommended habits to follow? Hints? Is there any primordial advice?
https://redd.it/12drpy9
@r_bash
Reddit
r/bash on Reddit: Optimizing bash noscripts?
Posted by u/Mark_1802 - No votes and 8 comments
Can someone explain how [:print:] works?
So one of the noscript is:
echo /sys/devices/pci0000:00/0000:00:14.0/i2c-4/4-002e/hwmon/hwmon[[:print:\]\]*/pwm2
I understand the result, but how does it work? What is it call in bash?
https://redd.it/12du8ed
@r_bash
So one of the noscript is:
echo /sys/devices/pci0000:00/0000:00:14.0/i2c-4/4-002e/hwmon/hwmon[[:print:\]\]*/pwm2
I understand the result, but how does it work? What is it call in bash?
https://redd.it/12du8ed
@r_bash
Reddit
r/bash on Reddit: Can someone explain how [[:print:]] works?
Posted by u/paulsiu - No votes and no comments
Checkbox menu without whiptail or dialog
I'm wanting to write a menu with checkboxes in bash. Installing whiptail or dialog are not options.
Is it even possible to a menu with checkboxes in bash without whiptail or dialog?
https://redd.it/12e8hbl
@r_bash
I'm wanting to write a menu with checkboxes in bash. Installing whiptail or dialog are not options.
Is it even possible to a menu with checkboxes in bash without whiptail or dialog?
https://redd.it/12e8hbl
@r_bash
Reddit
r/bash on Reddit: Checkbox menu without whiptail or dialog
Posted by u/DaveR007 - No votes and no comments
What am I doing wrong?
the noscript:
find /path/to/folder -type f \\( -iname "*.flac" -o -iname "*.wav" -o -iname "*.alac" -o -iname "*.aiff" -o -iname "*.ape" -o -iname "*.dsd" \\) -o -iname "*.mp3" -exec bash -c '
for audio_file do
audio_basename="$(basename "${audio_file}")"
target_file="/path/to/tracks/${audio_basename%.*}.flac"
[[ -f "${target_file}" \]\] && continue
lossless_file=""
for ext in flac alac wav aiff ape dsd; do
lf="${audio_file%.*}.${ext}"
[[ -f "${lf}" \]\] && lossless_file="${lf}" && break
done
[[ -z "${lossless_file}" \]\] && lossless_file="${audio_file}"
mp3_file="${audio_file%.*}.mp3"
if [[ -f "${mp3_file}" && "${lossless_file}" != "${audio_file}" \]\]; then
mtime_mp3="$(stat -f '%m' "${mp3_file}")"
mtime_lossless="$(stat -f '%m' "${lossless_file}")"
if [[ "${mtime_mp3}" -gt "${mtime_lossless}" \]\]; then
lossless_file="${mp3_file}"
fi
fi
cp -n "${lossless_file}" "${target_file}"
done' bash {} +
It should comb through a folder structure with audio files and copy them to a new folder. If there are two files with the same name it should prioritise the lossless format. When i run the noscript, I only get files with .flac format in the new folder. What am I missing?
https://redd.it/12eouk3
@r_bash
the noscript:
find /path/to/folder -type f \\( -iname "*.flac" -o -iname "*.wav" -o -iname "*.alac" -o -iname "*.aiff" -o -iname "*.ape" -o -iname "*.dsd" \\) -o -iname "*.mp3" -exec bash -c '
for audio_file do
audio_basename="$(basename "${audio_file}")"
target_file="/path/to/tracks/${audio_basename%.*}.flac"
[[ -f "${target_file}" \]\] && continue
lossless_file=""
for ext in flac alac wav aiff ape dsd; do
lf="${audio_file%.*}.${ext}"
[[ -f "${lf}" \]\] && lossless_file="${lf}" && break
done
[[ -z "${lossless_file}" \]\] && lossless_file="${audio_file}"
mp3_file="${audio_file%.*}.mp3"
if [[ -f "${mp3_file}" && "${lossless_file}" != "${audio_file}" \]\]; then
mtime_mp3="$(stat -f '%m' "${mp3_file}")"
mtime_lossless="$(stat -f '%m' "${lossless_file}")"
if [[ "${mtime_mp3}" -gt "${mtime_lossless}" \]\]; then
lossless_file="${mp3_file}"
fi
fi
cp -n "${lossless_file}" "${target_file}"
done' bash {} +
It should comb through a folder structure with audio files and copy them to a new folder. If there are two files with the same name it should prioritise the lossless format. When i run the noscript, I only get files with .flac format in the new folder. What am I missing?
https://redd.it/12eouk3
@r_bash
Reddit
r/bash on Reddit: What am I doing wrong?
Posted by u/lucidgazorpazorp - No votes and no comments
search and replace (if not equal)
I am hoping someone can help
I need to create a simple bash noscript
read through /etc/hosts
if IP does not equal "X.X.X.X" then rename it to Y.Y.Y.Y
I can run that whenever I would like and just change the IPs as necessary
Similarly, I want to do the same with printers.conf with a twist
if printer name starts with A, then change IP to X.X.X.X if printer name starts with B then change IP to Y.Y.Y.Y
Can anyone help with that, please
thank you
https://redd.it/12ex3mq
@r_bash
I am hoping someone can help
I need to create a simple bash noscript
read through /etc/hosts
if IP does not equal "X.X.X.X" then rename it to Y.Y.Y.Y
I can run that whenever I would like and just change the IPs as necessary
Similarly, I want to do the same with printers.conf with a twist
if printer name starts with A, then change IP to X.X.X.X if printer name starts with B then change IP to Y.Y.Y.Y
Can anyone help with that, please
thank you
https://redd.it/12ex3mq
@r_bash
Reddit
r/bash on Reddit: search and replace (if not equal)
Posted by u/runningonempty999 - No votes and no comments
Properly attach tmux session Jupyter Lab
Lately I have been running long Python noscripts in Jupyter lab on a remote server. However, when the server disconnects for some reason I lose all the progress, and I need to start over. Sorry in advance, I'm really new to this so I might sound plain stupid.
So I learned that a work around for this is to use tmux, in which I operate a noscript in the background? However, I am having trouble with it.
So first I open the ssh connection with the server in bash. Then I create a new tmux session (I have no other tmux sessions). Then I type the python environment I want to use and jupyter lab to open it in the browser.
Okay so I am running all the cells and then de-attach the tmux using ctrl+b and d. All good (I assume).
But then when I disconnect, and then try to connect again by attaching the tmux, I only get the log info. How am I supposed to open Jupyter lab again properly? I cannot type in that window. (Excuse my ignorance)
Anyways, I have tried creating a new pane using 'ctrl+b and %' and typing in the environment and jupyter lab to open. And also another window. I also just used the link it provides in the log window. In all cases, when I go to the noscript, it is not running at all, and there is no output?? I don't understand what I really need to do, or what went wrong.
I've also lost so much time on this lol, so currently I am running my code in the middle of the night, with my laptop on, the old fashion way. I'm going to sleep in a bit hoping the server didn't disconnect. But I feel like there gotta be something I am missing or just doing wrong. Does anyone know? I can provide more information. Thanks for the help anyway!
https://redd.it/12f3tam
@r_bash
Lately I have been running long Python noscripts in Jupyter lab on a remote server. However, when the server disconnects for some reason I lose all the progress, and I need to start over. Sorry in advance, I'm really new to this so I might sound plain stupid.
So I learned that a work around for this is to use tmux, in which I operate a noscript in the background? However, I am having trouble with it.
So first I open the ssh connection with the server in bash. Then I create a new tmux session (I have no other tmux sessions). Then I type the python environment I want to use and jupyter lab to open it in the browser.
Okay so I am running all the cells and then de-attach the tmux using ctrl+b and d. All good (I assume).
But then when I disconnect, and then try to connect again by attaching the tmux, I only get the log info. How am I supposed to open Jupyter lab again properly? I cannot type in that window. (Excuse my ignorance)
Anyways, I have tried creating a new pane using 'ctrl+b and %' and typing in the environment and jupyter lab to open. And also another window. I also just used the link it provides in the log window. In all cases, when I go to the noscript, it is not running at all, and there is no output?? I don't understand what I really need to do, or what went wrong.
I've also lost so much time on this lol, so currently I am running my code in the middle of the night, with my laptop on, the old fashion way. I'm going to sleep in a bit hoping the server didn't disconnect. But I feel like there gotta be something I am missing or just doing wrong. Does anyone know? I can provide more information. Thanks for the help anyway!
https://redd.it/12f3tam
@r_bash
Reddit
r/bash on Reddit: Properly attach tmux session Jupyter Lab
Posted by u/augusts99 - No votes and no comments