r_bash – Telegram
Catch error in command

Hi all, Is there a way to use "try" as in Python to capture errors when executing commands?

I’m running the following:

snapshotid=$(head -n 10 /home/egonzalez/ansible/awsoutput.txt)

for snapshot in $snapshot
id ; do
aws ec2 modify-snapshot-tier --snapshot-id "${snapshot}" --storage-tier archive
done

The modify-snapshot-tier command will return the error message containing the word "already in progress" my idea is to capture the error of the command and if it contains the word "already in progress" delete id snapshot from the file to follow with other snapshot IDs.


Regards,

https://redd.it/1089cr8
@r_bash
Script can handle spaces in file names when run without sudo, but can't when run with sudo

I have a noscript that finds all pdf files in certain folder and then moves them depending in their date. However, since I move them in the shared folder, I need to run noscript with sudo in order to make permission to move them. The problem is, that with sudo, for some reason, it can't handle spaces in names. So I created a test noscript:

#/!bin/bash
OIFS="$IFS"
IFS=$'\n'
for f in find /mnt/test -maxdepth 1 -type f -name "*.pdf"
do
echo "$f"
done
IFS="$OIFS"

When run it without sudo I get the result:

/mnt/test/I love spaces.pdf

However, when I run it with sudo, I get:

/m
t/test/I love spaces.pdf

And if I run it without OIFC and IFC thing, I get this result both with sudo and without:

/mnt/test/I
love
spaces.pdf

So for some reason, when run with sudo, it removes letter "n"

https://redd.it/1089bp3
@r_bash
How to run commannds with dynamic parameters inside a bash scipt?

For an exercise, I need to run the same command hundreds of times, but each time with a different second parameter. Fortunately I have all of them in a file (block.txt). My idea was to read the values and run the command for each value as the argument inside a bash noscript. However, passing the value to the command as a variable doesn't seem to work and it interprets my command as ./aes128-cbc-crackme "00112233445566778800112233445566" "$b". Any alternative methods or ideas of how to overcome this are highly appreciated.

Here's the code:

https://preview.redd.it/y6w9p4o6k9ba1.png?width=567&format=png&auto=webp&v=enabled&s=38343f724cfd443f5f6e04c089c5b910607f3156

https://redd.it/108hx6u
@r_bash
Bash help, terrible with linux, not sure what the name of this would be

Hey all,

I have a noscript I am working on in which there is a variable that indicates the number of variables (in the example there are 4 variables - The DESIRED variables, not total variables in use). I am trying to set variable TEMP to be "VARX" in the for loop, so VAR1 on first pass, VAR2 on 2nd, etc. I am not sure what the syntax is to accomplish that.

​

The NUMVARS will be defined in the noscript, not input or anything like that (that is WAAAAY over my head, and not desired). The noscript itself is lengthy, and has numerous calls to use VAR1, VAR2 in for loops, looking to make the whole thing dynamic based on the variables defined at the top, so the NUMBER of variables may be 4 now, but could be 6 later, soo NUMVARS changes to 6 at the other 2 VARS are defined, and it prevents dozens of lines of code changes.

​

Would this be a dynamic variable? I am not sure what the name would be for what I am trying to do.

​

#!/bin/bash
NUMVARS=4
VAR1=ONE
VAR2=TWO
VAR3=THREE
VAR4=FOUR
#
for ((X=1; X<="$NUMVARS"; X++)); do
#
TEMP="$VAR${X}"
#
echo TEMP $X - $TEMP
done


&#x200B;

&#x200B;

Output from above:

TEMP 1 - 1
TEMP 2 - 2
TEMP 3 - 3
TEMP 4 - 4


&#x200B;

&#x200B;

DESIRED OUTPUT:

TEMP 1 - ONE
TEMP 2 - TWO
TEMP 3 - THREE
TEMP 4 - FOUR

https://redd.it/108vt9o
@r_bash
Modify an IPTable Rule to Include Destination Port

I need to create a noscript that reads several iptable rules and adds a destination port. Is there a way to do this without manually parsing the iptables output as text via awk? Ideally I'm hoping there's a way to ask iptables for individual pieces of data about a certain rule so I can save those into variables and then delete the rule and recreate it with the destination port added.

https://redd.it/108wpai
@r_bash
automated corrupt archive testing help

