r_bash – Telegram
Command Line Issues Error But Not When Command Immediately Rerun?

1. Code produces error as expected:
`[[ 'a(' == *[(]* ]]`

`-bash: syntax error in conditional expression: unexpected token \`('`

2. Corrected by escaping the open paren but the command line still produces an error (different than the first error; almost as though it is till dealing with the first command some how):

`[[ 'a(' == *[\(]* ]]`

`-bash: syntax error near unexpected token \`'a(''`

3. When I rerun the last command using up arrow/enter, the code now works:

`[[ 'a(' == *[\(]* ]]`

`echo $?`

`0`

Why does the corrected command (2) initially fail?

https://redd.it/1k5f300
@r_bash
How not to get caught out by differences in macos and linux?

I am writing a bash noscript for building containers using Podman. My laptop is a M2 MacOS with bash 3.whatever, and my server uses alma linux (RHEL) 9.5. I aam running the following command to startup a postgres instance:

while read -r line; do
modified_line="${line//:su/$su}"
# modified_line="${modified_line//:\'sp\'/\'$sp\'}"
modified_line="${modified_line//:\'sp\'/'$sp'}"
modified_line="${modified_line//:d/$d}"
modified_line="${modified_line//:u/$u}"
modified_line="${modified_line//:schema/$schema}"
# modified_line="${modified_line//:\'pass\'/\'$pass\'}"
modified_line="${modified_line//:\'pass\'/'$pass'}"
echo "$modified_line" >> $dir/docker-entrypoint-initdb.d/0.0.0-a_modified.sql
done < $dir/migrations/0.0.0-a_users_dbs.sql


 

`modified_line="${modified_line//:\'sp\'/'$sp'}"` only works on MacOS bash and `# modified_line="${modified_line//:\'sp\'/\'$sp\'}"` only works on the almalinux bash.

 

The output between the two is vastly different as well. The MacOS version essentially creates a new file with the text replaced as desired, while the linux version writes two copies of the appended output into a new file, neither of which is correct.

 

How am I supposed to write bash code that is compliant with both systems?? Should I write in fish or another language that isnt subject to these versioning issues? Or should I save the effort and run all of my code in containers, so that I dont have to deal with this MacOS crap?

**Note:** this question isnt about how to fix the code. Im not too proud to say, I turn to chatgpt as often as I need to, but more of how to consider writing bash moving forward.

https://redd.it/1k63nk7
@r_bash
I made a CLI to use saved curl requests

I'm slowly going from using GUI apps to just CLI/TUI and while doing that I wanted to ditch Postman as the app I use to test my apis. Looked through a couple of them but none was what I wanted so I made this one.

I'ts called Curlier and it runs .sh files with the curl request inside of it. It's handy cause I can do whatever I want to the curl response inside my .sh file allowing me to just do curly <request_name> and getting the reponse parsed as I want (or whatever I want to do with it tbh).

Idk, felt like sharing it here for people to try it out, contribute and tell me what they think about it.

Kinda new to shell noscripting so be kind.

https://redd.it/1k6im99
@r_bash
What's a Bash command or concept that took you way too long to learn, but now you can't live without?

For me, it was using xargs properly, once it clicked, it completely changed how I write noscripts. Would love to hear your “Aha!” moments and what finally made things click!

https://redd.it/1k6n0v5
@r_bash
Is it possible that RSYNC lists all the directories to say that it passes for all of them?

Hello! (thanks to ~~goog~~... translator)
Is it possible that RSYNC lists all the directories to say that it passes for all of them to see if there was something inside them that has changed?
I clarify that I am using RSYNC with origin = Linux and destination (a pendrive) with Fat32.
and finally verbose say that the copy will be small weight something like equiv. to about 1 common.jpg (little transfer little copy).
See this screenshot for see the list o dirs with and without files into them...
of course I understand that dirs below are listed because they have newer files to copy, but upper them, the list is only of dirs.
https://imgbox.com/WoKhKR20
I am testing an SD formatted with Ext4 to try how RSYNC works with Linux origin and destination in both cases.
And in this case of a modest test with few test directories, when I do RSYNC, RSYNc does not list the directories, that is, it does not warn me that I pass through the directories of this small Linux Test Origin Destination (Ext4).
Thanks and greetings!

