Help with function command format and execution
So I've been compiling a good sized list if bash functions and I've hit a bit of a snag.
I've made some functions for portability between a few systems I run.
So I set up a few firewall functions that work well.
However, I decided to create a function that could serve as a catch-all, so-to-speak.
The individual
The general idea is by running
Unfortunately, when I run
If anyone has an idea on how to get it to run properly, I would very much appreciate it.
At this point, since the individual functions work, I'm just looking to understand more about what it's doing versus what it's supposed to be doing.
​
​
function allowip-fwd() {
sudo firewall-cmd --zone=home --add-source="$1" --permanent
sudo firewall-cmd --reload
}
​
function blockip-fwd() {
sudo firewall-cmd --zone=home --remove-source="$1" --permanent
sudo firewall-cmd --reload
}
​
function allowport-fwd() {
sudo firewall-cmd --zone=home --add-port="$1"/tcp --permanent
sudo firewall-cmd --reload
}
​
function blockport-fwd() {
sudo firewall-cmd --zone=home --remove-port="$1"/tcp --permanent
sudo firewall-cmd --reload
}
​
function allowip-ufw() {
sudo ufw allow from "$1"
}
​
function blockip-ufw() {
sudo ufw delete allow from "$1"
}
​
function allowport-ufw() {
sudo ufw allow "$1"/tcp
}
​
function blockport-ufw() {
sudo ufw delete allow "$1"/tcp
}
​
function allowip-ipfw() {
sudo iptables -I INPUT -s "$1" -j ACCEPT
sudo iptables -A OUTPUT -d "$1" -j ACCEPT
}
​
function blockip-ipfw() {
sudo iptables -I INPUT -s "$1" -j DROP
sudo iptables -A OUTPUT -d "$1" -j DROP
}
​
function allowport-ipfw() {
sudo iptables -I INPUT -p tcp -m tcp --dport "$1" -j ACCEPT
sudo iptables -A OUTPUT -p tcp -m tcp --dport "$1" -j ACCEPT
}
​
function blockport-ipfw() {
sudo iptables -I INPUT -p tcp -m tcp --dport "$1" -j REJECT
sudo iptables -A OUTPUT -p tcp -m tcp --dport "$1" -j REJECT
}
​
function allowip() {
if ! -x "$(command -v firewall-cmd)" ; then
sudo firewall-cmd --zone=home --add-source="$1" --permanent
sudo firewall-cmd --reload
elif ! -x "$(command -v ufw)" ; then
sudo ufw allow from "$1"
elif ! -x "$(command -v iptables)" ; then
sudo iptables -I INPUT -s "$1" -j ACCEPT
sudo iptables -A OUTPUT -d "$1" -j ACCEPT
else
echo "Something is wrong with your firewall... exiting" >&2
fi
}
​
function blockip() {
if ! [ -x "$(command -v firewall-cmd)" \]; then
sudo firewall-cmd --zone=home --remove-source="$1" --permanent
sudo firewall-cmd --reload
elif ! [ -x "$(command -v ufw)" \]; then
sudo ufw delete allow from "$1"
elif ! [ -x "$(command -v iptables)" \]; then
sudo iptables -I
So I've been compiling a good sized list if bash functions and I've hit a bit of a snag.
I've made some functions for portability between a few systems I run.
So I set up a few firewall functions that work well.
However, I decided to create a function that could serve as a catch-all, so-to-speak.
The individual
*-{fwd,ufw,ipfw} functions run properly but when i try to run allowip 8000 it's not running as I had expected.The general idea is by running
$ allowport 8000 the function would check if firewall-cmd is installed then run those commands. If firewall-cmd isn't found check for ufw and run those. If neither are found, check for and run the iptables commands.Unfortunately, when I run
$ allowip 8000 the only output I get is sudo: ufw: command not found. This would be OK as I don't have ufw installed on my main system. Since I use firewalld on my main system (Fedora), the function should just run the first set of commands then exit but it doesn't. I've probably missed something obvious but I'm still learning.If anyone has an idea on how to get it to run properly, I would very much appreciate it.
At this point, since the individual functions work, I'm just looking to understand more about what it's doing versus what it's supposed to be doing.
​
​
function allowip-fwd() {
sudo firewall-cmd --zone=home --add-source="$1" --permanent
sudo firewall-cmd --reload
}
​
function blockip-fwd() {
sudo firewall-cmd --zone=home --remove-source="$1" --permanent
sudo firewall-cmd --reload
}
​
function allowport-fwd() {
sudo firewall-cmd --zone=home --add-port="$1"/tcp --permanent
sudo firewall-cmd --reload
}
​
function blockport-fwd() {
sudo firewall-cmd --zone=home --remove-port="$1"/tcp --permanent
sudo firewall-cmd --reload
}
​
function allowip-ufw() {
sudo ufw allow from "$1"
}
​
function blockip-ufw() {
sudo ufw delete allow from "$1"
}
​
function allowport-ufw() {
sudo ufw allow "$1"/tcp
}
​
function blockport-ufw() {
sudo ufw delete allow "$1"/tcp
}
​
function allowip-ipfw() {
sudo iptables -I INPUT -s "$1" -j ACCEPT
sudo iptables -A OUTPUT -d "$1" -j ACCEPT
}
​
function blockip-ipfw() {
sudo iptables -I INPUT -s "$1" -j DROP
sudo iptables -A OUTPUT -d "$1" -j DROP
}
​
function allowport-ipfw() {
sudo iptables -I INPUT -p tcp -m tcp --dport "$1" -j ACCEPT
sudo iptables -A OUTPUT -p tcp -m tcp --dport "$1" -j ACCEPT
}
​
function blockport-ipfw() {
sudo iptables -I INPUT -p tcp -m tcp --dport "$1" -j REJECT
sudo iptables -A OUTPUT -p tcp -m tcp --dport "$1" -j REJECT
}
​
function allowip() {
if ! -x "$(command -v firewall-cmd)" ; then
sudo firewall-cmd --zone=home --add-source="$1" --permanent
sudo firewall-cmd --reload
elif ! -x "$(command -v ufw)" ; then
sudo ufw allow from "$1"
elif ! -x "$(command -v iptables)" ; then
sudo iptables -I INPUT -s "$1" -j ACCEPT
sudo iptables -A OUTPUT -d "$1" -j ACCEPT
else
echo "Something is wrong with your firewall... exiting" >&2
fi
}
​
function blockip() {
if ! [ -x "$(command -v firewall-cmd)" \]; then
sudo firewall-cmd --zone=home --remove-source="$1" --permanent
sudo firewall-cmd --reload
elif ! [ -x "$(command -v ufw)" \]; then
sudo ufw delete allow from "$1"
elif ! [ -x "$(command -v iptables)" \]; then
sudo iptables -I
INPUT -s "$1" -j DROP
sudo iptables -A OUTPUT -d "$1" -j DROP
else
echo "Something is wrong with your firewall... exiting" >&2
fi
}
​
function allowport() {
if ! [ -x "$(command -v firewall-cmd)" \]; then
sudo firewall-cmd --zone=home --add-port="$1"/tcp --permanent
sudo firewall-cmd --reload
elif ! [ -x "$(command -v ufw)" \]; then
sudo ufw allow "$1"/tcp
elif ! [ -x "$(command -v iptables)" \]; then
sudo iptables -I INPUT -p tcp -m tcp --dport "$1" -j ACCEPT
sudo iptables -A OUTPUT -p tcp -m tcp --dport "$1" -j ACCEPT
else
echo "Something is wrong with your firewall... exiting" >&2
fi
}
​
function blockport() { # Function to cover most used firewalls on other distributions
if ! [ -x "$(command -v firewall-cmd)" \]; then # Check if firewall-cmd is installed
sudo firewall-cmd --zone=home --remove-port="$1"/tcp --permanent
sudo firewall-cmd --reload
elif ! [ -x "$(command -v ufw)" \]; then # If no firewall-cmd check for ufw
sudo ufw delete allow "$1"/tcp
elif ! [ -x "$(command -v iptables)" \]; then # If ufw unavailable, resort to iptables
sudo iptables -I INPUT -p tcp -m tcp --dport "$1" -j REJECT
sudo iptables -A OUTPUT -p tcp -m tcp --dport "$1" -j REJECT
else
echo "Something is wrong with your firewall... exiting" >&2 # If no Firewalld, ufw or iptables
exit
fi
}
https://redd.it/107larf
@r_bash
sudo iptables -A OUTPUT -d "$1" -j DROP
else
echo "Something is wrong with your firewall... exiting" >&2
fi
}
​
function allowport() {
if ! [ -x "$(command -v firewall-cmd)" \]; then
sudo firewall-cmd --zone=home --add-port="$1"/tcp --permanent
sudo firewall-cmd --reload
elif ! [ -x "$(command -v ufw)" \]; then
sudo ufw allow "$1"/tcp
elif ! [ -x "$(command -v iptables)" \]; then
sudo iptables -I INPUT -p tcp -m tcp --dport "$1" -j ACCEPT
sudo iptables -A OUTPUT -p tcp -m tcp --dport "$1" -j ACCEPT
else
echo "Something is wrong with your firewall... exiting" >&2
fi
}
​
function blockport() { # Function to cover most used firewalls on other distributions
if ! [ -x "$(command -v firewall-cmd)" \]; then # Check if firewall-cmd is installed
sudo firewall-cmd --zone=home --remove-port="$1"/tcp --permanent
sudo firewall-cmd --reload
elif ! [ -x "$(command -v ufw)" \]; then # If no firewall-cmd check for ufw
sudo ufw delete allow "$1"/tcp
elif ! [ -x "$(command -v iptables)" \]; then # If ufw unavailable, resort to iptables
sudo iptables -I INPUT -p tcp -m tcp --dport "$1" -j REJECT
sudo iptables -A OUTPUT -p tcp -m tcp --dport "$1" -j REJECT
else
echo "Something is wrong with your firewall... exiting" >&2 # If no Firewalld, ufw or iptables
exit
fi
}
https://redd.it/107larf
@r_bash
reddit
Help with function command format and execution
So I've been compiling a good sized list if bash functions and I've hit a bit of a snag. I've made some functions for portability between a few...
How to read a text file and delete files listed in the file?
I am using a noscript that makes a list of audio files in a directory, then it concatenates the files via ffmpeg into one big file, then I want it to read the text file again and delete all the component files that went into the big file. But I'm having trouble doing it because the noscript keeps breaking the lines of the text file into multiple strings, instead i want it to find the file listed and delete it.
Here is the noscript:
#!/bin/bash
read -p 'Type the noscript of the book here: ' noscript
read -p 'Type the extension of the book here: ' ext
for f in *.$ext; do echo "file '$f'" >> book_order.txt; done
ffmpeg -safe 0 -f concat -i book_order.txt -c:a copy $noscript.$ext && \
IFS='' && \
for f in `cat book_order.txt`; do rm -r `$f`; done && \
rm -f book_order.txt
I set IFS to blank because it was initially separating each line of the file by blank spaces. This makes it set $f to the full `file ./book-part1.mp3` line of the text file. I want the second for loop to have an array of the file commands and then execute the file command to get the file location and then delete it.
I know the IFS thing is wrong because now it doesn't have a delimiter at all. I know by default it uses blank spaces and newlines i thought initially i could use `IFS='\n'` but it just read that as `'n'` and later found that the for loop may just be stripping the new line altogether.
Does anyone know how I could achieve this?
https://redd.it/107mw17
@r_bash
I am using a noscript that makes a list of audio files in a directory, then it concatenates the files via ffmpeg into one big file, then I want it to read the text file again and delete all the component files that went into the big file. But I'm having trouble doing it because the noscript keeps breaking the lines of the text file into multiple strings, instead i want it to find the file listed and delete it.
Here is the noscript:
#!/bin/bash
read -p 'Type the noscript of the book here: ' noscript
read -p 'Type the extension of the book here: ' ext
for f in *.$ext; do echo "file '$f'" >> book_order.txt; done
ffmpeg -safe 0 -f concat -i book_order.txt -c:a copy $noscript.$ext && \
IFS='' && \
for f in `cat book_order.txt`; do rm -r `$f`; done && \
rm -f book_order.txt
I set IFS to blank because it was initially separating each line of the file by blank spaces. This makes it set $f to the full `file ./book-part1.mp3` line of the text file. I want the second for loop to have an array of the file commands and then execute the file command to get the file location and then delete it.
I know the IFS thing is wrong because now it doesn't have a delimiter at all. I know by default it uses blank spaces and newlines i thought initially i could use `IFS='\n'` but it just read that as `'n'` and later found that the for loop may just be stripping the new line altogether.
Does anyone know how I could achieve this?
https://redd.it/107mw17
@r_bash
reddit
How to read a text file and delete files listed in the file?
I am using a noscript that makes a list of audio files in a directory, then it concatenates the files via ffmpeg into one big file, then I want it...
Grep 2 strings to print missing items - what am I doing wrong??
tag1 (file) - just an example, these will be strings instead of numbers
1,2,3,4,5,6,7,8
​
tag2 (file)
3
4
7
​
GOAL: Print missing items from tag1 that SHOULD BE in tag2
​
egrep -v -f tag2 tag1 \# Returns no output - should be 1,2,5,6,8
​
I've tried doing this in bash with strings and files as well, getting frustrated :(
TIA
https://redd.it/107jn0o
@r_bash
tag1 (file) - just an example, these will be strings instead of numbers
1,2,3,4,5,6,7,8
​
tag2 (file)
3
4
7
​
GOAL: Print missing items from tag1 that SHOULD BE in tag2
​
egrep -v -f tag2 tag1 \# Returns no output - should be 1,2,5,6,8
​
I've tried doing this in bash with strings and files as well, getting frustrated :(
TIA
https://redd.it/107jn0o
@r_bash
reddit
Grep 2 strings to print missing items - what am I doing wrong??
tag1 (file) - just an example, these will be strings instead of numbers 1,2,3,4,5,6,7,8 tag2 (file) 3 4 7 GOAL: Print...
How to select the latest created directory among multiple directories?
Hi everyone!
A beginner at bash here, still trying to understand stuff. I have a noscript which runs a program in a docker container like
For file in subdirectory
Do
Docker run
/Path/to/directory/subdirectory
Done
However, the folder called "directory" has multiple subdirectories, named on numbers like
102011, 102345, etc
Now I want to always select 102345 (the largest number) in the run because it was created last. How can I add a chunk which specifies: (in all runs of my for loop)?
For file in subdirectory
Do
Docker run
/Path/to/directory/latest_subdirectory
Done
https://redd.it/107qjhd
@r_bash
Hi everyone!
A beginner at bash here, still trying to understand stuff. I have a noscript which runs a program in a docker container like
For file in subdirectory
Do
Docker run
/Path/to/directory/subdirectory
Done
However, the folder called "directory" has multiple subdirectories, named on numbers like
102011, 102345, etc
Now I want to always select 102345 (the largest number) in the run because it was created last. How can I add a chunk which specifies: (in all runs of my for loop)?
For file in subdirectory
Do
Docker run
/Path/to/directory/latest_subdirectory
Done
https://redd.it/107qjhd
@r_bash
reddit
How to select the latest created directory among multiple directories?
Hi everyone! A beginner at bash here, still trying to understand stuff. I have a noscript which runs a program in a docker container like For file...
Deleting folders greater than X days old?
I'm cobbling together a backup noscript to do a database export and copy that and a number of other files to our file server. Thus far, it's working exactly as intended (which is good), but however, since i don't want to overwrite everything on a daily basis, i am leaving every backup in a folder with a time stamped name, eg:
Each backup is 500MB, so while not huge, they will snowball pretty quickly. So now, I would like to start removing backups older than a few days old. Well, I'd like to preserve one backup per month, except I'd also like to keep the last 5 days of backups as well.
I can do the monthly backup just by running
Now, I need to delete the oldest backups.
It appears i have two choices:
find /path/ -mtime +14 -type d | xargs rm -rf;
Or there is package i've just learned about called
Figured i'd give the entire background.
Any thoughts appreciated.
(i'm doing backups this way, because it honestly be easier for my coworkers to do a recovery if they ever need to. I've created a document outlining what files go where, etc)
https://redd.it/107omg4
@r_bash
I'm cobbling together a backup noscript to do a database export and copy that and a number of other files to our file server. Thus far, it's working exactly as intended (which is good), but however, since i don't want to overwrite everything on a daily basis, i am leaving every backup in a folder with a time stamped name, eg:
/mnt/smb/backups/daily/2023-01-08 /mnt/smb/backups/daily/2023-01-07 /mnt/smb/backups/daily/2023-01-06 /mnt/smb/backups/daily/2023-01-05Each backup is 500MB, so while not huge, they will snowball pretty quickly. So now, I would like to start removing backups older than a few days old. Well, I'd like to preserve one backup per month, except I'd also like to keep the last 5 days of backups as well.
I can do the monthly backup just by running
cp *0101 /smb/backups/monthly/Now, I need to delete the oldest backups.
It appears i have two choices:
find /path/ -mtime +14 -type d | xargs rm -rf;
Or there is package i've just learned about called
tmpreaper which looks like it could be pretty simple, but which has a very long scary warning.Figured i'd give the entire background.
Any thoughts appreciated.
(i'm doing backups this way, because it honestly be easier for my coworkers to do a recovery if they ever need to. I've created a document outlining what files go where, etc)
https://redd.it/107omg4
@r_bash
reddit
Deleting folders greater than X days old?
I'm cobbling together a backup noscript to do a database export and copy that and a number of other files to our file server. Thus far, it's working...
pkill -u $username vs pkill -9 -u $username, difference and flag explanation?
We have this code so that we close all processes of a user and log them out before changing name on them.
pkill -u $unam 2> /dev/null
pkill -9 -u $unam 2> /dev/null
usermod -l $newnam $unam
We're not 100% about the details, though.
What does "pkill -u jackiie" do, or more specifically, what does the -u flag do? Does it kill all the processes of the effective user ID of "jackiie"?
And "pkill -9 -u jackiie", does this kill the user itself, so that it forces a "log out"? Is the -u flag needed?
https://redd.it/107imam
@r_bash
We have this code so that we close all processes of a user and log them out before changing name on them.
pkill -u $unam 2> /dev/null
pkill -9 -u $unam 2> /dev/null
usermod -l $newnam $unam
We're not 100% about the details, though.
What does "pkill -u jackiie" do, or more specifically, what does the -u flag do? Does it kill all the processes of the effective user ID of "jackiie"?
And "pkill -9 -u jackiie", does this kill the user itself, so that it forces a "log out"? Is the -u flag needed?
https://redd.it/107imam
@r_bash
reddit
pkill -u $username vs pkill -9 -u $username, difference and flag...
We have this code so that we close all processes of a user and log them out before changing name on them. pkill -u $unam 2> /dev/null ...
i=0; ((i++)); echo "${i}${?}" # why 11 not 10?!!
Hi all!
I cannot understand why $? is set to error in the line below
i=0; ((i++)); echo "${i}${?}"
>11
But if I init with 1 there is no error:
i=1; ((i++)); echo "${i}${?}"
>20
Can you explain it?
https://redd.it/1086qqx
@r_bash
Hi all!
I cannot understand why $? is set to error in the line below
i=0; ((i++)); echo "${i}${?}"
>11
But if I init with 1 there is no error:
i=1; ((i++)); echo "${i}${?}"
>20
Can you explain it?
https://redd.it/1086qqx
@r_bash
reddit
i=0; ((i++)); echo "${i}${?}" # why 11 not 10?!!
Hi all! I cannot understand why $? is set to error in the line below i=0; ((i++)); echo "${i}${?}" >11 But if I init with 1 there is no...
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 $snapshotid ; 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
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 $snapshotid ; 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
reddit
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: snapshot_id=$(head -n...
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
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
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
reddit
Script can handle spaces in file names when run without sudo, but...
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...
I wrote an MVP testing library in Bash to make it as easy as possible to add test suites for commandline-driven tools
https://github.com/pmarreck/tinytestlib
https://redd.it/108ewcp
@r_bash
https://github.com/pmarreck/tinytestlib
https://redd.it/108ewcp
@r_bash
GitHub
GitHub - pmarreck/tinytestlib: An MVP shell noscript testing library (currently bash but may include more shells in future) allowing…
An MVP shell noscript testing library (currently bash but may include more shells in future) allowing you to assert on stdout, stderr and return codes in your shell noscript test suites. - GitHub - pma...
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
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
​
​
Output from above:
TEMP 1 - 1
TEMP 2 - 2
TEMP 3 - 3
TEMP 4 - 4
​
​
DESIRED OUTPUT:
TEMP 1 - ONE
TEMP 2 - TWO
TEMP 3 - THREE
TEMP 4 - FOUR
https://redd.it/108vt9o
@r_bash
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
​
​
Output from above:
TEMP 1 - 1
TEMP 2 - 2
TEMP 3 - 3
TEMP 4 - 4
​
​
DESIRED OUTPUT:
TEMP 1 - ONE
TEMP 2 - TWO
TEMP 3 - THREE
TEMP 4 - FOUR
https://redd.it/108vt9o
@r_bash
reddit
Bash help, terrible with linux, not sure what the name of this...
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...
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
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
reddit
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...
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
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
reddit
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'...
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
it successfully replace the string with the new string.
But when i try to do
It simply prints the help docs without replacing the string.
What am I doing wrong?
https://redd.it/1093da8
@r_bash
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
reddit
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...
Trouble generating big random hexadecimal numbers
I want to generate a random number from 2 to
#!/bin/bash
generaterandom() {
head -c 256 /dev/urandom | xxd -p -u -c 256 | tr -d '[:space:]\\'
}
p="$(generaterandom)"
q="$(generaterandom)"
n=$(echo "obase=16;ibase=16; ${p} * ${q}" | bc | tr -d '[:space:]\\')
witnesslimit=$(echo "obase=16;ibase=16; ${n} - 2" | bc | tr -d ':space:\\')
https://redd.it/10978v3
@r_bash
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="$(generaterandom)"
q="$(generaterandom)"
n=$(echo "obase=16;ibase=16; ${p} * ${q}" | bc | tr -d '[:space:]\\')
witnesslimit=$(echo "obase=16;ibase=16; ${n} - 2" | bc | tr -d ':space:\\')
https://redd.it/10978v3
@r_bash
reddit
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 %...
What happens when you open a terminal and enter ‘ls’
https://www.warp.dev/blog/what-happens-when-you-open-a-terminal-and-enter-ls
https://redd.it/109czo4
@r_bash
https://www.warp.dev/blog/what-happens-when-you-open-a-terminal-and-enter-ls
https://redd.it/109czo4
@r_bash
Warp
What happens when you open a terminal emulator and enter “ls” | Warp
This article explores how terminal emulators function, decoding the process of sending commands to the shell for execution. Unveil the mystery behind what happens when you open a terminal and enter Is.
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.
​
https://github.com/ohmybahgosh/YT-DLP-SCRIPTS/tree/main/DISCOVERY-PLUS-YTDLP
https://redd.it/109g561
@r_bash
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.
​
https://github.com/ohmybahgosh/YT-DLP-SCRIPTS/tree/main/DISCOVERY-PLUS-YTDLP
https://redd.it/109g561
@r_bash
GitHub
YT-DLP-SCRIPTS/DISCOVERY-PLUS-YTDLP at main · ohmybahgosh/YT-DLP-SCRIPTS
...Just a place for me to share my various YT-DLP & related bash noscripts. - ohmybahgosh/YT-DLP-SCRIPTS
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
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
reddit
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...
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
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
reddit
Percentage in file name breaks noscript when assigning output of...
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...