r_bash – Telegram
Why is this bash command not doing anything on macOS?

For some reason following command doesn't work on macOS in bash. It does work on other linux machine. Any idea why it doesn't work on macOS?

~ $ du -cBG . --max-depth=1 2>/dev/null | sort -n

~ $

~ $ which $SHELL

/bin/bash

Just du works.

https://redd.it/18ihhfm
@r_bash
How to grep a word where I only know the beginning and end of the word?

Let's say I have a long text and I want to find words that start with a and end with n. I thought I could simply grep -o a*n but this will get me no results, even tho those words exist. I guess grep tries to find a three letter word that is a * and n. What can I use here to express an unknown string in between the a and n?

https://redd.it/18imi6b
@r_bash
Curl - Collection history fzf support

Hello

Are there any CLI utility that you came across where I can do the following using Curl

\- History of the past requests

\- Store it as a collection

\- Search using fzf like utility.

If none, anyone interested to build such a thing and I can initiate a thread on that.

​

https://redd.it/18it5fk
@r_bash
What sed delimiter to use when all are being ued in my variable?

i have a txt file with a bunch of youtube video noscripts (alot of witch contain / , # , | ... and pretty much all special chars i can think of) i then need to add that to a second file (at the beginning of the second file) useing sed -i "1s/ ( here goes that video noscript) /" (the second txt file) . Should i maybe use something else than sed?

To specify my situation: i loop trough all lines in the first txt and if that youtube noscript does not exist in the second file i add it to the beginning of the second file with sed -i "1s/ $(that line as a varibale) /" second_file.txt

https://redd.it/18iwi2y
@r_bash
Double brackets quirk

Hi,

While I was messing with my code, I made what I thought was a typo but it seems to work anyway.
Notice how I'm not referencing the array item with ${codei}.

codes=(101 202 303)
i=0

echo $(( codesi == 101 ))
-> 1
echo $(( codesi == 202 ))
-> 0

What am I missing here? Is it a know behavior? I couldn't find anything in the references.

https://redd.it/18ixixg
@r_bash
Background Processes kept running after exiting

Hi all, first time poster here. Hope this is the right place. I was ssh’d into a server that I work on, where I was running a few processes in the background like this
- ./noscript &


I accidentally exited the server, and thought the processes would have ended. But they kept going and seemingly finished. Why is that? Can I trust that everything was done right? Thanks!

https://redd.it/18j0h5u
@r_bash
Variable substitution in a command

So I'm making this bash noscript that essentially transfers files from a source directory to a destination directory and organises them based on their extensions. I've completed this part but now I want to add a flag to exclude certain extensions like -e. I have done this with getopts and it's working fine.

The problem I'm encounterung is while executing the find command that gets me the file paths. I'm building the conditional string based on the input to the -e flag.

The code for this part :

declare excluded_extensions="-name '*.*'"
if [ ! "$excluded_extensions" == "-name '*.*'" ]; then
extension_string="-not \("
for ext in $excluded_extensions; do
extension_string+=" -name '*.$ext' -o"
done
extension_string="${extension_string:0:-2}"
extension_string+="\)"
fi

The logic is that I set a default value to the variable which is `-name '*.*'`. So if the user doesn't want to exclude any extensions (so the -e is not used) the variable value is substituted as is : `-name '*.*'` which means find all files in the directory. But if there are any extensions specified by the user then it builds the string and it becomes `-not /( -name ext1 -o -name ext2 /)` and so on. Then the value is substituted in the find command:
`find source_dir -type f $extension_string`
This is to get all the file paths

I've echoed the content of the command with the string based on various inputs and the value is showing up properly formatted in the terminal.
However when I run it there's an error with find :

`find:paths must precede expression : ``\('``

I know the code and method is very messy so I would really appreciate any help or even if there's a better strategy to this. Researched a lot for this problem on stack overflow, chatgpt but no answer. Thanks in advance. Kindly let me know if there's anything more that I should explain about the noscript, I'll gladly do so.

https://redd.it/18j79wk
@r_bash
I don't understand what is going on here.

I have a noscript that parse command line for flags that modify the noscript behaviour.
By accident, I typed invoking the noscript on cmd line with "=v" instead of "-v" and for my surprise, looks like its breaks getopts somehow because looks like that none of flags are processed in the `while getopts` loop:

```
....
set -o errexit -o nounset -o noclobber -o pipefail
trap go_exit EXIT SIGHUP SIGQUIT SIGTERM

export LC_ALL=C
...
while getopts "s:d:r:hnv" OPTION; do
case $OPTION in
s)
f_src=1
src="${OPTARG}" ;;
d)
f_dst=1
dst="${OPTARG}" ;;
r)
f_regex=1
regex="${OPTARG}" ;;
h)
usage ;;
v)
f_verbose=1 ;;
n)
f_dry=1 ;;
:)
echo "Option -${OPTARG} requires an argument." > /dev/sdtderr
usage
;;
?)
echo "unknow argument." > /dev/sdtderr
usage ;;
esac
done

