r_bash – Telegram
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
[Help] Bash Array Question(s)

I'm parsing the content of a file or files. There will always be at least one (1), and up to four (4) of these files, ordered sequentially, e.g. **syno_mac_address1**, **syno_mac_address2**. The content of each file will always be a unique MAC address, e.g.: **001132623d3f**, **001132623d40**

Here's my approach to extracting these MAC addresses:

# GNU bash, version 4.4.23(1)-release (x86_64-pc-linux-gnu)

mac_address=( $(cat /proc/sys/kernel/syno_mac_address{1..4}))

and then printing these address:

for i in "${!mac_address[@]}"; do
printf '%s\n' "LAN $i ${mac_address[$i]}"
done

Here's sample output:

**LAN 0 001132623d3f**

**LAN 1 001132623d40**

This almost does what I'd like, except there's mismatch between the LAN number (the OS counts from `1`, **not** `0`). The desired output would be:

**LAN 1 001132623d3f**

**LAN 2 001132623d40**

But I'm not sure how to approach that. In addition, (ShellCheck)[shellcheck.net] complains about the way I declare the array, e.g.: **Prefer mapfile or read -a to split command output (or quote to avoid splitting)**, but the proposed solutions don't give me the output I expect.

I am also wondering if there's a more succinct way to handle this e.g.:

mac_address() {

MAC=$(cat /proc/sys/kernel/syno_mac_address{1..4})
printf "%s\n" "$MAC"
}

with some slight modification, e.g.: `printf '%s\n' "${mac_address[@]/LAN/ }"`. The problem I have there is that I am not sure how to add a count.

Thanks.

https://redd.it/100woy5
@r_bash
Howto measure uptime of hosts in local network (lightweight)?

Hi,I'm looking for a simple lightweight tool that can measure hosts uptime in hours/minutes per day in my local network (I guess ping can be used for that). I'm just looking for a simple report (HostA: X hours/minutes up on Monday, etc...) The remote hosts are windows so Im looking for a solution that doesn't require running a client on the host.
I need to get insight in uptime hours per day on a few hosts in my network.I'm planning to use my Raspberry PI (running Rasbian/pihole) for that.
I have Nagios running now but that's not really reporting what I'm looking for (it's just polling every now and then).
Could you please advise what to use? I discovered 'monitorr' on the web but that was written for windows.
Thanks in advance for your help

https://redd.it/1019dra
@r_bash
Script to delete specific files

I have a directory with the following files:

file1.txt
file1.txt.old
file2.txt.old
file3.txt
file3.txt.old
file4.txt.old

My goal is to write a noscript that deletes all *.old files, except if there is a non-old file with the same name. So it would keep file1.txt, file1.txt.old, file3.txt, file3.txt.old in this example. The filenames can contain spaces and letters from A-Z (with accents).

How can I achieve this with a bash noscript?

https://redd.it/1019c5d
@r_bash
find -exec executing on more files than find lists?

Edit: Of course as soon as I posted, I think I figured out I'm an idiot and that it's executing on the directories and I need to include -type f (or ! -type d would be better?), but would love any input from others anyway on the matter!

Will try to exclude extraneous details, but please let me know if more details would help. Basically, I have a folder structure where some files were changed recently. I'd like to archive just those newer files in a way that preserves the folder structure.

(This is because I already archived and compressed the original files, and figure it's easier just to have two archives side by side to avoid recompressing and reuploading.)

I am trying this out:
find . -ctime -2 -exec bash -c 'tar rvf archive.tar "{}"' \;


When I run find . -ctime -2 | wc -l, I get 978. However, running the above command, it archives over 1030 before I stop it, and it looks like it repeats some files.

Could anyone please help explain why it's not just archiving only the 978 newer files that I intend to archive? Alternatively, any better solution to achieve the same goal?

TIA!

https://redd.it/101l27c
@r_bash
cannot get bash to do case-insensitive completion

$ echo $BASH_VERSION
4.4.12(3)-release

$ shopt|grep case
nocaseglob on
nocasematch on

$ bind 'set completion-ignore-case on'

$ compgen -W 'foo Frank' f
foo

$

This is very frustrating. What is the point of having three separate options to ignore case if none of them work? Just to taunt the user?

How can I get bash to do case-insensitive completion?

https://redd.it/101p4vu
@r_bash