r_bash – Telegram
Bash Scripting training/certification

Hi everyone,

Could someone please let me know a possible training or certification that can be done to learn bash shell noscripting better? While a wealth of information is accessible online, I would like a course that can bring me and my team from zero to pros. We don't mind paying for it, but would like it to be recognized by the wider IT community; something like what CompTIA does.

Thanks!

https://redd.it/10dud81
@r_bash
Okay.. At a bit of a loss here. Numbers in variable names?....

This is not valid according to my shell2_4GHzAdapter=wlp35s0But this isGHzAdapter=wlp35s0I have for years used numbers in variable names but my system has decided not to allow it. It was my understanding that underscores and numbers were fine but any other symbols aside numbers, letters, and underscores were not... Or is my memory a lie..?

Solution:

Apparently variables cannot start with a number. Something I have never come accross and something that all the guides on variable names I have read trying to solve this issue failed to mention. I hope this helps someone... this issue has held me up for an hour and I am mad enough to stop for the day... I feel stupid now.

https://redd.it/10e583a
@r_bash
Finding files based on number inside

HW assignment: I have 100 files, all named accordingly from 1 to 100. In each of the files, there's a random number. I need to find 15 files with largest random numbers then write the names of the said files inside some .txt file.

I have tried adding the filename into the file itself upon randomizing the numbers. Something like this:

for x in $(1 100)

do

shuf -i 1-10000 -n 1 -o $x.txt

echo "$x.txt" >> $x.txt

done

That way I could somehow sort all of these random numbers into a separate .txt file without losing the names of the files the numbers initially belonged to. Then I could maybe use head command to find the values I need, but I don't understand the logistics of it. Maybe I don't need to write the filename inside the file itself in the first place? I can't think of any convenient way to do this.

https://redd.it/10e9wjz
@r_bash
Creating a grep pattern that would work like this: <Keyword><any amount of special characters or lowercase leters><any numbers and uppercase letters>

I wanted to create a scipt that can find Invoice number from text. It basically converts PDF to txt and then searches for company name and Invoice number. And I have a problem with invoice number.

&#x200B;

For now I used this to get the invoice number (output.txt is the converted PDF)

&#x200B;

```

\#!/bin/bash

\#some code before

&#x200B;

keyword1="Rechnung"

keyword2="Invoice"

&#x200B;

\# Use grep to search for the pattern in the file and extract only the matched (non-empty) parts of a matching line

output=$(grep -ioE "$keyword1[\^0-9\]*([A-Z\]*[0-9\]+)|$keyword2[\^0-9\]*([A-Z\]*[0-9\]+)" "$path/$fileName/output.txt")

&#x200B;

&#x200B;

\# Use sed to extract the first set of letters and numbers that comes after keyword

result_array=($(echo $output | awk -F '[\^[:alnum:\]\]' '{for (i=1; i<=NF; i++) if ($i \~ /[A-Z\]*[0-9\]+/) print $i}'))

&#x200B;

\#some code after, where it finds the longest elemnt in the array

```

THe $output get the line with keyword, and then $result_array gets only the number

&#x200B;

&#x200B;

And it works if, for example the invoice number itself only has number (91895851851) or if it has letter at the start (RE19515515). But some of the invoices have letter beetween the bumbers, for example AA1111AA11. And if this is the case, rhis noscript will only take AA1111 and leave everything else.

&#x200B;

So I need a pattern that would work like this:

<Keyword><any amount of spaces, special characters or lowercase leters><any numbers and uppercase letters>

And it should return only the invoice number

&#x200B;

It should work on all cases below:

&#x200B;

"invoice nunmber is RE5050AE5050" ==> "RE5050AE5050"

&#x200B;

"invoiceNumber: - . | RE5050AE5050" ==> "RE5050AE5050"

&#x200B;

and so on.

https://redd.it/10e8mh7
@r_bash
vim in a while loop gets remaining lines as a buffer... can anyone help explain?