https://redd.it/1k6rg7h
@r_bash
Script works locally not when curl is used

I have a noscript that requires a y/n response that works when run locally, but when I curl it it seems as if a random character is passed:

Script `test.sh`:

#!/bin/bash

while true; do

read -p "Do you want to proceed? (Yn) " yn

case $yn in
[Y] ) echo ok, we will proceed;
break;;
[n] ) echo exiting...;
exit;;
* ) echo invalid response;;
esac

done

echo doing stuff...
df -hT

Whenever I use curl like this:

`curl -sSL https://url.com/test.sh | bash -x`

Then I get:

+ true
+ read -p 'Do you want to proceed? (Yn) ' yn
+ case $yn in
+ echo invalid response
invalid response
+ true
+ read -p 'Do you want to proceed? (Yn) ' yn
+ case $yn in
+ echo invalid response
invalid response
+ true
+ read -p 'Do you want to proceed? (Yn) ' yn
+ case $yn in
+ echo invalid response
invalid response
+ true
+ read -p 'Do you want to proceed? (Yn) ' yn
+ case $yn in
+ echo invalid response
invalid response
+ true
+ read -p 'Do you want to proceed? (Yn) ' yn
+ case $yn in
+ echo invalid response
invalid response

It seems as a character is passed continually. What is going wrong here? I really have no idea. Same noscript locally and curl.

https://redd.it/1k6umwg
@r_bash
This media is not supported in your browser
VIEW IN TELEGRAM
Telert - Telegram/Slack/Desktop alerts when commands finish
https://redd.it/1k7awst
@r_bash
Need help running automatic command on terminal

As noscript says, first of all I am new to this. I need help (not sure which MacOS terminal I should even begin with- the basic one that it comes with, iTerm2, or Tabby)

I am trying to run a sha512 hash command that will generate a seed. But I need to do it automated- way faster than manually typing. I need to run the command about 100,000 times.

The command I need to use: echo -n "1710084026-4b0f5fc279ba41b3e6d6b73fb26b8b333a1c3b7963a4c5b03f412538596b440c-UYwqnEx6DT9L-Number: 50796" |sha512sum

Which generates the seed: 312e1a1f5e194adfa429fefc001d2d01ea41d96591ae9fbbd59ab7f04a541f4d658440163142908d97a6c083b37482ab6565d9d212a95c58fab9a19589244a41

Now, I need to also change the "Number" value each time I run the command, so the seed generated changes obviously. For example, listed above is "50796", and I would need to change each time, lets say the second number I would test next would be "40048".

That would give the generated seed of:
885120a467d71ec6e14964e9898eb2ac1c49060945665d74665564bf075bbf6919ef886f37d3843993452092bcbcd39945e4774f252edd3dbfc2c6f7823af890

I need to do this for about 100,000 different numbers, until I get the seed match I am looking for. I have 120 characters for the hash seed im looking for, but missing the last 8.

I don't even know if I'm In the right place to post this, or what subreddit to do. But I desperately need help with this.


So far, I have this:

\#!/bin/bash

start_number=0

end_number=100000

target_seed="30b842d3b1c1fcf6eb24bc06f64b7d9733106633bbd98c66bda1365466a044580d0a452500397252ff4d129d17404a5ee244e0c42bab5624e86a423a"



echo "Searching for target seed pattern in range $start_number to $end_number..."

echo "Target pattern: $target_seed"

echo ""



found=false



for ((num=start_number; num<=end_number; num++)); do

\# Generate the seed

seed=$(echo -n "1710084026-4b0f5fc279ba41b3e6d6b73fb26b8b333a1c3b7963a4c5b03f412538596b440c-UYwqnEx6DT9L-Number: $num" | sha512sum | awk '{print $1}')



\# Display progress every 1000 iterations

if (( num % 1000 == 0 )); then

echo -ne "Checked: $num | Current seed: $seed\\r"

fi



\# Check for match

if [[ "$seed" == "$target_seed" \]\]; then

echo -e "\\n\\nMATCH FOUND!"

echo "Number: $num"

echo "Seed: $seed"

found=true

break

fi

done



if [[ "$found" == false \]\]; then

echo -e "\\n\\nNo match found in the specified range."

fi


