r_bash – Telegram
Command to open a noscript in a new terminal window

Hey everyone!

I'm fairly new to bash noscripting (currently studying for LPI).

we had an exercise to create two noscripts, one counts to 10K, the other one monitors the first noscript (with ps -aef) and if it's down, re-open it.

I did the second one like this:

*****

declare -i counter

counter=0

while $counter -lt 2 ; do

declare -i lines

lines=$(ps -aef |grep bex3.sh |wc -l)

if test $lines -lt 2; then

faildate=$(date)

<path>/bex3.sh

sleep 1

fi

done

*****

but it starts the noscript in the same terminal. while it's correct in terms of the homework I was wondering how I can write it to open the noscript in a new terminal window.


Thanks for any help :)

https://redd.it/zwjldf
@r_bash
Weird Behavior Running Script From cron

This has had me scratching my head all day. I have a simple noscript that cd's into a directory and uploads files via sftp. If I run it from the shell, everything runs just fine. But when running via cron it uploads some random txt files that don't even exist on the server.

I adding some logging to monitor what exact files get uploaded, and there's no mention of these files, yet every time the noscript runs via cron those random files appear on the remote server.

Here's the noscript:

-d /mnt/folder/ || exit
date >> log.txt
cd /mnt/folder/SFTP/
pwd >> log.txt
ls .txt&&for i in .txt;do gpg -e --sign -r "key" "$i"; echo "encypted $i" >> log.txt;done
ls .txt&&for i in .gpg; do lftp sftp://user@server -e "cd folder;put $i; bye"; echo "sent $i" >> log.txt; done
rm .gpg
mv
.txt Processed/

https://redd.it/zxef46
@r_bash
How can I make WAIT to run properly in this case?

I'm trying to wait for a couple of processes to finish before executing other commands, including apt update coming next in the queue, as follows:

pop-upgrade release upgrade & # Pop!OS tool checking for own OS updates
PID=$(pidof pop-upgrade)
wait $PID
apt update && apt full-upgrade -y

This *occasionally* returns this output from `apt`:

E: Could not get lock /var/lib/apt/lists/lock. It is held by process 1485 (packagekitd)
E: Unable to lock directory /var/lib/apt/lists/

To circumvent this, I sandwiched another `wait`:

pop-upgrade release upgrade & # Pop!
OS tool checking for own OS updates
PID=$(pidof pop-upgrade)
wait
$PID
PID=$(pidof packagekitd)
wait
$PID
apt update && apt full-upgrade -y

But now wait throws this error message preventing the system update:

wait: pid 1485 (meaning packagekitd) is not a child of this shell

The thing is I'm running these commands from a noscript as sudo. But don't know how to deal with parents and children thingy.

How can I make wait to do its job(s) properly? Or, maybe, how can I know which process(es) prevent apt work as it should?

Thank you!

https://redd.it/zxea6l
@r_bash
How to start with this problem

Hello guys

&#x200B;

So I have these 4 variables:

A=40

B=50

C=60

Z=100

If I would try to calculate/addition Variable A, B, and C together. Which combination would bring me to the nearest Value of Variable Z, without exceeding the value of Variable Z.

So the answer obviously would be A + C.

How would I do this in a bash noscript? I don't even know how to start because I don't understand the algorithm behind. What would you guys call this problem?

Thank you all!

https://redd.it/zxh3a0
@r_bash
Help with Bash Script

Here is what we currently have and the output:

Any advice would be amazing

Script:

u/echo off

if exist membership.txt del membership.txt

dsquery group -name *splk* >groups.txt

::The FOR command is one single line

for /f "tokens=*" %%g in (groups.txt) do u/echo %%g >>membership.txt && echo Members: >>membership.txt && dsget group %%g -members >>membership.txt && echo **************************************** >>membership.txt

Output

"CN=NERP-Splk-Admin,OU=splunk,OU=Security,OU=Group,DC=erp,DC=navy,DC=mil" 

Members: 

"CN=Name,OU=Users,OU=Accounts,DC=erp,DC=navy,DC=mil"

"CN=Name,OU=Users,OU=Accounts,DC=erp,DC=navy,DC=mil"

****************************************

"CN=NERP-SPLK-Citrix,OU=splunk,OU=Security,OU=Group,DC=erp,DC=navy,DC=mil" 

Members: 

"CN=Name,OU=Users,OU=Accounts,DC=erp,DC=navy,DC=mil"

"CN=Name,OU=Users,OU=Accounts,DC=erp,DC=navy,DC=mil"

****************************************

&#x200B;

What we want the output to look like:

"CN=NERP-Splk-Admin,OU=splunk,OU=Security,OU=Group,DC=erp,DC=navy,DC=mil"-"CN=Name,OU=Users,OU=Accounts,DC=erp,DC=navy,DC=mil"

