r_bash – Telegram
Does anyone know how to create a program that hat accepts three parameters and displays their average? Thanks :)



https://redd.it/xytxhx
@r_bash
While read do if

I have this one liner im trying to solve. It checks a file and does some task whenever the file changes.

Now i need to extend it a bit so that it checks another file to see if a specific keyword exist.

inotifywait -m -e close_write "/tmp/test.txt" --format --quiet | while read -r dir; do if [[ grep -q "Pattern123" "/tmp/test123.txt" \]\]; [ $? -eq 0 \] && echo "Pattern Found!" || echo "Pattern nonexitent" done.

I do belive the do if statement is the error here.

Help is appreciated how i can solve this in one line.

https://redd.it/xz0687
@r_bash
Replace all occurrences of a persons name

I have a text file with names (temp_filteredlist.txt) and a file with facts about several persons (temp_facts.txt).

I have used a for loop to create several files for each person in the list, and used grep to get the name and fact about that person into a file with their name.

names=$(cat temp_filteredlist.txt)

for name in $names;
do
touch facts-$name.txt;
grep -i "$name" temp_filteredlist.txt > facts-$name.txt
grep -i "$name" temp_facts.txt >> facts-$name.txt
wc -l facts-$name.txt >> facts-$name.txt
done

I have also tried to get the number of facts about that person on the last line, but I get the number of all the lines, not only the facts + I get the file name besides the word count. So I need some input on how to fix that.

Now it looks like this:

andrea
When it rains, Andrea goes running for at least 2 hours.
Andrea thinks that the winter is better than the summer.
Kyle will visit Andrea in the countyside tomorrow.
4 facts-andrea.txt

But I want the structure to look like this for all the files:

andrea
When it rains, Andrea goes running for at least 2 hours.
Andrea thinks that the winter is better than the summer.
Kyle will visit Andrea in the countyside tomorrow.
4

The “main” task I’m trying to solve now, is to replace all occurrences of that persons name with “this funny person” and replace all occurrences of numbers (not digits) with my birthday.

At the moment I have tried this:

sed -E 's/andrea/THIS FUNNY PERSON/ig; s/[0-9]+/30.DES/g' facts-andrea.txt >> facts-andrea.txt

sed -E 's/daniel/THIS FUNNY PERSON/ig; s/[0-9]+/30.DES/g' facts-daniel.txt >> facts-daniel.txt

But this is not a “universal” way, because I have to write each name and file name, and I’m not sure how to solve this. I tried a for loop, but it didn’t work (I probably wrote something wrong).

Also the way I currently have written it, doesn’t quite work either because the output is this:

andrea
When it rains, Andrea goes running for at least 2 hours.
Andrea thinks that the winter is better than the summer.
Kyle will visit Andrea in the countyside tomorrow.
4 facts-andrea.txt
THIS FUNNY PERSON
When it rains, THIS FUNNY PERSON goes running for at least 30.DES hours.
THIS FUNNY PERSON thinks that the winter is better than the summer.
Kyle will visit THIS FUNNY PERSON in the countyside tomorrow.
30.DES facts-THIS FUNNY PERSON.txt

I get the output 2 times and it replaces digits when I only want it to replace numbers.
If I change the >> to > I get a empty file..

https://redd.it/xz2sck
@r_bash
How to backup/sync files from multiple remote Ubuntu instances

Hi, not very familiar with file backups on Linux/bash. I have a windows machine and I'd like to backup files from 8 Linux VM's onto the Windows machine (have about 10GB of content per instance). I also have an Ubuntu workstation and can back up there too if easier. Is there software that can backup/sync remote files to my local machine?

https://redd.it/xz1tmu
@r_bash
What are some good websites with tasks except Codewars to practice using bash?

What are some good websites with tasks except Codewars to practice using bash?

https://redd.it/xz8gpi
@r_bash
Print the outputs of 2 commands on one line?

I'm trying to create a simple bash noscript that finds the number of files in a directory and the number of directories in a directory. I've got this completed by doing