# sanity checks

if [ "$f_src" == "0" ]; then
echo "Usage: the source folder must be specified." > /dev/stderr
usage
fi
```

All sanity checks that test the f_whatever=0, succeed (as f_whatever are all equal to 0, but it shouldn't as I am passing -s, -d, -r, -n.... - except instead -v it is =v)

Another question related to `set -o errexit .. trap go_exit EXIT `:
If there is any error during the execution the program simply call the go_exit as intended.
How to I get a feedback of which error generated the TRAP and where ?

https://redd.it/18jnvok
@r_bash
Sometime "wc -l" is not enough to count all the outputs of a noscript, how can I solve?

I have this function to install programs using several noscripts from a database:

function installarg(){
if grep -q 'read -r' ./$arg; then
$SUDOCOMMAND ./$arg | less -E
else
outputlines=$($SUDOCOMMAND ./$arg | wc -l)
for i in "$outputlines"; do
printf '\033['$i'A\033[K'
done
for i in "*"; do
echo $* 2>&1
done
fi
}

All this function should do is to replace extra outputs of the installation noscript.

During the installation, the progress bar of "wget" is shown, and each line is replaced depending on the state of the installation noscript.

Normally, on the same line, appear:

◆ RUNNING THE INSTALLATION SCRIPT FOR LXTASK

then something like this:

Task
Manager-.0.1.9 100%===================> 2,97M 7,11MB/s in 0,4s

and finally

◆ "LXTASK" INSTALLED (4 MB OF DISK SPACE)

That said, this is what actually does:

-----------------------------------------------------------------------
>> START OF ALL INSTALLATION PROCESSES <<
-----------------------------------------------------------------------

◆ "FFWA-NETFLIX" INSTALLED (1 MB OF DISK SPACE)

index.html.tmp 100%===================> 2,94M 5,82MB/s in 0,5s
index.html.tmp 100%===================> 802 --.-KB/s in 0s

◆ "PALEMOON" INSTALLED (110 MB OF DISK SPACE)

◆ "FFWA-WHATSAPP" INSTALLED (1 MB OF DISK SPACE)


◆ "LXTASK" INSTALLED (4 MB OF DISK SPACE)

-----------------------------------------------------------------------
>> END OF ALL INSTALLATION PROCESSES <<
-----------------------------------------------------------------------


and this is what I'd like to see instead

-----------------------------------------------------------------------
>> START OF ALL INSTALLATION PROCESSES <<
-----------------------------------------------------------------------

◆ "FFWA-NETFLIX" INSTALLED (1 MB OF DISK SPACE)

◆ "PALEMOON" INSTALLED (110 MB OF DISK SPACE)

◆ "FFWA-WHATSAPP" INSTALLED (1 MB OF DISK SPACE)

◆ "LXTASK" INSTALLED (4 MB OF DISK SPACE)

-----------------------------------------------------------------------
>> END OF ALL INSTALLATION PROCESSES <<
-----------------------------------------------------------------------


The "wc -l" command is not enough for this, I'd like to list all the outputs of the noscript and grab them in the count to replace them (see that index.html.tmp 100%[===================>] and the doubled space between ffwa-whatsapp and lxtask).

How can I solve this?

PS: previously I had to use less -E, but now I'd like to show all the outputs without having to use less.

Please, help me.

https://redd.it/18jw9zi
@r_bash
noscript to add numbers stored in environment variables

Hello, I have a task assignment as follows:

Write a shell noscript that adds the two numbers stored in the environment variables `WATER`
and `STIR` and prints the result.

* `WATER` is in base water
* `STIR` is in base stir.
* The result should be in base bestchol.

the noscript must only contain two lines including shebang, and use no operators such as &&, ||, ;, sed or bc.

the noscript i came up with, is as follows:

`#!/bin/bash`
`printf "%x" "$((0x$WATER + 0x$STIR))"`