"CN=NERP-Splk-Admin,OU=splunk,OU=Security,OU=Group,DC=erp,DC=navy,DC=mil"-"CN=Name,OU=Users,OU=Accounts,DC=erp,DC=navy,DC=mil"

"CN=NERP-SPLK-Citrix,OU=splunk,OU=Security,OU=Group,DC=erp,DC=navy,DC=mil"-"CN=Name ,OU=Users,OU=Accounts,DC=erp,DC=navy,DC=mil"

"CN=NERP-SPLK-Citrix,OU=splunk,OU=Security,OU=Group,DC=erp,DC=navy,DC=mil"-"CN=Name,OU=Users,OU=Accounts,DC=erp,DC=navy,DC=mil"

Etc.. we want the output on one line

https://redd.it/zxihr6
@r_bash
(noscript request) automate your server to send this message after a successful backup, or after service restart
https://redd.it/zy57yi
@r_bash
Comment-out (prefix with '#') every line in a file that is not already commented-out?

Hi everyone,

I am struggling for a couple of days with this one.
I cannot figure it out by myself and googling for hours on end did not help either.


What I am looking for:
There is a file, test.cfg for example.


It's contents:
lineone
\#linetwo
\#linethree
linefour

I want my noscript to do the following after executing it:
\#lineone
\#linetwo
\#linethree
\#linefour


Does anyone know a solution for this?
Thanks in advance

https://redd.it/zy8ztv
@r_bash
Why I keep getting a nonsense value?

In the following noscript `p` and `q` are at least 2048 bit long numbers, when multiplying both values (`p`\*`q`) the product in this case `n` keeps giving a smaller value than the expected, instead of getting a 4096 bit value it returns a value around 160 bit long.

#!/bin/bash

generate_random() {
hex=$(head -c 256 /dev/urandom | xxd -p -u | tr -d '\n')
bc <<< "ibase=16; $hex"
}

p="$(generate_random | sed -z 's=\\\n==g')"
q="$(generate_random | sed -z 's=\\\n==g')"

n=$((p * q))

echo "$n"

What is causing this? How can I fix it?

https://redd.it/zyb6xv
@r_bash
How can I tell when a forked process finishes?

Im trying to figure out a good and reliable way to fork processes off in a loop but limit the maximum number of "active" forked processes. right now I figure this out by keeping track of the number of started and completed forked processes (which means that every completed forked process needs to be accounted for, or else the count of currently active processes gets off). Currently, I use something like this:

p0=0 # number of started forked processes
p1=0 # number of finished forked processes
maxParallelThreads=$(nproc)

parFunc() {
# some function
sha256sum "$1"
}

# load inputs to parallelize over into array
mapfile -t inArgs < <(find ./ -type f)

for nn in "${inArgs@}"; do
parFunc "$nn" &
((p0++))
(( ( p0 - p1 ) >= maxParallelThreads )) && wait -nf && ((p1++))
done

The problem with this is that p1 (which counts the number of finished forked processes) only gets incremented if the process finishes while the wait -nf call is active. If it finishes in between wait -nf calls it doesnt get counted in p1, which makes (( p0 - p1 )) (which is the count of currently active forked processes) 1 higher than it should be. If you have a bunch of inputs this loop will slowly fork fewer and fewer processes simultaneously until it is only forking one at a time.

Ive tried a number of ways to increment p1 from within the forked process (modifying it normally, exporting it, etc.), but as best as I can tell bash wont allow this. That said, if anyone here knows how you can modify a variable in the parent shell from a forked process please do share.

The only thing Ive tried that seems like it might work is keeping p1 in a file (on a tmpfs), but im concerned about what would happen when multiple forked processes try to modify the file at the same time. I also could probably keep track on the pid's oof the started forked processes, and replace the wait -nf call with one that continually loops through checking that each process pid still exists until one of them doesnt, but that seems like a really inefficient and unsatisfying solution.

Any suggestions on how I might accomplish this? Thanks in advance.

https://redd.it/zypo1c
@r_bash
bashrc inspiration - your favorit trick

I'm looking for inspiration for my bashrc to imporve interactive shell experience.

Especially I'm interesetd in smart completion, history and navigation hacks - everything that makes your life more easy. Feel free to also post gits of otheres that you learned from.

https://redd.it/zyxihb
@r_bash
Trying to make a noscript that find if multiple packages are installed.

I'm not having any luck getting it to work.

I clearly have the packages on my system but dnf is saying there are no matches.

&#x200B;

Here is my noscript

https://preview.redd.it/0gtlgb51f29a1.png?width=1198&format=png&auto=webp&s=af0a166afbf586761cb28774d35e04ef2917997e

Here is my result