Hi all,

I've been using the snippet below to create a text file with all the corrupt rar filenames in it.

find . -type f -iname '*.cbr' -exec unrar t {} \; 2>"cbrerrors.txt" >"cbroutput.txt"

I can't help but feel, that for a bash guru it would take about 10 seconds to put together a mod that just moves the corrupt files to another location. I don't suppose I could get an assist please ?

Cheers

https://redd.it/10930zt
@r_bash
Replacing string from command output with sed

I am trying to replace a string from a command output but it's not doing anything.

When i do
echo "Usage: swaylock [options]" | sed 's/swaylock/swaylock-corrupter/g'
it successfully replace the string with the new string.

But when i try to do
swaylock --help | sed 's/swaylock/swaylock-corrupter/g'

It simply prints the help docs without replacing the string.
What am I doing wrong?

https://redd.it/1093da8
@r_bash
Trouble generating big random hexadecimal numbers

I want to generate a random number from 2 to $witness_limit ( It's value is a 1025 digit long number ). I've tried using $((2 + RANDOM % $witness_limit)) but it's causing an error due the size of the number also as far as I know $RANDOM has a limit of 32767

#!/bin/bash

generaterandom() {

head -c 256 /dev/urandom | xxd -p -u -c 256 | tr -d '[:space:]\\'
}

p="$(generate
random)"
q="$(generaterandom)"

n=$(echo "obase=16;ibase=16; ${p} * ${q}" | bc | tr -d '[:space:]\\')

witness
limit=$(echo "obase=16;ibase=16; ${n} - 2" | bc | tr -d ':space:\\')

https://redd.it/10978v3
@r_bash
Error while pulling json in youtube-dl Script

Hello Everyone,

I have been using this premade noscript from another reddit user to grab content from Discovery plus and download it using youtube-dl. Recently, it stopped gathering the URLS and I lack the knowledge to fix it. It seems to no longer be getting the json information and filtering through it. Does anyone kno how to fix this?

If this is not allowed via community rules, let me know and I will take it down.

&#x200B;

https://github.com/ohmybahgosh/YT-DLP-SCRIPTS/tree/main/DISCOVERY-PLUS-YTDLP

https://redd.it/109g561
@r_bash
bash alias show command?

My bash alias file got deleted(nvm how) while I was running the alias commands so currently I have the alias running but the bash alias file has been deleted. Is there a way I can see what the alias is running? I create alias files because I have terrible memory of what I need to run/do for my program.

https://redd.it/109czar
@r_bash
Percentage in file name breaks noscript when assigning output of command to variable

Hi everyone.

So I have this noscript, it runs fine and I was satisfied with it, until I ran into a file that had a percentage on the file name, it brakes the noscript, more specifically when doing what I think is called a subshell, what I want to do is assign the output of a command to a variable.

for i in *; do Hash+=$( sha256sum "${i%}" ); Hash+=$( echo "\n"); done; fi;

When it gets to `Hash+=$( echo "\n")` is when it stops working if there is a file name with a %. I have tried multiple ways of replacing it with printf and adding prefixes before "\\n", but it doesn't work either.

Also, really weird, for me, normally `echo "\n"` will print `\n`, but here it does what it's supposed to and adds a line.

https://redd.it/109mt98
@r_bash
Is sleep in a while true loop acceptable ?

Hello I have a system that has some commands that need to be manually issued when certain conditions are met.

I monitor journal logs to determine when these conditions are met. My current code is just a few lines. It’s just a while true loop which checks the logs from the last 5 minutes and if it finds the condition it issues the command. After that sequence it just sleeps for 5m

Everything works fine but I’m just wondering if sleep is considered amateur or “bad code” if it is should is there a better way I should do it?

I searched around and it seems like it’s ok since people say it doesn’t really consume cpu cycles. Is that correct?

https://redd.it/109u9mx
@r_bash
scp'ing a file name with spaces in it is driving me crazy

I'm trying to make a tool to scp a file to a remote directory and rename it, but it's not working. The scp works when I do it manually e.g.

scp file.mp4 user@host:/path/to/directory/"new name.mp4"

but with the noscript it always fails with

No such file or directory

or

ambiguous target

When there are no spaces in the file name it works fine, and when I do it manually it works fine using 1x quotes for the file name on the local machine and 2x quotes for the file name on the remote machine. The noscript is like this:

echo "Which file to transfer?"
read noscript
echo "What to save it as?"
read name
scp $noscript user@host:/path/to/folder/'"$name"'

There are 2x quotation marks in the location on the remote machine (I even tried 3x quotation marks). This got the error

<last word of the renamed file name>.mp4" No such file or directory

How do I use variable and name with spaces together in a bash noscript??

Thanks

https://redd.it/109tcyn
@r_bash
Alias to locate for greater ease

Hello.

This isn't great code, but it saves you from typing three commands when your locatedb is out of sync with the filesystem, so it was an idea.

I update the locatedb once per day, but that doesn't seem to be enough, I should probably have it run every 2 hours. And still then, it wouldn't be updated enough for me to find that file I want to locate I reckon.

Finding files with locate is so much faster than with find when you know the file name, or large parts thereof, otherwise I tack on a | grep <something> to filter down the output.

If you don't use locate, and you use the apt package manager, then you can install it by:

sudo apt update && sudo apt upgrade -y && sudo apt install mlocate

Anyways, I made an alias, that took care of running sudo updatedb if locate didn't find the file, in the first place, and then ran locate afterwards, so now I know for sure that the file isn't there, or I may re-spell my search. With only one command, instead of three.

alias locate='suomynona() { locate $1 || sudo updatedb && locate $1 ; unset -f suomynona ; } ; suomynona '

And, I reckon this is within the scope of this subreddit, since we are talking about aliases, but by all means, feel free to reimplement it as a function. :)

And the weird function name, is named that way, so I don't undeclare somebody else's function.

Enjoy!

https://redd.it/109ymzz
@r_bash
"While loop" isn't l going through all variables

Starting to get my hands a little more dirty in bash.


I have a input file that contains a list of hosts that I would like to check if "/dump" is mounted.


Currently the list of host has 18 hosts. All are passwordless accessible.


So I pretty much want to ssh into a server and check to see if "/dump" is mounted or not.



#!/bin/bash

while read hostList; do
result=$(ssh "$hostList" 'mount | grep "/dump"')
if -z "$result" ; then
echo "$hostList: /dump is not mounted"
else
echo "$hostList: /mnt is mounted"
fi
done < hostList


I guess the problem is when I run this noscript sometimes it just comes back with 1 result, like it's only hitting the first hostname in the "hostList". Other times it comes back with the first 3 servers in the "hostList".


I can't seem to get it to hit all 18 servers in the "hostList".

https://redd.it/10a2ew9
@r_bash
Arithmetic Expressions

Hello,

I was hoping someone could explain to me what I am doing wrong here. I am a newbie at bash.

In hackerrank (website) there is a task to display a given expression rounded to 3 decimal places.

___________________

|Sample 1 input: |

\---------------------------

5+50*3/20 + (19*2)/7

\-------------------------------------------------------------------------------

\#my_code

\#!/bin/bash

read A

read B

read C

read D

read E

read F

x=$(( $A+$B * $C/$D + ($E**2) / $F ))

echo $x

&#x200B;

\------------------------------------------------------------------------------

The arithmetic works up until I add /F, then the red error marks occur.

If I try to run it I get the following error:

Solution.sh: line 10: A+B*C/D + (E**2) / F : division by 0 (error token is "D + (E**2) / F ")

I've taken the /F out and then the code runs. Is there an operator or syntax format I'm overlooking?

any additional help would be greatly appreciated.

https://redd.it/10agu8e
@r_bash
Why can't I pipe into the select command

Hello,

I want to make an user interactive selection within a pipe with

any_producing_commands|select menu_item in $(cat); do echo -n "${menu_item}"; break; done|any_consuming_commands

in between other commands.

Example

seq 0 4|select menu_item in $(cat); do echo -n "${menu_item}"; break; done|wc -c

But this breaks the pipe and just prints me the selection menu.

Can somebody tell me why and if possible provide me a solution?

I know this does work, but I want to be able to pipe parameters into the menu:

select menu_item in $(seq 0 4); do echo -n "${menu_item}"; break; done|wc -c

https://redd.it/10aq82w
@r_bash
ramfetch, now with the ascii logo having 2 notches.
https://redd.it/10b2jo2
@r_bash