assuming that the variables WATER and STIR are set, i understand that i first need to convert the variables from base water and stir respectively, to decimal and add these conversions.

I then converted the result from decimal to base bestechol by mapping the decimal values to corresponding values in bestechol. i am stumped here... while i did ask someone for help, and got the following result:

echo $(printf %o $(($((5#$(echo $WATER | tr 'water' '01234'))) + $((5#$(echo $STIR | tr 'stir.' '01234'))))) | tr '01234567' 'behlnort')

i have no idea how the mapping was done to `behlnort.` Additionally, testing this against the given test cases works for one testcase and none of the others.

**edit during typing:**
i just realised while asking that the mapping was arbitrary and mapping to `behlnort` was arbitrary and i could just use `bestchol.` i am so excited to solve it.


https://redd.it/18jxx4s
@r_bash
Why i can't get output when i use subshell?

I'm trying to solve OverTheWire's natas16 challenge its a simple command injection '$()' chars aren't restricted so i can run code through input. Tried something like "$(grep <token> /etc/natas_webpass/natas17)easters" as input it works but there is no output. When i read write-ups about natas16 similar ways are shared. If token in password file there is no output if not easters out. I can't understand why i can't get output when grep find something in password file?

lab is: http://natas16.natas.labs.overthewire.org/

username:natas16

password: TRD7iZrd5gATjj9PkPEuaOlfEjHqj32V

source code:

```<?
$key = "";
if(array_key_exists("needle", $_REQUEST)) {
$key = $_REQUEST["needle"\];
}
if($key != "") {
if(preg_match('/[;|&\\
'"\]/',$key)) {
print "Input contains an illegal character!";
} else {
passthru("grep -i "$key" dictionary.txt");
}
}
?>```

https://redd.it/18kdsef
@r_bash
red syntax error in vim

Hi,

Working my way through TLCL by William Shotts and just curious about why this line in VIM has a red syntax error. It seem to work.

https://imgur.com/a/4fs4Sj8

if (( ((INT % 2)) == 0)); then

Also, why is there a space between (( (( but not once between 0)), seems inconsistent.

Thanks

https://redd.it/18kwk3s
@r_bash
Tips for beginners

I’ve been thinking of starting learning bash noscripting first quite a while and now that I have the time I want to know how to start. Can someone please suggest any links or materials?

https://redd.it/18lijx4
@r_bash
Problem with understaning bash while trying to follow the manual on installing Dolphin AI locally

Hello.

I'm running Ubuntu through WSL, and I have to say that I'm completely new to Unix in general. The problem is: I'm currently following a step-by-step manual on how to install Dolphin from here:

https://erichartford.com/dolphin-25-mixtral-8x7b

Specifically, this part:
> git clone https://github.com/ggerganov/llama.cpp.git
> cd llama.cpp
> make -j
> cd models
> # download whichever version you want
> wget https://huggingface.co/TheBloke/dolphin-2.5-mixtral-8x7b-GGUF/resolve/main/dolphin-2.5-mixtral-8x7b.Q5KM.gguf
> cd ..
> ./server -m models/dolphin-2.5-mixtral-8x7b.Q5KM.gguf -c 16384


Everything went smoothly until the last command. The response I get to it is:
> -bash: ./server: No such file or directory


And yeah… when I run ls, I can actually see that there's no such directory. Since the manual is pretty much step-by-step, I'm kind of stumped. What did I do wrong and how can I fix it? Any help, please?

https://redd.it/18m3vf6
@r_bash
How to handle empty values in bash

I'm trying to obtain the value of the "DEFAULT SOURCE VERSION" column for both rows. The issue arises from the fact that the "DEFAULT SOURCE VERSION" is located in the 4th column. However, there is an issue with the "lawn-mower" row where the column "PREVIOUS DEFAULT VERSION" is empty. As a result, when accessing the 4th column using a bash command, the correct value (1.25.0) is retrieved for "earth-shaker," but for "lawn-mower," it returns 1 due to the absence of a value in the "PREVIOUS DEFAULT VERSION" column. The expected output is "1.25.0" for both rows, but the actual output shows "1.25.0" for "earth-shaker" and 1 for "lawn-mower.

Tabular data

The columns here are not separated by any specific delimeter as versions may vary. I tried to use bash arrays to solve this issue but i was not able to resolve this issue .

&#x200B;

OVERALL:

Expected output :

1.25.0

1.25.0

My Output:

1.25.0

1

&#x200B;

Tabular data

NAME DEFAULT VERSION PREVIOUS DEFAULT VERSION DEFAULT SOURCE VERSION INSTALLED VERSION INSTALLED SOURCE VERSION UNKNOWN ERROR PENDING SUCCESS/DONE
earth-shaker 1 0 1.25.0 1 1.25.0 0 0 0 1
lawn-mower 1 1.25.0 1 1.25.0 0 0 0 1


https://redd.it/18m64pb
@r_bash
why is my noscript not working?


The noscript reads domain names from a text file, performs a DNS query using "dig +short NS" for each domain, and checks if "ns14.net" is present in the results. The issue is that, despite correct manual queries returning "ns14.net," the noscript consistently outputs "Nein" (No) for all domains, even if the correct answer would be Yes.

&#x200B;

Can someone help?

#!/bin/bash

if $# -ne 1 ; then
echo "Verwendung: $0 inputdomains.txt"
exit 1
fi

input
domains="$1"

desktoppath="/mnt/c/Users/stiglmmi/Desktop"

output
csv="$desktoppath/Ergebniss.csv"

echo "Domain;IntX?;dig-Ergebnis" > "$output
csv"

while IFS= read -r domain; do
result=$(dig +short NS "$domain")

# Debug-Ausgabe
echo "hdaten für $domain"
echo "dig-Ergebnis: $result"

# Überprüfen, ob "ns14.net" in den Ergebnissen enthalten ist
if [ "$result" == *"ns14.net"* ]; then
nx="Ja"
else
nx="Nein"
fi

# Ergebnisse in der Ausgabedatei in separaten Spalten anzeigen
echo "$domain;$nx;\"$result\"" >> "$outputcsv"

# Debug-Ausgabe
echo "Ja/Nein: $nx"
done < "$input
domains"

echo "Skript abgeschlossen. Ergebnisse wurden auf dem Desktop in 'Ergebniss.csv' gespeichert."


&#x200B;

https://redd.it/18msvkm
@r_bash
Can I have a noscript output live/changing data?

Say I'm running a few dozen processes 3 at a time. I'd like to have the output show which is running, and update the existing output it to say when they're done. Is this possible?

eg:

Job 1: running
Job 2: running
Job 3: running

which would then change to:

Job 1: completed
Job 2: completed
Job 3: running
Job 4: running
Job 5: running

I'd prefer to do this without clearing the whole terminal.

https://redd.it/18mwl2z
@r_bash
Some useful bash projects for linux servers as a helpdesk tier 2?

What I've made

- log vomitter (thing that vomits logs so that I can use logrotate and verify-I know that's stupid).

- glassfish/payara domain restart command.

That's all that I've needed and I've made them. Is there anything else that I can make? That will be useful to me a helpdesk tier 2?

https://redd.it/18mvtld
@r_bash
Sed help needed!

I asked ChatGPT to help me create a function to help me parse YT URL's. FYI a YT URL consists of a string where it can have the video id alone, or the video id plus extra metadata. The video id is 11 characters long. Anything after it, after a "&" or "?" character is just extra metadata These 3 sample YT URL's are the exact same video: youtu.be/watch?=12345678901 and `youtu.be/watch?=12345678901&pp=abcdefghij%3D%3D` and `youtu.be/watch?=1234678901?si=abcd1234`

The code that ChatGPT gave me was:

youtube_url=$1

video_id=$(echo "$youtube_url" | sed -n -e 's/^.*[?&]v=\([^&]*\).*$/\1/p; s/^.*`youtu.be`\/\([^?]*\).*$/\1/p')

if [ -n "$video_id" ]; then

echo "$video_id"

else

echo "Video ID not found in the given URL."

This works, basically. But my problem is that it only really works correctly with URLs of the first type. This will echo the correct video id,12345678901 and end. Which is the correct response I want with all 3 URLs. With the second or third type of URLs it will echo a Job id# then12345678901 and waits for me to hit ENTER before echoing a job id# with +Done and the function name. This is an unwanted result as I just want the result and no job id#s or having to press ENTER or an echo of the function name. What do I have to add for this to happen???

https://redd.it/18n7utu
@r_bash
Was planning to use the output of a command in a bash noscript, but I don't know how to deal with the command behavior

I'm fiddling with motd, to be able to display some information at login.

I created this noscript:

#!/bin/bash
echo "OS: $(lsbrelease -s -d)"
echo "sendmail: $(sendmail -V)"

Fantasizing about this result:

OS: Ubuntu 22.04.3 LTS
sendmail: sSMTP 2.64 (Not sendmail at all)

But got this instead:

OS: Ubuntu 22.04.3 LTS
sSMTP 2.64 (Not sendmail at all)
sendmail:

Then I tried to assign the result of "sendmail -V" to a variable and get it printed:

#!/bin/bash
echo "OS: $(lsb
release -s -d)"
sendm=$(sendmail -V)
echo "sendmail: ${sendm}"

But it didn't work:

OS: Ubuntu 22.04.3 LTS
sSMTP 2.64 (Not sendmail at all)
sendmail:

Apparently "sendmail -V" is related only to sSMTP.

My actual point here is to learn what is going on, and if it's possible to achieve what I want with this specific kind of output. I kind of see what is going on, I mean, that the output is different than what I see in other commands I've dealt with before, but have no idea how to begin to understand it or to talk about it. I don't really care about displaying the version of sSMTP, it's just overall curiosity now.

UPDATE: $(sendmail -V 2>&1) did the trick, it was going to stderr and I just wouldn't find out by myself. Thank you!

https://redd.it/18n7343
@r_bash
Bash noscript as a service - but able to display STDOUT on request?

Hi guys

I have written a series of Bash noscripts that run as services on my home server.

But is there a way, I can get the running service to come to the foreground and display STDOUT (and maybe evend STDERR) as if it was running as a standard noscript.

Like when I run a noscript in Screen, I can always just type screen -r (pid) and it pops to the front again, then CTRL+A D and it goes back to the background. I want to do something like this but as a service?


Edit: Reading the man page for screen. I see there is an option -S that allows me to name a screen. If I t was to put under exec in my service file: screen -S nameofservice ./nameofnoscript.sh would that run the service in a screen session then I can use screen -r nameofservice to bring it to the foreground.

https://redd.it/18ni4n6
@r_bash