https://preview.redd.it/60m65a5af29a1.png?width=1196&format=png&auto=webp&s=67331b5872b423493c340262417987e261d33cd6

https://redd.it/zz5c87
@r_bash
Scripting and licensing - Do bash noscripts need a license?

I've noticed that some folks license their noscripts. Specifically, I recently found some licensed under the GPLv3.

This didn't make much sense to me, as the "Script" in question amounted to a number of bashisms, with common commands that could easily be run in the terminal.

I have no issue if someone wants to do that for convenvience, however, it's strange to me to license something that uses commands that everyone uses daily. I could understand licensing a complicated noscript with some functions and procedural steps, such as cloning a few git repos and building each. If something like that had some error correction and iteration, great.

Or if someone needed to combine custom regex, or parsing a file signature; maybe some complex math, etc.

What are your thoughts on licensing daily tasks in a noscript, as opposed to some original and specific code tailored to what might be a unqiue use case?

https://redd.it/zz61yl
@r_bash
How to use file for list of variables?

I've written noscript that checks when an item has come into stock and triggers a webhook to Discord to notify me. It does a CURL on the web page, GREPs for the 'Add to basket' button and uses the -c switch to return either 0 or 1 (for matched number of lines)

Presently I have the item URL defined as a variable at the top of the noscript, and for multiple items I just use multiple noscripts. I would like this to instead be called from a file with a list of URLs, one on each line. This would allow me to just echo https://itemurl.com >> urlfile to add a new item, and just use a single noscript for multiple items.

I am a newbie so go easy on me! Here is what I have at the moment, any advice appreciated

URL=https://www.com
DISCORDWEBHOOK=https://discord.com
STOCKCHECK=$(curl -s $URL | grep -c "Add to basket")

if "$STOCKCHECK" -eq "1" ; then
curl -X POST -H "Content-Type: application/json" -d '{"username":"Stock","content":"'$URL' IN STOCK - ORDER NOW"}' $DISCORDWEBHOOK;
fi

Thanks

https://redd.it/zz39y0
@r_bash
dyetide/dye - convert color codes from the terminal

Hi All!

I recently finished up a bash noscript project called dyetide/dye. dye is a simple utility that converts hex, rgb, and hsl color codes in the terminal. dyetide uses dye to search through a (css) file and convert said color codes throughout the whole file.

This was an educational project, I know there are better tools (python) out there that could have accomplished this. I just wanted to see what was possible with bash. It turned out...okay! Feel free to try it out. Constructive criticism and suggestions are always welcome, but this will probably be my last extensive bash noscript for a while, so don't expect much more work on this going forward (we'll see, I've come to really enjoy bash noscripting).

Special thanks are specified in the README, comments, and man pages. Thanks to all in r/bash that helped me hash out some of the details and made helpful suggestions along the way!

P.S. If you'd rather not get the noscript from github for some reason, here is the mirror repo on codeberg.

https://redd.it/zzlkdm
@r_bash
I need to change this noscript so that if nothing is selected it stops

I have this noscript that I use to get a list of pdfs and view them in dmenu or fzf and then open the selected in Evince. The problem is that its gonna open Evince even if I don't select a noscript. I don't know how to fix it, I know its bound to be simple but I couldn't find anything in my bash bible.
I know that : means do nothing in bash but I have no idea how to tell the noscript that if the user didn't select anything do :
Thank you for your time and any help is appreciated.

# List PDFs in given directory and opens selection in Evince

# Change to PDFs path
cd $HOME/Downloads/Documents/ || return

# List only the files with .pdf format
book=$(find . -name "*.pdf" | dmenu -p "Select Book")

# Open selected file in Evince
evince "$book" &

# Get terminal PPID and close terminal when the noscript is done
PPPID=$(awk '{print $4}' "/proc/$PPID/stat")
kill "$PPPID"


https://redd.it/10051kd
@r_bash
Anyone with bash get shopt -s lithist to work?

I'm running 4.4.12 under Cygwin (latest version as of this writing) and can't get lithist to work. cmdhist is also set.

Under Linux running bash 5.0.17 it works for that login session. If I log off then log back in, the multiline commands are broken out into different commands again.

https://redd.it/100jdbu
@r_bash
Standard or simplified way to pass all parameters of a function to a command

I have a few functions where I'd like to easily pass 0 or more arguments to a function and have those passed to a single command with quoting and escaping intact.

For example:

function useless()
{
less $*
}

But if I call:

useless 'this is first' 'this is second'

It effectively calls less with 6 arguments instead of two:

less this is first this is second

What I want is:

less 'this is first' 'this is second'

Of course I can write a loop in `useless()` to build the command line, but I'm wondering if there's something built-in I'm missing that would be simpler.

https://redd.it/100kev0
@r_bash