But I haven't had matches, or I am doing something improperly. Does anyone have any help they could show me or point me to the right direction? Thank you so much!

https://redd.it/1k86if3
@r_bash
I know that cp does not have --exclude=this_dir/ ... but I like exclude any (only 1) subdir/

**Hi**, How can I copy a dir/ excluding only 1 subdir/ of a dir/ in this alias:

fecha="cp -r ../parcial/ ./$(date +%y%m%d)"

dir/ is ../parcial/ and exclude subdir/ is "some_subdir_name/"
Thank you and regards!

https://redd.it/1k8fs31
@r_bash
On my system, why do some commands print their output in a read-only mode that ends with (END), which I have to leave with a any-keypress? What is this and how can I turn this off once and for all?

noscript

https://redd.it/1k91qur
@r_bash
Efficiently delete a block of text containing a line matching regex pattern

File in the format:

General

StartWithLastProfile=1

Profile0
Name=default
IsRelative=1
Path=Profiles/default.cta

Profile1
Name=alicew
IsRelative=0
Path=D:\Mozilla\Firefox\Profiles\alicew
Default=1

Profile2
Name=sheldon
IsRelative=0
Path=D:\Mozilla\Firefox\Profiles\sheldon

How to delete entire block of text (delimited by an empty line) if line matches Name=alicew? It can be assumed there's only one unique match. So the file should be overwritten as:


General

StartWithLastProfile=1

Profile0
Name=default
IsRelative=1
Path=Profiles/default.cta

Profile2
Name=sheldon
IsRelative=0
Path=D:\Mozilla\Firefox\Profiles\sheldon

Preferably efficiently (i.e. requires only reading the file once) and in something relatively easy to understand and extend like awk or bash.

https://redd.it/1k9msmj
@r_bash
inotify use cases, generic app reloader responding to config changes

I'm looking for a way to automatically/efficiently do things when certain files change. For example, reload the status bar or notification application when their config changes. `inotify` seems appropriate for that, checking for changes as events instead of constantly polling with e.g. `sleep 1` in an indefinite loop (if the info you're looking to update changes rarely, the former would be much more efficient).

* Is the following suitable for a generic app reloader on config change and can it be improved? `app_reloader` is the most app-specific part of the implementation--some apps take a signal to reload the config without restarting the process, but the "generic" way would be to simply restart the process.

# This specific example is hardcoded for `waybar`, can/should it work for any
apps in general?

app_config="$HOME/.config/waybar" # App's dir to check for changes
app_cmd() { exec waybar & } # Command to start app

# Reload app. Usually means kill process and start new instance, but in this
example with waybar, signal can be sent to simply reload the config without
restarting the process
app_reload() {

killall -u "$USER" -SIGUSR2 waybar

# Wait until the processes have been shut down
# while pgrep -u "$UID" -x waybar > /dev/null; do sleep 1; done

}

while true; do
pgrep -u "$UID" -x waybar &>/dev/null || app_cmd

# Exclude hidden files sometimes created by text editors as part of
# periodic autosaves which could trigger an unintended reload
inotifywait -e create,modify -r "$app_config" --exclude "$app_config/\."

app_reload

done


* Is it a good idea to make heavy use of inotify throughout the filesystem? For example, checking `~/downloads` for when files complete their downloads (e.g if a `.part*`,`aria2`, etc. file no longer exists) and updating that count on the on the status bar (or similarly, do a `du -sh` only when a file is finished downloading, as opposed to status bars typically polling every 3-30 seconds).

* Also interested in any other ideas to take advantage of `inotify`--it seems heavily underutilized for some reason.

https://redd.it/1k9xhsf
@r_bash
comparing 2 sets of variables?

My code is unfortunately not working. It appears that it is only looking at the last 2 variables:

for reference a matches b and x matches y. I am attempting to compare the first 2 (I want a and b to match each other) and match the last 2 (I want x and y to match) if either set does not match, I want it to echo "no match".

if [[ "$a" == "$b" && "$x" == "$y" \]\];

then

echo "match"

else

echo "no match"

fi

https://redd.it/1kbkph0
@r_bash
Do you still write pure Bash, or do you mix in other tools?

At what point do you ditch Bash for Python, Go, or something else? Curious where others draw the line.

https://redd.it/1kbhtxo
@r_bash