r_bash – Telegram
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
"table=2 dl_dst=00:00:00:00:00:07 action=strip_vlan,output(2)"

\#vlan 2

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

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

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

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

\#vlan 1 broadcast

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

&#x200B;

\# rules for switch 4

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

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

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

&#x200B;

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

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

\#vlan 1

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

\#vlan 2

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

\#vlan 1 broadcast

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

&#x200B;

\# rules for switch 5

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

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

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

&#x200B;

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

ovs-ofctl --protocols=OpenFlow13 add-flow s5 "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 s5 "table=2 dl_dst=00:00:00:00:00:07 action=strip_vlan,output(1)"

\#vlan 2

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

\#vlan 1 broadcast

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


I'm getting this error when I run it
```ovs-ofctl: none of the usable flow formats (OpenFlow10+table_id,NXM+table_id) is among the allowed flow formats (OXM-OpenFlow13)```, I've looked online and have used a few ai bots to help fix it but I can't.

&#x200B;

Where am I going wrong? What do I do to fix it and make it work the way I want it too?

&#x200B;

Any help is great thanks.


https://redd.it/18fzy3d
@r_bash
Metrics on a bash noscript

Hello all!

Does anyone know some good ways to gather metrics on a bash noscript? I’m doing a report on a noscript and aside from the time command, I can’t think of any other way to gather data about the noscript.

https://redd.it/18g1urs
@r_bash
OK, I give up. I am search fail. A little grep help please.

I have a file with mostly a crap ton of lines that start with "http".

There are a few lines that start with an English word. A few of those lines are followed by another line which also starts with an English word.

I'd like to grep, or whatever, and get all of the lines that start with a word and are followed by a line with a word, but I only want the fist line displayed in result. eg..

heading
subheading
http..bla...
http..bla...
http..bla...
subheading
http...bla
heading
subheading
http.. bla..

I just want all of the heading lines.

https://redd.it/18guyqu
@r_bash
I want to "cat" some files with unknown names and a small pause in between

This can easly be explained with an example.

I have a directory with several txt files: 1.txt 2.txt 3.txt and I want to read 1.txt, then press enter and read 2.txt, press enter and read 3.txt. Instead of pressing enter I currently just use sleep 5, but I know how to change that later. However, the names are not 1 2 3 but something else I don't know, because I want to use this skript in several directories with different content.

&#x200B;

Problem (or better said challenge, since there are no problems):

When I type cat *.txt it will display all .txt files, but I cannot read that fast. I would like to do something like cat 1.txt; sleep 5; clear; cat 2.txt; sleep 5; clear; cat 3.txt; just without typing every filename in there. Is there a way to read the contents of a directory and fill this out automatically?

https://redd.it/18hllm1
@r_bash
Join 2 Bash Arrays in Json using jq

I have two bash arrays.

COLORS=(blue yellow)
FOODS=(cheese ham)

I would like to use jq to combine the two arrays into json that looks something like this.

{
"blue": "cheese",
"yellow": "ham",
}


Or even this

{
"blue": "cheese"
}
{
"yellow": "ham"
}

If not possible I could make it work this way as well, although this would be less preferred.


{
"key1": "blue",
"key2": "cheese",
}
{
"key1": "yellow",
"key2": "ham",
}

Any thoughts on how to do this? Thanks!

https://redd.it/18hmyuv
@r_bash
Receive notification at SSH Login

Hi all!

I have a new machine on which I have set up an SSH server. I wanted to setup a little noscript so that I can receive a Telegram notification upon login. Following a few online guides I have put together the following noscript:

#!/usr/bin/env bash

TELEGRAMTOKEN="MYTOKEN"
CHAT
ID="MYCHATID"

# Build the SSH login message
SSHMESSAGE="SSH login detected on $(hostname) at $(date) by user $(whoami) from $(echo $SSHCONNECTION | awk '{print $1}')"

# Send the message to Telegram
curl -s -X POST "https://api.telegram.org/bot$TELEGRAMTOKEN/sendMessage" \
-H 'Content-Type: application/json' \
-d '{"chat
id": "'"$CHATID"'", "text": "'"$SSHMESSAGE"'"}' > /dev/null 2>&1