find $1 -type f | wc -l
find $1 -type d | wc -l

This works but it prints out on 2 different lines. How would I get this to print on 1 line?

https://redd.it/xzx8fz
@r_bash
How to grep text between two words within single and double quote?

Sample HTML file saved to test.txt

$ cat test.txt
name="abc">
name='def'>

grep text after **name=**

$ cat test.txt | grep -Po '(?<=name=).*(?=)'
"abc">
'def'>

grep text after **name="**

$ cat test.txt | grep -Po '(?<=name=").*(?=)'
abc">

grep text between **name="** and **">**

$ cat test.txt | grep -Po '(?<=name=").*(?=")'
abc

grep all characters contains **c** or **f**

$ grep [cf] test.txt
name="abc">
name='def'>

grep all characters contains ' or "

$ grep ['"] test.txt
> ^C
$
$ grep [\'\"] test.txt
name="abc">
name='def'>

However, I can't do this. What's wrong in this syntax? How do I escape it correctly?

$ cat test.txt | grep -Po '(?<=name=[\'\"]).*(?=[\'\"])'
bash: syntax error near unexpected token `)'

What is the right way to get both "abc" & "def" without quote?

$ <the right command here>
abc
def

&#x200B;

https://redd.it/y01zi6
@r_bash
Source bash noscript in .ini file

I'm trying to source a bash noscript into a .ini file, specifically polybar's config.ini file. Is there any way to do this? I'm trying to source variable values.

https://redd.it/y0i4ra
@r_bash
How to output middle line

When i run

mpc | sed 's/ \ {2,\}/|/g' | cut - d '|' - f1

I get

Artist - song
paused
Volume 100%

What i want is to get paused as output. I understand how head and tail work however i can not get them to display the second line.

https://redd.it/y18h65
@r_bash
Trouble processing filenames with special characters in them? Lots of strange bash expansion?

I have a recursive archive of photos, organized first by year (so, say, 2005), then in year.month (so, 2005/2005.11) then potentially further. I'm working on a noscript to recursively touch files to set the date, particularly on scanned photos with no metadata other than the date I created the files. I have gotten bash to pull the correct year/month/day from the file name and apply them recursively, I'm 90% of the way there, but in the last phase, character expansion for photos whose filenames have weird characters like "!" and "'" are messing me up. I've done experiments with all kinds of escaping and encapsulation, but I'm kind of stuck on files with both ! and ', specifically this:

2005\2005 - See, She's Addicted!.jpg

My code right now is outputting a text file with the touch commands, and files like that one are tripping me up. I'm stuck down a sed/escaping rabbit hole I can't quite get out of, and would love someone with better eyes than mine to see the simple solution. Everything up to "filename="$1/$i"" is working, but I'm stuck after that. Any help is appreciated!

numCheck='^0-9+$'
function checkdates {
IFS=$(echo -en "\n\b")
local directory="$1"
for i in \ls -1 $directory; do
local year="${i:0:4}"
local month="${i:5:2}"
local day="${i:8:2}"
local sep1="${i:4:1}"
local sep2="${i:7:1}"

if ! [ $year =~ $numCheck ] ; then
local year="$2"
fi

if [ $sep1 == "." ]; then
if ! [ $month =~ $numCheck ] ; then
if [ -z $3 ]; then
local month="01"
else
local month="$3"
fi
fi
if [ $sep2 == "." ]; then
if ! [ $day =~ $numCheck ] ; then
if [ -z $4 ]; then
local day="01"
else
local day="$4"
fi
fi
else
if [ -z $4 ]; then
local day="01"
else
local day="$4"
fi
fi
else
if [ -z $3 ]; then
local month="01"
else
local month="$3"
fi
if [ -z $4 ]; then
local day="01"
else
local day="$4"
fi
fi

filename="$1/$i"
cleanFilename=$(echo $filename | sed 's|'\''|\\'\''|g')
if [ -d "$filename" ]; then
checkdates "$filename" "$year" "$month" "$day"
fi
if [ -f "$filename" ] || [ -d "$filename" ]; then
echo "touch -d \"\$(date --date='$month/$day/$year')\" '"$cleanFilename"'"
fi

done
}

checkdates . > testfile.txt

cat testfile.txt | grep -v -e "testfile" > testfile2.txt
mv testfile2.txt testfile.txt

https://redd.it/y1l8k9
@r_bash
Bash noscript to download dynamic image name from website?

Hello all. Im looking for a little help on a bash noscript that can download an image from a website that dynamically changes every 15 minutes.

https://www.wunderground.com/maps/radar/current/bgm

&#x200B;

The main radar GIF link is https://s.w-x.co/staticmaps/wu/wxtype/county\_loc/bgm/20221012/0045z.gif but it changes with the date and time, so Im looking for a noscript that can read the web page, get the latest link, so I can download the .gif every so often. Can anyone think of a simple way to do this With bash?

Thanks!

https://redd.it/y1q1zk
@r_bash
how to respect empty values in columns?

for example, I am retrieving data:


col1 | col2 | col3 | col4 | col5
1 x
2 x
3 b x
4 x
5 c x

---------

When I try printing column 2 only; doing awk '{ print 2}'.. it'll just merge in values from col 3, giving me:

x

x

b

x

c

-------

I would like it to be:

''

''

b

''

c

-------

I dont know which values will be empty when data gets back.. I just want empty values in ANY column to be respected or replaced with null or empty string?.. how should I approach this?

https://redd.it/y1rfrk
@r_bash
exactly one file found

Hi,

when I do a find in a bash-noscript, looking for files with a certain pattern, like

found=$(find $DIR --name \\*.hubba)

what is then the best way to check that exactly one file has been found?

many thanks!

https://redd.it/y2afdz
@r_bash
Iniciate in shell

Hi guys, i was trying make a shell with echo $#, but is not working. Its for a homework. Pls be fast.


cat > oiMundo.sh
\#!/bin/bash


echo “Bom dia”

echo $#

(CTRL+D)

chmod 755 oiMundo8.sh

https://redd.it/y2ehoi
@r_bash
set -e linting

Is there any way to use a linter to enforce set -e being used in all bash noscripts? I have a particularly annoying colleague who 'forgets' to use set -e and deal with errors properly, and I want to improve the quality of the codebase we're using.

https://redd.it/y2pt88
@r_bash
How can I learn more about signals and using them in bash?

I recently had to do something for work where I had to write some bash to send the TERM signal to a process. This made me realise how little I know about signals and using them in practice.

&#x200B;

What are some practical exercises I could do to get deeper into using signals?

Like maybe something I can build so I can expand my knowledge of signals and using them in the context of bash.

Or any book/doc/video you can recommend would be much appreciated.

https://redd.it/y2s6kl
@r_bash
Test if heredoc was successful?

I tried verifying if a heredoc was successful by checking status of $? on the line after heredoc, but it's not failing if an error occurred inside.

Heredoc is testing SFTP commands, so I can't run an exit or anything inside to fail it. Any suggestions?

printf "\rTesting removing a folder\r"
sftp "monitoruser@$server" <<'EOF'

rmdir incoming/test
dir
EOF

if $? -eq 0 ; then
printf "\rRemoved test directory\r"
else
printf "\rFailed to remove test directory!\r"
exit 1
fi

&#x200B;

https://redd.it/y2xhak
@r_bash
Can no longer check if associative array exists in Bash 5.2 with -v, is this a bug?

bash-4.4# declare -A a
bash-4.4# a[foo]=bar
bash-4.4# [[ -v a[@] ]]; echo $?
0

bash-5.2# declare -A a
bash-5.2# a[foo]=bar
bash-5.2# [[ -v a[@] ]]; echo $?
1


https://redd.it/y2zxg1
@r_bash
How can I see what is coming from stdout vs stderr?

I’d like to see when a command returns something what if it is stdout and what of it is stderr.

Is there any way to make that explicit?

https://redd.it/y33flb
@r_bash