So I'm trying to edit a bunch of things, one at a time slowly, in a loop. I'm doing this with a while loop (see wooledge's explainer on this `while` loop pattern and ProcessSubstitution). Problem: I'm seeing that vim only opens correctly with a for loop but not with a while loop. Can someone help point out what's happening here with the while loop and how to fix it properly?


Here's exactly what I'm doing, in a simple/reproducible case:

# first line for r/bash folks who might not know about printf overloading
$ while read f; do echo "got '$f'" ;done < <(printf '%s\n' foo bar baz)
got 'foo'
got 'bar'
got 'baz'

# Okay now the case I'm asking for help with:
$ while read f; do vim "$f" ;done < <(printf '%s\n' foo bar baz)

expected: when I run the above, I'm expecting it's equivalent to doing:

# opens vim for each file, waits for vim to exit, then opens vim for the next...
for f in foo bar baz; do vim "$f"; done

actual/problem: strangely I find myself on a blank vim buffer ([No Name]) with two lines bar followed by baz; If I inspect my buffers (to see if I got any reference to foo file, I do see it in the second buffer:

:ls
1 %a + "No Name" line 1
2 "foo" line 0

I'm expecting vim to just have opened with a single buffer: editing foo file. Anyone know why this isn't happening?

### Debugging

So I'm trying to reason about how it is that vim is clearly getting ... rr... more information. Here's what I tried:

note 1: print argument myself, to sanity check what's being passed to my command; see dummy argprinter func:


$ function argprinter() { printf 'arg: "%s"\n' $@; }
$ while read f; do argprinter "$f" ;done < <(printf '%s\n' foo bar baz)
arg: "foo"
arg: "bar"
arg: "baz"


note 2: So the above seems right, but I noticed if I do :ar in vim I only see [foo] as expected. So it's just :ls buffer listing that's a mystery to me.

https://redd.it/10egdhk
@r_bash
What tool is it ?

I changed my computer and re-install everything from scratch a month ago.

But I am missing a command/tool/setting or whatever that I had on the old computer that is related to browsing/scrolling up/down the output in terminal . There was a key bind that makes the cursor jump to the begging of the previous command in the terminal. I am not talking of a simple Page Up, the thing knows exactly how many pages to scroll up on terminal output to the beginning of previous command.

Which is this one ?

I really don't remember if it is something specific to bash (I only use bash) or it is something related to KDE/konsole....

https://redd.it/10egz0w
@r_bash
Name of that utility which generates DAGs from text to SVG?

I've forgotten the name of the utility which generates DAGs from text, can you remember it?


You can give it a mydag.txt like:

a -> b
b -> c

In fact I'm very confident that was the basic syntax.

And then you can call:

cat mydag.txt | program -t noscript -o out.noscript


And a DAG will be drawn in SVG format

I'm pretty sure this image used the same utlity because it matches the default style exactly:


https://en.wikipedia.org/wiki/Directed\_acyclic\_graph#/media/File:Tred-G.noscript

(p.s its not gnuplot as far as I can remember)

https://redd.it/10eraf3
@r_bash
Another noscript

Hi everyone. Hope you all are doing well.

I am working on another noscript and I am having an issue. Sharing the noscript below:

#!/bin/bash

###########################
# Created by Diego Castro #
# and Ryan Curran #
###########################

<<'Tip'

Tip: instead of running the noscript like this: ./swaks..., you can do the following

Instead of running the noscript like this: ./swaks..., you can do the following:

1. vi ~/.bashrc
2. Add this line at the bottom: alias swaks='~/./swaks-core-lab1.sh' <- The directory changes depending on the folder you are saving the noscript.
3. Save changes - :x
4. source ~/.bashrc

Tip

# This noscript will just use the server to get the email in the lab for CORE - lab1

server=xx.xx.xxx.xx (can't share the IP)

ehlo="xxx.xxxxx" (another thing I can't share)

at="--attach"

read -p "Recipient: " recipient

read -p "Any attachments Y/n: " name

if [ ${name} == "y" ]
then
read -p "Attachment location Documents | Downloads | etc: " location

if [ ${location} == "Documents" ]
then
read -p "Name of the file: " fileDocument
$docLoc= "cd /mnt/c/Users/$USER/Documents/"
fileDoc=${docLoc}${fileDocument}
swaks -t ${recipient} -s ${server} -h ${ehlo} ${at} ${fileDoc}

elif [ ${location} == "Downloads" ]
then
read -p "Name of the file: " fileDownloads
$downLoc="/mnt/c/Users/$USER/Downloads/"
fileDown=${downLoc}${fileDownloads}
swaks -t ${recipient} -s ${server} -h ${ehlo} ${at} ${fileDown}

else
read -p "Location of the file: " fileDir
read -p "Name of the file: " fileName
otherLoc="/mnt/c/Users/$USER/"
slash="/"
fileElse="${otherLoc}${fileDir}${slash}${fileName}"
swaks -t ${recipient} -s ${server} -h ${ehlo} ${at} ${fileElse}
fi

elif [ ${name} == "n" ]
then
swaks -t ${recipient} -s ${server} -h ${ehlo}

else
echo "Error. Closing the program now."
exit 1
fi


The issue I am having now is when getting an attachment for the noscript, just for the options Downloads and Documents, the noscript gives an error on this line ${downLoc}${fileDownloads} for the Downloads options and, this one fileDoc=${docLoc}${fileDocument} for the Documents option. The only one that works is the first else which you can choose the directory. Any idea guys?

&#x200B;

Thank you so much for your collaboration and effort.

https://redd.it/10f36rr
@r_bash
CTRL-R not working?? :x

I got a new system set up and have a really weird behaviour.

- Ubuntu 22.04.1 LTS
- Bash: 5.1.16

When I hit CTRL-R I get:

bash: bash_execute_unix_command: cannot find keymap

okay.. So I guess I need a mapping for this (I thought default) hotkey.

I ran

bind -x '"\C-r": "reverse-search-history"'

After hitting ctrl-r I get:

reverse-search-history: command not found


I would be really happy about any advice or ideas!

https://redd.it/10f5n9w
@r_bash
Dynamically exclude dirs in the find command

Hi, I have made a small noscript so that given a list of directories it executes find excluding these, the corpus of the noscript is this:


EXCLUDIRS=(dira dirb 'hello word');

if [ ${#EXCLUDIRS[@} -gt 0 ]]; then
declare -a INDXS=("${!EXCLUDIRS@}");
declare -i LASTINDX="${INDXS*: -1}";

for I in "${INDXS@}"; do
EXCLUDIRSTR+="-path ./${EXCLUDIRS$I} -prune";
((I != LASTINDX)) && EXCLUDIRSTR+=' -o ';
done

EXCLUDIRSTR="( $EXCLUDIRSTR ) -o -print";
fi

# shellcheck disable=SC2086
find . $EXCLUDIRSTR;

As you can infer, EXCLUDIRSTR ends up becoming a string of the type:

'(' -path ./dira -prune -path ./dirb -prune -path ./hello word -prune ')'

This works as expected, as long as EXCLUDIRS does not have names with spaces, in this case "hello world" will flag the problem since that space could not be escaped. I have tried several ways, does anyone know what is the correct way for this?

https://redd.it/10fc6af
@r_bash
Count frequency of each "alphabet" in file

I can count the frequency of each individual character in a file using cat $1 | awk -vFS="" '{for(i=1;i<=NF;i++)w[toupper($i)]++}END{for(i in w) print i,w[i]}'.

But this prints the frequency of each character. I want to count the frequency of each "alphabet". Could someone suggest a way to do this? (I also want to convert the alphabets to lower case like I am doing in the awk noscript)

https://redd.it/10fc3qu
@r_bash
noscript windows template creation with packer

writing a bash noscript to automate the deployment of a packer windows template. I am using RHEL and have a few challenges and would love to get some help please. I have 2 passwords i would like to encrypt so that it does not show up as plain text. I also need to sometimes run the manual process several times as it sometimes fails to run.

I have all the files in my pwd and the below process is all i am needing in a bash noscript

$ export PKR_VAR_vsphere_password=*******

$ export PKR_VAR_winadmin_password=******

packer init .

packer validate .

paker build .

https://redd.it/10fgjw8
@r_bash
Auto-generate folders and convert files!

Hello,

I am trying to create a bash noscript that converts h.265 to h.264 and I would like for it to loop through each folder, convert all mkv files and then in another folder create an identical folder with the converted mkv. Essentially if there's a show with many seasons I want it to loop through each season then store the converted file in another folder with the folder name being the season it came from and so on.

Btw I'm newbie :D

Here's what I'm currently doing:

for i in *.mkv;
do name=`echo "$i" | cut -d'.' -f1`
echo "$name"
ffmpeg -i "$i" -map 0 -c:v libx264 -crf 18 -c:a copy rest1/"${name}.mkv"
done

What I'm thinking about: (this is mainly pseudo code)

for i in */;
(creates a folder named the same)
do
for i in *.mkv;
do name=`echo "$i" | cut -d'.' -f1`
echo "$name"
ffmpeg -i "$i" -map 0 -c:v libx264 -crf 18 -c:a copy ${foldernamehere?}/"${name}.mkv"
done
done

Thank you!

https://redd.it/10fn1if
@r_bash
Replace IPTables Rule Based on Rule Specification

In a noscript I have the specification of an iptable rule (like what would be used with the "iptable -A" command) that already exists and I need to replace it. Unfortunately the "iptables -R" command expects a rule number not a rule specification. Is there a way to get the rule number of a rule based on its specification? Or is there some other approach to take to replace a rule if all you have is its specification in a noscript?

https://redd.it/10g7f9b
@r_bash
monthly crontab jobs

Any idea how i can get a crontab job to run on every 2nd Wednesday of each month? not sure i can get that scheduled using crontab

https://redd.it/10gddja
@r_bash
pls help with bluetoothctl wrapper noscript ("Missing dev argument")

Hi everybody,

I am trying to write a wrapper noscript for bluetoothctl to easily connect/disconnect particular devices.

I base it on this arch wiki entry. THIS is my noscript (pastebin link).

When I run bluetoothhandler.sh trennen az_lautsprecher, it will disconnect that device and show the expected output that bluetoothctl produces; however, when I run bluetoothhandler.sh verbinden az_lautsprecher, I just get Missing dev argument.

I don't understand this. The syntax is the same for connecting and disconnecting; the functions are constructed accordingly. Yet one works, the other does not.

When I manually run bluetoothctl -- connect AB:06:CD:49:EF:E3 in the terminal, it works fine as well. When the noscript runs it, it won't work at all, while disconnect does work either way (both in the noscript and in the terminal).

Can you please tell me what I need to change in order to make connect work as well? Thank you in advance for your help :)

https://redd.it/10grvms
@r_bash