r_bash – Telegram
How can I avoid this `-bash: /usr/bin/<functionName>: Argument list too long` error?

I have a file in my $PATH (at, say, `/usr/bin/functionName`) that looks like the following:

#!/bin/bash

functionName() {
#do stuff
}

(( $# > 0 )) && functionName "${@}"

When I pass it (admittedly large) inputs it throws an error:

functionName "$largeInput1" "$largeInput2"
-bash: /usr/bin/<functionName>: Argument list too long

Interestingly, if I source the function first it runs just fine

source "$(which functionName)"
functionName "$largeInput1" "$largeInput2"

<returns expected output>

I know that I could just source the noscript in my `.bashrc` file, but (if possible) Id rather not, since then I have to do that for all my custom functions that might recieve large inputs at some point (so, basically all of them). And, honestly, "putting them in my $PATH and having them just work" seems like a better solution.

Un-wrapping the code from a function *might* work too, but again im not crazy about this.

Best solution I have so far is to add something like this to my .bashrc:

trapfun() { which "${*%% *}" | grep -q -E '^.+$' && source "$(which "${*%% *}")" && "${@}"; }
trap 'trapfun "${BASH_COMMAND}"' ERR

Which works in some simple testing but seems fragile. Id feel a bit better about it with some additional checks before trying to source and re-run the command (e.g., if I could get the trap to fetch the error message text and make sure it contains `-bash: * Argument list too long`, perhaps a check with `file` to ensure that the file returned by `which <functionNAme>` is a sourceable shell noscript, etc.)

Any suggestions?

***

Heres an actual example with code and inputs

#/usr/bin/linecat

#!/bin/bash
linecat () {
(( ${#} < 2 )) && echo "${@}" && return;

local -a A;
local -a B;

mapfile -t B < <(echo "${2}");
mapfile -t A < <(echo "${1}");

(( ${#A[@]} < ${#B[@]} )) && { local -a Aadd; mapfile -t Aadd < <(printf '%0.0s\n' "${B[@]:${#A[@]}}"); A+=("${Aadd[@]}"); }

linecat "$(printf "$(printf '%s %%s\n' "${A[@]}")" "${B[@]}")" "${@:3}"
}
(( $# > 0 )) && linecat "${@}"
# commands

a="$(dd if=/dev/urandom bs=64k count=4|hexdump)"
b="$(dd if=/dev/urandom bs=64k count=5|hexdump)"

linecat "$a" "$b"

# THROWS ERROR

source "$(which linecat)"
linecat "$a" "$b"

# RETURNS CORRECT OUTPUT

https://redd.it/zptkru
@r_bash
What can I do if I constantly get memory garbage and all the datas are deleted?

Hi


First of all: Sorry for my lack of programming knowledge and my English as well.


Before I wanted to write down the question I wanted to check how my code works. The point is that its on a remote computer, so I've checked in via ssh username and my password. I've used to cd command to navigate me into a repository where my certain files are.


In this repository there are a .txt file called "surname.txt", where there are different strings and they're from each other with a new line and one code which is called rand1.cpp and reads from the previous .txt file. It looks like this: https://codeshare.io/Rb1Ze3


I've used the g++ complier in order to build the code, and the "./.out" in order to run the program, but it always wrote memory garbage. I've checked my surname.txt file and all the datas have been dissappeared. I remember that was one of those parts of my code which is actually worked... Nevermind. So I've rewrote it. And nothing.


I use nano in order to edit a file. I didn't touch it, I've just checked that all the strings are there. (Yes, they were.) After that I've used the g++ again and checked the .txt second time that all the strings are there. (Yes, they were.) I've run the code and again... Memory garbage. I did this multiple times.


Please help. What have I done wrong?

https://redd.it/zpxda7
@r_bash
Rookie question Getting the noscript report.

Hello community!

&#x200B;

I wonder if there is any possibility to make a statement in the bash noscript which create a report of recent changes in it and for example send a notification via e-mail? Is this even possible to do in bash, without any external plugins?

https://redd.it/zpx29b
@r_bash
How to enforce stdout=PIPE in bash without cat?

Sometimes I want to ensure, that stdout is a pipe as this may change the behavior of a program in a wanted way. E.g. more has a nice side effect when it sends output to a pipe instead of a terminal: It adds the file noscript - embedded in lines with colons - to the output. Hence a command like the following is a quick hack to generate an overview in the output:

more /etc/cron.d/* | cat

I‘m just curious: Does anybody knows another - easy to type - method, how to trigger this output mode of more?

https://redd.it/zq1gn4
@r_bash
I have a programming question :)

Hey fellas,

I'm on the quest to kind of automate my semester in terms of checking due dates and such things of that nature. Is there a way that I could grab the due dates that are posted to the website that holds my assignments and stuff? Like, for example:

&#x200B;

I would like to have a noscript that runs with cron every so often (That part I know how to do) and have it send me a desktop notification saying y'know.. something like "You have a math final due..... blah blah".

I know the wget command does things with websites and HTML and all that good stuff, but is there a way to do what I wanna execute within the command line?

&#x200B;

Ps: I am doing this strictly for fun :D because I have a newfound love for the linux command line because of its flexibility

so any fun ways to do this, please let me know!

https://redd.it/zq3ugq
@r_bash
pass (password manager) autocompletion for items

I would ask question about pass. Is it a way to autocomplete in the bash in that way do not type parent directory name?

For example structure is

\- directory
   \- password1
   \- password2

If I want password1 pass I have to type firstly the parent directory name.

Basically I can't type pass TAB and get password1 or password2 suggestions.

For now I'm using noscript providing by pass developers https://git.zx2c4.com/password-store/tree/src/completion/pass.bash-completion

https://redd.it/zq3tim
@r_bash
How to do a noscript check with adb devices

Hi, I'm trying to figure out a good way to add a check in my noscript with adb devices to see if any Android device exists before running my noscript further with more adb commands.

Currently, When I try a check like this:

   if ! adb devices; then
...
exit
fi


But it doesn't work that way. It's not designed very noscripting friendly. It doesn't even output a different ERRORLEVEL if a device is attached or not, it just outputs a string like "List of devices attached\n" then exits.

I already have a feeling piping into sed is needed here, so then I can test with [ -z $(CMD | sed "blah") ]. but I am TERRIBLE at figuring out the right sed lines to do this. So if anyone can help, that would be great.

I am actually using dash (Debian shell), so I'd prefer something POSIX sh compliant, but if I have to use bash, that's fine. IDK if there's a Subreddit for POSIX sh noscripting, but would like to know. :)

https://redd.it/zqb2xw
@r_bash
How do you put the content of a folder inside a tar file?

I tried:

&#x200B;

tar -cvzf a.tar.gz a

&#x200B;

and it didn't work. Is there a way to put them inside a tar.gz file without compressing the files? I am not sure what's wrong but my filereader doesn't work when I read the tar.gz file even if the content is the same. I am basically creating a tar.gz file and then extracting the content and then running the bash command with the extracted content to see if my filereader can still read the file, but apparently the files aren't the same and my filereader can't read the created tar, even if it can read the first tar generated through my app.

https://redd.it/zqdhm8
@r_bash
Does a Gruvbox theme exist for Bash?

Looking for a gruvbox theme for bash. And maybe konsole, as I use plasma.

https://redd.it/zqimzm
@r_bash
get user input and add it to an ffmpeg screen recording

I have the following noscript added as aliases in bash

today=`date '+%d_%m_%Y-%H_%M_%S'`;
filename="/tmp/$today.mp4"
alias srec='ffmpeg -f x11grab -video_size 1600x900 -framerate 25 -i $DISPLAY -f alsa -i default -c:v libx264 -preset ultrafast -c:a aac ${filename}'

it records the screen and saves the file to /tmp with todays date.

how can I make it ask for input and it after the date and before the file extension of the video

example:

20-12-2022-{video}.mkv

https://redd.it/zqj63a
@r_bash
curl not finding the file

I made this short noscript

pic_name= "$HOME/Screenshots/$(date +%s).png"

maim -s $pic_name

url= $(curl -F'file=@$pic_name' https://0x0.st)

echo $url | xclip -sel clip

dunstify "File Uploaded" "URL: $url \ncopied to clipboard" -t 5000 -i "$HOME/.config/dunst/images/success.png"

but it's not pushing the screenshot to 0x0 any clue?

https://redd.it/zqk5ax
@r_bash
When using echo, both ${pwd} and $pwd work, does it matter which you use?

They both seem to be working. I know the {} is used for arrays but I want to ask whether there is a reason to use one over the other when echoing.

https://redd.it/zr2552
@r_bash
rgb/hex converter syntax - how does this work?

Hi All.

I'm preparing a wrapper noscript around these snippets I found that convert rgb to hex and hex to rgb. I eventually want to expand it so that it also converts the alpha channel, but I don't quite understand the syntax. Could someone please break down how these following two functions work:

&#x200B;

hextorgb() {
: "${1/\#/}"
((r = 16#${:0:2}, g = 16#${:2:2}, b = 16#${:4:2}))
printf '%s\n' "rgb($r, $g, $b)"
}

rgb
tohex() {
printf '#%02x%02x%02x\n' "$1" "$2" "$3"
}


Any insights into what is happening here, particularly in hex\
to_rgb(), would be

greatly appreciated. Thanks in advance.

https://redd.it/zqmvz8
@r_bash
Guidance with homework

I am a beginner and would like some help with an exercise:

Generate 100 files containing one random number each. Scan the files and find the 5 files that have the highest numbers and the 5 files that have the lowest numbers. Write the numbers you receive in a "list.txt" file.

I have already completed the beginning of generating the files.

for x in $(seq 1 100)

do

shuf -i 1-1000 -n 1 -o $x.txt

done

I am uncertain of how to sort the 100 files by what's actually written inside each of the files. This is a written example of how I imagined I could do the rest of the exercise, but I don't actually understand how to put it all together:

for x in $(seq 1 5)

do

for x in $(seq 1 100)

do

#find largest number in files out of the directory

#find lowest number in files out of the directory

#move both of the numbers to list.txt

#remove both of the files out of the directory

#repeat the process by moving and removing the files

done

done

Would this work? Do I need to use head and tail to find the needed values? Sorry if this isn't enough info.

https://redd.it/zrg7o2
@r_bash
Dead-simple command-line options for small noscripts. Specify all your options on a SINGLE line. Live option values are available as environment and shell variables.
https://gist.github.com/fareedst/8979cc7bd9c811ecd450b9be6d511cdf

https://redd.it/zrxzmd
@r_bash
remove all files that contain numerical string in filename?

Is there an ideal way to remove all files in a directory that are within a certain numerical range?

I have tried using both find and rm but the issue i run into is that the numbers are seperated by periods in the file name so i can't do something like:

rm {5..2}

I know I can just run multiple commands like:

find -type f -name '.6' -delete

but find this inefficient!

https://redd.it/zskhq6
@r_bash
I don't understand how bash -c and sh -c work in regard to keybinding

Hello everybody, I have a little noscript that I launch from the terminal to close my current Firefox session open my VPN connection, kill ipv6, so it doesn't rat out my real IP and then start a new FF instance in my VPN profile.

I wanted to make my life a little easier and bind the noscript to a key, and I accidentally felt into a rabbit hole.

The problem is that killing off ipv6 requires sudo permission. So after I press the keybinding (let's say super +v) I want to be prompted by a GUI asking me for my sudoer password.

This is my_noscript that I launch from the terminal, and it works fine, after I call it from the command line I get prompted for my password inside the terminal:

\#!/bin/bash

&#x200B;

>pkill -f firefox
>
>sudo sysctl -w net.ipv6.conf.all.disable_ipv6=1
>
>sudo sysctl -w net.ipv6.conf.default.disable_ipv6=1
>
>sudo sysctl -w net.ipv6.conf.lo.disable_ipv6=1
>
>nmcli con up id " ......"
>
>firefox --detach -P "my_vpn" &
>
>pkill terminator

terminator is my terminal emulator.

I tried to just associate this noscript to a key, but obviously I don't get prompted for my sudoer password. So after a day of searching online, I still can't make it work.

I tried associating the super+v to the following command : sh -c "pkexec new_noscript"

where the new_noscript is:

>\#!/bin/bash
>
>pkill -f firefox
>
>sysctl -w net.ipv6.conf.all.disable_ipv6=1
>
>sysctl -w net.ipv6.conf.default.disable_ipv6=1
>
>sysctl -w net.ipv6.conf.lo.disable_ipv6=1
>
>sudo -u "my_username" nmcli con up id "..."
>
>/usr/bin/firefox --display="127.0.0.1:10.0" -P "my_vpn"

So I get a GUI prompt asking me for my sudoer password, ipv6 is turned off, my VPN is launched (well at first it was complaining about no VPN secrets, which sounded interesting because I kind of like secrets, but in the end I figured it was just a problem that I was launching nmcli through the root account hence the sudo -u to go back to my normal user account), but Firefox won't launch. The noscript works when started from an interactive bash shell but not as a keybinding, so I guess some x11 variable isn't being passed on.

I tried export DISPLAY=localhost:0.0 before the Firefox line, but I'm just guessing here, I don't really understand what is going on. I would also like in the future to make the noscript more robust, by using ufw as a kill switch.

Can somebody please explain why I can't use bash -c "pkexec command" when associating a keybinding, but I can with sh -c. The OS is Linux Mint cinnamon DE (fork of gnome).

And what exactly does the -c option does. I do not understand it. The bash manual starts with it, really depressing stuff. From a historical point, what exactly does that "c" stand for?

https://redd.it/zspdv5
@r_bash
my noscript to preview all images in a directory (any suggestions?)

Hello,

I have this short noscript that can provide a preview of all images in a directory:

#

for file in *

do

if [[ $file =~ \.jpg$|\.jpeg$|\.png$|\.gif$ ]]

then

kitty +kitten icat --align="left" --background="#1e1e2e" --loop="-1" "$file"

echo "$file"

fi

https://redd.it/zsqwei
@r_bash