r_bash – Telegram
timeout vs gtimeout? (MacOS)

I am cleaning up a Makefile, and see this line:

TIMEOUT := $(shell command -v timeout || command -v gtimeout)


AFAIK gtimeout is needed for MacOS.

Is this really needed for MacOS?

I can't test it, I only use Linux.

https://redd.it/12bf462
@r_bash
How can i have this ignore files that are already gzipped?

I am working on a noscript and I want to ignore already gzipped files so that it is not trying to zip them again. What would be the best way to do so?

for LOG in $(find "${ARCHIVE}/${COMPONENT_TYPE}" -type f -mtime +"${AGE_TO_ZIP}"); do
gzip "${LOG}" -S .$(date -I).gz
done


Edit: Thanks for the help. Solution was to add ! -name \\\*.gz after my age variable

https://redd.it/12bjgq0
@r_bash
GPT quality check

I'm not very good at bash noscripting so I asked chatgpt solve a problem for me. I want to ask if any string in one array also exists in the other array. It gave me a result and it works, my question here is if it is a good way of doing it?

this noscript won't be running on huge datasets so this is for my curiosity.

#!/bin/bash

# define the first array
array1=("apple" "banana" "orange" "pear" "peach")

# define the second array
array2=("banana" "grape" "peach" "watermelon")

# loop through each element in array1
for item in "${array1@}" do
# check if the item is in array2
if [ " ${array2[*} " == " $item " ]]
then
echo "$item is in array2"
else
echo "$item is not in array2"
fi
done

https://redd.it/12biwvc
@r_bash
To provide an exit 1 status and also allow the noscript to continue running

Hello all,

I want to check if it is possible to add an exit 1 statement if $comparison_delta1 and/or $comparison_delta2 is not empty and then also allow the noscript to finish? The idea is that, I want to deploy my noscript to Jenkins, mark my Jenkins build failed when comparison_delta* exist and the output of it. I can go through my failed Jenkins jobs to find out the servers that aren't supposed to be pinging on my subnets.

​

Here is part of my noscript. This part is to compare the ping results of the subnets to the master/gold copy (known IP addresses). Once the comparison function is completed, the noscript will be finishing with cleaning up old ping/comparison files. Thank you all in advance.

​

\# Comparing ping result of 192.28.x.x subnet against gold copy

cat $output_file1|awk -F. {'print $1'}|while read host; do egrep -w $host $mastercopy1 >/dev/null||echo $host "${BOLD}${RED}is NOT my server!${RESET}" >> $comparison_delta1

done

echo "See result for 192.28.x.x subnet"

if [ -s "$comparison_delta1" \]

then

cat $comparison_delta1

else

echo -e "${BOLD}${GREEN}Subnet only known IP pinging!${RESET}"

fi

\# Comparing ping result of 192.27.x.x subnet against gold copy

cat $output_file2|awk -F. {'print $1'}|while read host; do egrep -w $host $mastercopy2 >/dev/null||echo -e $host "${BOLD}${RED}is NOT my server!${RESET}" >> $comparison_delta2

done

echo "See result for 192.27.x.x subnet"

if [ -s "$comparison_delta2" \]

then

cat $comparison_delta2

else

echo -e "${BOLD}${GREEN}Subnet only have known IP pinging!${RESET}"

fi

\##Cleaning up old logs

find /tmp/ \\( -name 'PingResults*' -o -name 'Non-Known_IP*' \\) -mmin +1 -exec rm -f {} \\;

https://redd.it/12bpna0
@r_bash
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
Get all arguments between named arguments

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
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
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 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
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
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
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
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
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
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
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
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
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
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