How can I have this running at each SSH login without interfering with the process? I had this set up in the past by modifying the SSH configuration under etc/ssh/sshd_config with

ForceCommand /usr/local/bin/sshloginnotify.sh; CANT REMEMBER

The command in the square bracket was added because initially the noscript would run at login, and log me out immediately after, but I can't remember how I had fixed that.

Ultimately however, completely removed the ForceCommand as it would not let me work remotely using Remote SSH login with Vscode.

Any help on how to set this up properly is highly appreciated, and sorry for my scarce knowledge, I am just getting into this!

https://redd.it/18hmv25
@r_bash
Hacky bashy way to restart a server when frozen?

Whenever a tomcat server inside linux is frozen(by frozen, I mean stops printing logs and gets stuck), I want to restart it, automatically.
What is a bashy way to do it?

https://redd.it/18i1mij
@r_bash
Run a command as a non-root user when logged in as root

I have a noscript that requires root privileges and I don't want to hard code sudo (or doas) in the noscript. Thus, I run the noscript with sudo. So far, so simple.
However, some commands in the noscript have to be run as a non-root user. Is there a way to accomplish this?

https://redd.it/18i8a1r
@r_bash
ffmpeg and stdout vs stderr

Hi there...

I am aware that ffmpeg outputs everything on screen to stderr. I know how to make it output to stdin, but what I actually want is only the progress stats to output to stdin. Does anyone have an idea on how to accomplish this?

https://redd.it/18i8zpn
@r_bash
Resizing all images in a folder, with GUI progress bar, and total size

I created a simple noscript that uses imagemagick to go through all files in a folder and resize them, but I wanted to improve it, since the only way to monitor the progress of the command was to actually watch the files as they changed sizes (noscript is saved in *\~/.local/share/nautilus/noscripts* so I can access it from a right-click menu).

I also have a habit of checking the folder size before and after, because I like seeing how much space it saved after the noscript has run. There are two noscripts I use, one to resize to 4K (4000px on long edge), and one to resize to 1920 on the long edge.

I learned about *zenity* yesterday, and spent a few hours going through documentation and experimenting with the results. I don't have anyone else to share it with, so here it is!

#!/bin/bash

DIR="."
SIZE_ORIG=`du -h . | cut -f1`

# Starting Count
PROGRESS=0
NUMBER_OF_FILES=`find $DIR -maxdepth 1 -type f -name "*.*" | wc -l`

(for file in *.*; do
PROGRESS=$(( PROGRESS+1 ))
SIZE=`du -h . | cut -f1`
echo "# Resizing $file ... ( $SIZE_ORIG → $SIZE )"
mogrify -resize "1920x1920>" -quality 90% $file; sleep 0
echo $(( 100*$PROGRESS/$NUMBER_OF_FILES ))
done) | zenity --progress --width=420 --noscript "Resizing "$NUMBER_OF_FILES" files..." --percentage=0

The result looks like this:

&#x200B;

https://preview.redd.it/mek0ykrts96c1.png?width=789&format=png&auto=webp&s=846aea13d344c3dee04e296c1e98e22c8671abfd

https://redd.it/18i9upl
@r_bash
TIL to continue too

So I have this wee app (bash function), gsi, which loops through files in a git clone, offering actions on each. And it showed me the dir fred/ today.

I do ignore fred.* files in git, but I don't ignore fred/ dirs, could be intersting stuff in them.

But I still don't want to see them in this app, they're not often inetresting.

So I asked the GPT how to add my own ignores list, and it suggested

declare -a CUSTOMIGNORES=("fred" "." "*.cd")
for file
in $(gitstatuslinedirchanges); do
[ -f "$file_" ] || continue
git check-ignore -q "$file" && continue
for ignore in "${CUSTOM
IGNORES@}"; do
[ "$file_" == *"$ignore"* ] && continue 2
done
done

I've been writing bash for 30+ years, I never knew you could continue 2.

HTH you next week, ...

https://redd.it/18if7qa
@r_bash
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