r_bash – Telegram
Functions and Libraries

So...... I have started moving all my snips of valuable functions that I use into a bunch of library files which I will make available to anyone who wants. The hardest part is documenting everything so it is actually useful.

My next step, once this step is done, is two make a "bash make" tool, that scans your noscript, scans the libraries that are called using `source` and then builds a single file containing only what is needed. Single file is easier for distribution.

BUT!!!! I have a question: Some of my functions from abc.lib.sh are needed in xyz.lib.sh as well as getting used by mainnoscript.sh. The kicker comes in that if I `source abc.lib.sh` in both the other files, the function loads twice which causes an error.

I can do a test before the source command to see if it is already loaded. I just want to know what is common practice for sequence of events.


I am currently doing:

1. declare statements
2. source statements
3. functions
4. main code

https://redd.it/18ae4xk
@r_bash
Help needed with my noscript

So basically i made simple neofetch, and i want to add ascii on left of the text.

i was thinking about creating something like list of ascii that i can change

github repo with code: https://github.com/Kotuu3/shfetch

https://redd.it/18akcqk
@r_bash
Why learn bash if there's python?

I don't get it. Python is inherently a simpler language(in terms of syntax mainly which obviously makes it easier to learn and stuffs).

Why should I learn bash when I can do everything else in python?

https://redd.it/18amkcn
@r_bash
Backup database with rsync to remote server, how?

My brain is stuck trying to figure out how to do this with rsync in a noscript.

This is what I have so far, and its just very wrong:

docker compose exec -T database pg_dump -U user teslamate | rsync -az 192.168.1.100:/media/user/backup/teslamate/"teslamate.bck.$(date)

any suggestions?

https://redd.it/18aobj0
@r_bash
Bash noscript with ffmpeg uses 71 GB memory, how do I fix it?

​

https://preview.redd.it/zyznrwd4oe4c1.png?width=1330&format=png&auto=webp&s=cf9d3e9f41de3a4274b6a7183760f70f632fd74f

​

* [I am trying to generate a mosaic from 4 input videos using filter complex](https://trac.ffmpeg.org/wiki/Create%20a%20mosaic%20out%20of%20several%20input%20videos)
* The mosaic is generated inside a for loop because start time is different in each iteration on the mosaic
* Then I cut a part of the video as specified by timestamps in array (basically 4 parts) and finally join them
* Then delete the intermediate stuff
* [Here is the bash noscript that does all this](https://pastebin.com/vB9GMMj4)
* When tested on 640x480 the whole thing works perfectly
* When run on 1920x1080, first 2 iterations work well, 3rd iteration gets a KILLED 9 error meaning I guess it ran out of memory
* I am on Apple M1 16GB Sonoma 14 if that helps
* How can I resolve this?

https://redd.it/18b3yg6
@r_bash
Detecting hot keys and inserting text into stdin

Right now i have a program using a simple loop to get input:
while true
do
read -ep "" string
echo "Text Entered: $string"
done

I would like to be able to detect a hotkey combination, such as ctrl+1, and from that, insert into the currently edited stdin.
to be clear, i don't want to edit the input after getting it, but instead add text into the input. for example how some programs automatically add a closing bracket when you enter an opening one.

is this possible? where should i look? i have already looked around and can't find anything for the inserting text.

https://redd.it/18bfn2z
@r_bash
Help creating AWK noscript that compares values ?

I need to create a awk noscript that does the following. Finds the specific Max value and Min value for a specific hour found in files that represent a day. So I have a directory with files multiple files like this

day1.csv
day2.csv
day3.csv
day4.csv
day5.csv
day6.csv
day.7.csv

Here is an example of what a few lines in each file would look like

Google,2023-11-30T04:00:00.000Z,38322391063,dev1
Google,2023-11-30T05:00:00.000Z,17091898399,dev1
Google,2023-11-30T06:00:00.000Z,19000746641,dev1
Facebook,2023-11-30T04:00:00.000Z,38322391063,dev1
Facebook,2023-11-30T05:00:00.000Z,17091898399,dev1
Facebook,2023-11-30T06:00:00.000Z,19000746641,dev1

if we take a look at the second field 2023-11-30T04:00:00.000Z , 04 = 4th hour.

So what I am wondering is how would I go about going through all the day files , getting the lines with the specific hours and their first field (Protocol) and 3rd field (usage). So for example if I wanted to see the Max and Min value for the 4th hour for Google over the 7 day files , I would have to find each line in each file that has Google as a first field and contains 04 as the hour in its date field and then store its 3rd field , the usage , and then compare all those lines to see which is the Highest value and which is the lowest. If someone could help me understand how to do this with awk I would really appreciate it.

https://redd.it/18bk8h2
@r_bash
Replacing white spaces with underscores. I originally put this on the Linux help sub but people here might also like it... I may edit this more and made one quick edit just now. This topic comes up a lot for those new to Linux ...

(This is a recent version I just finished. Before anyone points this out, yes, I know there are many ways to do this and you can do it with just one line. I am fully aware that most of those in this sub already know how to deal with the issue this noscript addresses but I felt like sharing it anyway because - why not?)

A few points:

*The help menu is there for a reason and that's why it doesn't have the simple one line and instead informs the user on how the tool works. I think noscripts should be helpful for not only technical people but folks new to Linux too.

*The find command does have flaws here and I was hesitant to specify max depth - but I did this because I felt it was a good balance between specifying no depth an using a simple max depth of only 1

*Wild cards are powerful - and I always have to think if I should use them - but I decided to go for it. Other than that, all I can say is I make the best decisions I can in the limited time I have to write this full knowing they always have flaws and if I obsessed over everything, I would get nothing done. So I move fast but unlike Facebook, I try "not" to break things, haha.

\#!/bin/bash -e

​

if [[ "$1" == "-h" \]\]; then

echo "================== Help Menu =================="

echo This noscript requires two arguments

echo "Ex: filerename [ \~/Desktop \] [ \\"file name\\" \]"

echo "Changes 'file name' in \~/Desktop to file_name'"

echo -e "Use quotes or wildcards to reference the file"

echo "[ \~/Desktop \] [ file*name (or) \\"file name\\" \]: \\"file_name\\""

echo "================== filerename ================="

exit 0

fi

​

varfind=$(find "$1" -maxdepth 2 -iwholename "*$2*")

​

​

if [[ -z "$varfind" \]\]; then

echo No results - please enter a valid string

echo Also consider altering your syntax

else

echo "$varfind"

read -r -p "The file(s) above match the string you entered.

Do you want to rename them? Enter \\"yes\\" or \\"no\\": " ans

if [[ "$ans" == "yes" \]\]; then

echo OK - processing files now

logloc=$HOME/tmp/remove-whitespace-rename.log

​

echo "$varfind" | while IFS= read -r fileitem

do

if mv -v "$fileitem" "$(echo "${fileitem}" | sed 's/ /_/g')" | tee -a "$logloc"; then

echo "success!"

underscore=$(echo "$fileitem" | grep _ ) &&

echo renamed "$1" "$underscore" &&

\#if [[ -e "${fileitem}" \]\]; then

sleep 1s && wait

exit 0

else

echo "Error: failed to rename file"

echo Check your syntax or review

exit 1

fi

done

else

echo OK - exiting noscript

exit 0

fi

fi

​

https://redd.it/18bmgr3
@r_bash
nohup not working?

I have a simple fzf launcher (below) that I want to call from a sway bindsym $mod+d like this:

foot -e fzf-launcher

... ie it pops up a terminal and runs the noscript, the user picks a .desktop file and the noscript runs it with gtk-launcher. When I run the noscript from a normal terminal it works fine, but when I run it as above, it fails to launch anything - actually it starts the process but the process gets SIGHUP as soon as the noscript terminates.

The only way I've got it to work is to add a 'trap "" HUP' just before the gtk-launcher - in other words, the nohup doesn't seem to be working.

Has something changed in nohup or am I misunderstanding something here?

Here's the noscript 'fzf-launcher' - see the 3rd line from the end:

#!/bin/bash
# shellcheck disable=SC2016

locations=( "$HOME/.local/share/applications" "/usr/share/applications" )

#print out the available categories:
grep -r '^Categories' "${locations[@]}" | cut -d= -f2- | tr ';' '\n' | sort -u|column

selected_app=$(
find "${locations[@]}" -name '*.desktop' |
while read -r desktop; do
cat=$( awk -F= '/^Categories/ {print $2}' "$desktop" )
name=${desktop##*/} # filename
name=${name%.*} # basename .desktop
echo "$name '$cat' $desktop"
done |
column -t |
fzf -i --reverse --height 15 --ansi --preview 'echo {} | awk "{print \$3}" | xargs -- grep -iE "name=|exec="' |
awk '{print $3}'
)

if [[ "$selected_app" ]]; then
app="${selected_app##*/}"
# we need this trap otherwise the launched app dies when this noscript
# exits - but only when run as 'foot -e fzf-launcher':
trap '' SIGHUP # !!!! why is this needed? !!!!
nohup gtk-launch "$app" > /dev/null 2>&1 & disown $!
fi

https://redd.it/18c26qs
@r_bash
Cut, dash, behaviour in noscript using stdin creates surprising result

Can anyone help explain below? In short, when the input is cat'd and read as input cut behaves differently, and seems to consider - a delimiter. Why would it do this?

$ cat in

M – Municipality

$ cat in|cut -c 1-4

M –

$ echo 'M – ' | cut -c 1-4

M –

$ cat `mycut.sh`

while read X ; do echo $X | cut -c 1-4 ; done

$ cat in|./mycut4.sh

M

What....? Where did the dash go...? Thanks all!

https://redd.it/18c8so8
@r_bash
Help with selecting a Chrome or Firefox tab/window for typing

Hello,

I would like to put together a noscript where the noscript selects a particular Chrome or Firefox tab and then types in a character every few minutes (to keep the website from timing out). Does anyone know how to do this in Bash in a Wayland DE? Thank you!

https://redd.it/18c8cag
@r_bash
virtualenv:command not found

i am a complete newbie trying to use venv in bash. Its been showing this for so long..i tried searching for solutions on stack overflow but theyre all for macOS and im using linux. Can someone please tell me what to do?

https://redd.it/18cuec6
@r_bash
Can you capture lines displayed to the terminal as they are shown on the screen in a bash variable?

So, you can do a lot to control what part of the terminal some text gets printed to using escape/control codes with `printf`. For example,

printf '\033[D'

will move the cursor 1 character to the left, basically making it a backspace. So,

printf '12345\033[D\033[D\033[D\033[D\033[Dabcde'

will print `12345`, then go to the left 5 characters, then print `abcde`, which is the only output you see on screen. If I try and capture this in a variable and print it

x="$(printf '12345\033[D\033[D\033[D\033[D\033[Dabcde')"
echo "$x"

you get

abcde

BUT, if you run `${#x}` you get 25, not 5. And, sure enough

cat -A <<<"x"

gives

12345^[[D^[[D^[[D^[[D^[[Dabcde$

meaning that `$x` has the full series of commands, not just the output shown on screen.

Any good way (i.,e., without writing a full-fledged ascii parser) to get just the text printed as shown on screen (i,.e., so `x='abcde'; [[ ${#x} == 5 ]]`)? Can you somehow steal this from the screen buffer?

***

ACTUAL USE CASE

I wrote [this](https://github.com/jkool702/misc-public-noscripts/blob/main/memtester/memtester.sh) short noscript that runs a number of forked instances of `memtester`, which is a program that checks your RAM for stability (since unstable RAM causes all sorts of issues).

`memtester` is only single-threaded, so my noscript takes all the "not-unevictable" memory (minus 1 gb) and splits it up between `$(nproc)` memtester instances, each running in a bash coproc. Each instance sends its stdout/stderr both to unique log files and to the stdout/stderr of the shell that forked them all off.

Problem is `memtester` uses the backspace method described above to update what is shown on screen, so when you see

TESTNAME: ok

if you ran the output through `cat -A` youd see

TESTNAME: setting 1 ^H^H^H^H^H^H^H^H^Htesting 1^H^H^H^H^H^H^H^H^Hsetting 2 ^H^H^H^H^H^H^H^H^Htesting 2 ... setting N^H^H^H^H^H^H^H^H^Htesting N^H^H^H^H^H^H^H^H^Hok

As an example, getting [these 42 lines of output](https://github.com/jkool702/misc-public-noscripts/blob/main/memtester/memtester_1.4mb_output.txt) output involved 1.4 mb of recorded commands in the log file.

Im trying to figure out a decent way to monitor all the instances (while ensuring that one's backspacing doesnt overwrite/hide another's error message. And, well, this would be much easier if I could collapse the output into just what got printed top screen.

https://redd.it/18czvbq
@r_bash
While loop runs only once

Hi. I wrote the below function in my bashrc file:

function kube-find(){
if "$#" -ne 2 ; then
echo "usage kube-find where what";
echo "example: kube-find app-pod error123";
return;
fi

kubectl get po | grep $1 | awk '{print $1}' | while read i; do echo $i; kubectl exec $i -- grep -irl $2 /opt/myapp/logs; done
}


In remote server that's running RHEL it works fine but in my local wsl the while loop doesn't run for all the pods. It exits after the first one.

Does anyone know why that is? Could it be because of the difference in bash version? Thank you!

https://redd.it/18d0vfo
@r_bash
Filling white spaces and .... Drum Roll... Special characters with underscores. Oh, and I added an index for curmudgeons who go wa wa wah: big babies over equal-opportunity renaming. This is still in the editing process but I like this version...

#!/bin/bash

if [ -z "$1" ]; then
echo "This noscript requires an argument."
echo "Try 'rename-single-index -h' for help."
exit 1
fi

if [ "$1" == "-h" ]; then
echo "================== Help Menu ========================="
echo "This noscript requires two arguments:"
echo "Ex: rename-single-index ~/Desktop \"file name\" "
echo "Changes 'file name' in ~/Desktop to 'filename'"
echo -e "Use quotes or wildcards to reference the file"
echo "[ ~/Desktop
] [ file*name (or) \"file name\" ]: \"filename\""
echo "================= rename-single-index =================="
exit 0
fi

directory="$1"
searchstring="$2"

varfind=$(find "$directory" -maxdepth 2 -iwholename "*$search
string")

if [[ -z "$varfind" ]]; then
echo "No results - please enter a valid string."
echo "Also consider altering your syntax."
else
index=1
echo "Matching files found:"
while IFS= read -r fileitem; do
echo "$index. $fileitem"
((index++))
done <<< "$varfind"

read -r -p "Enter the number of the file you want to rename. Press ctrl+c to exit the noscript: " file_index

if [[ "$file_index" -gt 0 && "$file_index" -le "$index" ]]; then
selected_file=$(sed -n "${file_index}p" <<< "$varfind")
new_name=$(echo "$selected_file" | tr '[ )](
!{}&^%$' '')
logloc="
$HOME/tmp/remove-whitespace-rename.log"

if mv "$selected
file" "$newname" | tee -a "$logloc"; then
echo "Success! Renamed '$selected
file' to '$newname'"
else
echo "Error: failed to rename file '$selected
file'"
echo "Check your syntax or review."
exit 1
fi
echo "Finished processing files."
else
echo "Operation aborted."
fi
fi

https://redd.it/18dp8li
@r_bash
Bash noscript to curl URL's using getmail

Hello,

I'm trying to tweak a noscript that was posted on another sub over in r/automation (kind of an [old thread](https://www.reddit.com/r/automation/comments/gaiixq/comment/j7zzg77/?utm_source=reddit&utm_medium=web2x&context=3), not sure the original author will respond. last comment in the thread).

I've got getmail installed and the noscript works, but it's working using whatever criteria the original author of it used to evaluate which e-mails the noscript should look for to run the curl commands on.

I've really looked it over and can't tell which part of the noscript needs to be changed so that I can tailor this to my needs? FWIW, this is a dedicated gmail account that I am only interested in getting e-mails from one address (also me, but my M365 account, exclusively).

Can anyone help point me in the right direction?

#!/bin/bash

# prep
mkdir -p ~/skripty/bazos-obnovovac/prokliknute

# download emails
getmail

# search for new emails
results=$(find ~/skripty/bazos-obnovovac/new -name '*.*.*')

# We check every email to see if it is the one we want to look for links in.
for file in $results; do
if mu view $file | grep -q obnovit.php; then
#These are good results, we will continue with clicks
inzeratnoscript=$(mu view $file | grep -A2 smazán | tail -n1)
toclick=$(mu view $file | grep obnovit.php)
curl -s \'$toclick\'
mv $file ~/skripty/bazos-obnovovac/prokliknute
sleep 1
else
#These are NOT the emails we need, we will delete them.
rm $file
fi
done

More details in the linked, original thread, if needed.

Thanks!

https://redd.it/18engbv
@r_bash
Mac Terminal + Star Wars
https://redd.it/18f599y
@r_bash
LUKS encryption and decryption: In the cryptsetup-laboratory with Termux (running under the Android 11 operating system), "cryptsetup reencrypt --disable-locks --type luks2", no root access, no loop device, and an unusable "mount" command.
https://old.reddit.com/r/termux/comments/18am78j/luks_encryption_and_decryption_in_the/

https://redd.it/18fkvv2
@r_bash
How do i split a lot of files within a folder into sub folders by size?

Hello,

&#x200B;

i have about 150.000 images within 1 folder.
i need to have them sorted to sub folders - every sub folder should be maximum 8GB of size.

the only noscripts i found on the web are: "split files by amount of files", i need the "8GB per folder"

https://redd.it/18fp2za
@r_bash
Need help with bash noscript and mininet.

Hi not sure if this is the right place or not, I'm trying to make a bash noscript that works with my mininet topology. The topology is made up of 5 switches and 8 hosts, it goes s1 to h1,2, s2 to h3,4, s4 to h5,6 and s5 to h7,8 and s3 is in the middle of all of them and is the connecting bridge, I'm trying to have it where if any host has an odd number in it's mac address it will go to vlan1 and even goes to vlan2, then have it where vlan1 hosts can ping each other but not vlan2 hosts and vice versa. I must use openflow1.3, here is my current code for the bash noscript.
```# rules for switch 1

ovs-ofctl --protocols=OpenFlow13 add-flow s1 "table=0 dl_vlan=1 priority=100 action=resubmit(,2)"

ovs-ofctl --protocols=OpenFlow13 add-flow s1 "table=0 dl_vlan=2 priority=100 action=resubmit(,3)"

ovs-ofctl --protocols=OpenFlow13 add-flow s1 "table=0 priority=1 action=resubmit(,1)"

&#x200B;

ovs-ofctl --protocols=OpenFlow13 add-flow s1 "table=1 dl_src=00:00:00:00:00:01 action=mod_vlan_vid:1,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s1 "table=1 dl_src=00:00:00:00:00:02 action=mod_vlan_vid:2,resubmit(,4)"

\#vlan 1

ovs-ofctl --protocols=OpenFlow13 add-flow s1 "table=2 dl_dst=00:00:00:00:00:01 action=strip_vlan,output(1)"

\#vlan 2

ovs-ofctl --protocols=OpenFlow13 add-flow s1 "table=3 dl_dst=00:00:00:00:00:02 action=strip_vlan,output(2)"

\#vlan 1 broadcast

ovs-ofctl --protocols=OpenFlow13 add-flow s1 "table=4 action=resubmit(,0)"

&#x200B;

\# rules for switch 2

ovs-ofctl --protocols=OpenFlow13 add-flow s2 "table=0 dl_vlan=1 priority=100 action=resubmit(,2)"

ovs-ofctl --protocols=OpenFlow13 add-flow s2 "table=0 dl_vlan=2 priority=100 action=resubmit(,3)"

ovs-ofctl --protocols=OpenFlow13 add-flow s2 "table=0 priority=1 action=resubmit(,1)"

&#x200B;

ovs-ofctl --protocols=OpenFlow13 add-flow s2 "table=1 dl_src=00:00:00:00:00:03 action=mod_vlan_vid:1,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s2 "table=1 dl_src=00:00:00:00:00:04 action=mod_vlan_vid:2,resubmit(,4)"

\#vlan 1

ovs-ofctl --protocols=OpenFlow13 add-flow s2 "table=2 dl_dst=00:00:00:00:00:03 action=strip_vlan,output(1)"

\#vlan 2

ovs-ofctl --protocols=OpenFlow13 add-flow s2 "table=3 dl_dst=00:00:00:00:00:04 action=strip_vlan,output(2)"

\#vlan 1 broadcast

ovs-ofctl --protocols=OpenFlow13 add-flow s2 "table=4 action=resubmit(,0)"

&#x200B;

\# rules for switch 3

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=0 dl_vlan=1 priority=100 action=resubmit(,2)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=0 dl_vlan=2 priority=100 action=resubmit(,3)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=0 priority=1 action=resubmit(,1)"

&#x200B;

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=1 dl_src=00:00:00:00:00:01 action=mod_vlan_vid:1,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=1 dl_src=00:00:00:00:00:02 action=mod_vlan_vid:2,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=1 dl_src=00:00:00:00:00:03 action=mod_vlan_vid:1,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=1 dl_src=00:00:00:00:00:04 action=mod_vlan_vid:2,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=1 dl_src=00:00:00:00:00:05 action=mod_vlan_vid:1,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=1 dl_src=00:00:00:00:00:06 action=mod_vlan_vid:2,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=1 dl_src=00:00:00:00:00:07 action=mod_vlan_vid:1,resubmit(,4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=1 dl_src=00:00:00:00:00:08 action=mod_vlan_vid:2,resubmit(,4)"

\#vlan 1

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=2 dl_dst=00:00:00:00:00:01 action=strip_vlan,output(1)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=2 dl_dst=00:00:00:00:00:03 action=strip_vlan,output(4)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3 "table=2 dl_dst=00:00:00:00:00:05 action=strip_vlan,output(3)"

ovs-ofctl --protocols=OpenFlow13 add-flow s3