r_bash – Telegram
arguments with spaces to a noscript, run from another noscript

So here is an example with a simple noscript that just prints out its first, second and third argument.

Works as intended with both single and space-embedded arguments

~/tmp$ cat args.sh
#!/usr/bin/env bash                                                                 
echo "1: $1"
echo "2: $2"
echo "3: $3"

~/tmp$ ./args.sh a b c
1: a
2: b
3: c

~/tmp$ ./args.sh a 'b b' c
1: a
2: b b
3: c

But now if i run this noscript from another noscript that uses a variable to pass the arguments, then the quotations dont work.

How can i get this working so that "b b" is understood as one single argument?
In reality these arguments are fetched from a text-file, but I tried to simplify as much as possible here.

~/tmp$ cat wrapper.sh 
#!/usr/bin/env bash
args="a 'b b' c"
./args.sh $args

~/tmp$ ./wrapper.sh 
1: a
2: 'b
3: b'

https://redd.it/1hxzsgy
@r_bash
Bash unpredictability

Does anyone know why Bash works the way it does? Why are there so many ways to do a particular thing, with most only yielding partially successful results and, say, one out of seven giving the result you're looking for?

https://redd.it/1hy0qvf
@r_bash
How to Display Dynamic Menu Under Active Command Line Input in Bash Terminal?

I want to write a Bash noscript that implements a menu which updates in real-time directly beneath the active command line as the user types. Like what you see here with ble.sh , where the user was able to select "tmux" from options displayed below the line they were typing on.

I'm still a beginner, so I wanted to know if this is something feasible for me right now, or if it's more complicated than it appears. If it is feasible, how can I get started?

https://redd.it/1hxz61b
@r_bash
Does rbash disable functions?

I've built a sandbox that restricts the user to the rbash shell. But what I've found was that the user was still able to execute functions which can be bad for the environment because it enables the use of a fork bomb:

:(){ :|:& };:

#

I don't want to set a process limit for the user. I would like to just disable the user from declaring and executing functions.

https://redd.it/1hy2mnw
@r_bash
Reading when user enters a response without hitting enter

I have this:

cat <<EOF
Press x
EOF

read response

if [ $response == 'x' ]; then
printf "you did it!"

else
printf "dummy"
fi

This requires the user to press x [Enter], though.

How do I get it to listen and respond immediately after they press x?

https://redd.it/1hyqv8v
@r_bash
Something i do on all BASH noscripts I write. What do you guys think?
https://redd.it/1hz2kku
@r_bash
A noscript for renaming movie files

Most of the time, when you get a movie file it's a directory containing the video file, maybe some subnoscripts, and a bunch of other junk files. The names of the files are usually crowded and unreadable. I used to rename them all myself, but I got tired of it, so I learned how to write shell noscripts.

stripper.sh is really useful tool, and it has saved me a huge amount of work over the last few years. It is designed to operate on a directory containing one or many subdirectories, each one containing a different movie. It formats the names of the subdirectories and the files in them and deletes extra junk files. This noscript is dependent on "rename," which is really worth getting, it's another huge time saver.

It has four options which can be used individually or together:

1. Option p: Convert periods and underscores to spaces
2. Option t: Trim directory names after noscript and year
3. Option s: Search and remove a pattern/string from directory and file names
4. Option m: Match file names to the names of their parent directories
5. No option or any other letter entered: Shows the user guide.

Here is an example working directory before running stripper.sh:

Cold.Blue.Steel.1988.1080p.s3cr3t.0ri0le.6xVHAYT
↳Cold.Blue.Steel.1988.1080p.s3cr3t.0ri0le.6xVHAYT.mkv
poster.JPG
english.srt
info.nfo
other torrents.txt

Angel Feather 1996 720pan0rtymous2200
↳Angel Feather 1996 720pan0rtymous2200.mp4
english SDH.srt
screenshot128620.png
screenshot186855.png
screenshot209723.png
readme.txt
susfile.exe

...and after running stripper.sh \-ptm:

Cold Blue Steel (1988)
↳Cold Blue Steel (1988).mkv
Cold Blue Steel (1988).eng.srt

Angel Feather (1996)
↳Angel Feather (1996).mp4
Angel Feather (1996).eng.srt

It's not perfect, there are some limitations, mainly if there are sub-subdirectories. Sometimes there are, with subnoscript files or screenshots. The noscript does not handle those, but it does not delete them either.

Here is the code: (I'm sorry if the indents are screwed up, reddit removed them from one of the sections, don't ask me why)

#!/bin/bash

OPT=$1

#----------------Show user guide

if -z "$OPT" || `echo "$OPT" | grep -Ev [ptsm ]
then
echo -e "\033[38;5;138m\033[1mUSAGE: \033[0m"
echo -e "\t\033[38;5;138m\033[1mstripper.sh\033[0m [\033[4mOPTIONS\033[0m]\n"
echo -e "\033[38;5;138m\033[1mOPTIONS\033[0m"
echo -e "\tPick one or more, no spaces between. Operations take place in the order below."
echo -e "\n\t\033[38;5;138m\033[1mp\033[0m\tConvert periods and underscores to spaces in file and directory names."
echo -e "\n\t\033[38;5;138m\033[1ms\033[0m\tSearch and remove pattern from file and directory names."
echo -e "\n\t\033[38;5;138m\033[1mt\033[0m\tTrim directory names after noscript and year."
echo -e "\n\t\033[38;5;138m\033[1mm\033[0m\tMatch filenames to parent directory names.\n"

exit 0
fi

#-----------------Make periods and underscores into spaces

if echo "$OPT" | grep -q 'p'
then
echo -n "Converting underscores and periods to spaces... "

for j in *
do

if [ -d "$j" ]
then
rename -E 's/\_/\ /g' -E 's/\./\ /g' "$j"
elif [ -f "$j" ]
then
rename -E 's/\_/\ /g' -E 's/\./\ /g' -E 's/ (...)$/.$1/' "$j"
fi

done

echo "done"
fi

#---------------Search and destroy

if echo "$OPT" | grep -q 's'
then
echo "Remove search pattern from filenames:"
echo "Show file/directory list? y/n"
read CHOICE

if [ "$CHOICE" = "y" ]
then
echo
ls -1
echo
fi

echo "Enter pattern to be removed from filenames: "
IFS=
read SPATT
echo -n "Removing pattern \"$SPATT\"... "
SPATT=
echo "$SPATT" | sed -e
A noscript for renaming movie files

Most of the time, when you get a movie file it's a directory containing the video file, maybe some subnoscripts, and a bunch of other junk files. The names of the files are usually crowded and unreadable. I used to rename them all myself, but I got tired of it, so I learned how to write shell noscripts.

[stripper.sh](http://stripper.sh) is really useful tool, and it has saved me a huge amount of work over the last few years. It is designed to operate on a directory containing one or many subdirectories, each one containing a different movie. It formats the names of the subdirectories and the files in them and deletes extra junk files. This noscript is dependent on "rename," which is really worth getting, it's another huge time saver.

It has four options which can be used individually or together:

1. Option p: Convert periods and underscores to spaces
2. Option t: Trim directory names after noscript and year
3. Option s: Search and remove a pattern/string from directory and file names
4. Option m: Match file names to the names of their parent directories
5. No option or any other letter entered: Shows the user guide.

Here is an example working directory before running stripper.sh:

Cold.Blue.Steel.1988.1080p.s3cr3t.0ri0le.6xV_HAYT_
↳Cold.Blue.Steel.1988.1080p.s3cr3t.0ri0le.6xV_HAYT_.mkv
poster.JPG
english.srt
info.nfo
other torrents.txt

Angel Feather [1996] 720p_an0rtymous_2200
↳Angel Feather [1996] 720p_an0rtymous_2200.mp4
english [SDH].srt
screenshot128620.png
screenshot186855.png
screenshot209723.png
readme.txt
susfile.exe

...and after running [stripper.sh](http://stripper.sh) \-ptm:

Cold Blue Steel (1988)
↳Cold Blue Steel (1988).mkv
Cold Blue Steel (1988).eng.srt

Angel Feather (1996)
↳Angel Feather (1996).mp4
Angel Feather (1996).eng.srt

It's not perfect, there are some limitations, mainly if there are sub-subdirectories. Sometimes there are, with subnoscript files or screenshots. The noscript does not handle those, but it does not delete them either.

Here is the code: (I'm sorry if the indents are screwed up, reddit removed them from one of the sections, don't ask me why)

#!/bin/bash

OPT=$1

#----------------Show user guide

if [ -z "$OPT" ] || [ `echo "$OPT" | grep -Ev [ptsm]` ]
then
echo -e "\033[38;5;138m\033[1mUSAGE: \033[0m"
echo -e "\t\033[38;5;138m\033[1mstripper.sh\033[0m [\033[4mOPTIONS\033[0m]\n"
echo -e "\033[38;5;138m\033[1mOPTIONS\033[0m"
echo -e "\tPick one or more, no spaces between. Operations take place in the order below."
echo -e "\n\t\033[38;5;138m\033[1mp\033[0m\tConvert periods and underscores to spaces in file and directory names."
echo -e "\n\t\033[38;5;138m\033[1ms\033[0m\tSearch and remove pattern from file and directory names."
echo -e "\n\t\033[38;5;138m\033[1mt\033[0m\tTrim directory names after noscript and year."
echo -e "\n\t\033[38;5;138m\033[1mm\033[0m\tMatch filenames to parent directory names.\n"

exit 0
fi

#-----------------Make periods and underscores into spaces

if echo "$OPT" | grep -q 'p'
then
echo -n "Converting underscores and periods to spaces... "

for j in *
do

if [ -d "$j" ]
then
rename -E 's/\_/\ /g' -E 's/\./\ /g' "$j"
elif [ -f "$j" ]
then
rename -E 's/\_/\ /g' -E 's/\./\ /g' -E 's/ (...)$/.$1/' "$j"
fi

done

echo "done"
fi

#---------------Search and destroy

if echo "$OPT" | grep -q 's'
then
echo "Remove search pattern from filenames:"
echo "Show file/directory list? y/n"
read CHOICE

if [ "$CHOICE" = "y" ]
then
echo
ls -1
echo
fi

echo "Enter pattern to be removed from filenames: "
IFS=
read SPATT
echo -n "Removing pattern \"$SPATT\"... "
SPATT=`echo "$SPATT" | sed -e
's/\[/\\\[/g' -e 's/\]/\\\]/g' -e 's/ /\\\ /g' -e 's/\./\\\./g' -e 's/{/\\\{/g' -e 's/}/\\\}/g' -e 's/\!/\\\!/g' -e 's/\&/\\\&/g' `
#Escape out all special characters so it works in sed
for i in *
do
FNAME=`echo "$i" | sed s/"$SPATT"//`
if [ "$i" != "$FNAME" ]
then
mv "$i" "$FNAME"
fi
done

echo "done"
fi

#------------------Trim directory names after year

if echo "$OPT" | grep -q 't'
then
echo -n "Trimming directory names after noscript and year... "
for h in *
do

if [ -d "$h" ]
then
FNAME=`echo "$h" | sed 's/\[\ www\.Torrenting\.com\ \]\ \-\ //' | sed 's/1080//' | sed 's/1400//'`
EARLY="$FNAME"
FNAME=`echo "$FNAME" | sed 's/\(^.*([0-9]\{4\})\).*$/\1/'` #this won't do anything unless the year is in parentheses

if [ "$FNAME" = "$EARLY" ] #testing whether parentheses-dependent sed command did anything
then
FNAME=`echo "$FNAME" | sed 's/\(^.*[0-9]\{4\}\).*$/\1/'` #if not, trim after last digit in year
FNAME=`echo "$FNAME" | sed 's/\([0-9]\{4\}\)/(\1)/'` #and then add parentheses around year
mv "$h" "$FNAME" #and rename
else
mv "$h" "$FNAME" #if the parentheses-dependent sed worked, just rename it
fi

fi

done
rename 's/\[\(/\(/' *
rename 's/\(\(/\(/' *
echo "done"
fi

#------------------Match file names to parent directory names

if echo "$OPT" | grep -q 'm'
then
echo -n "Matching filenames to parent directory names and deleting junk files... "

for h in *
do

if [ -d "$h" ]
then
rename 's/ /_/g' "$h"#replace spaces in directory names
fi#with underscores so mv doesn't choke

done

for i in *
do

if [ -d "$i" ]
then
cd "$i"

for j in *
do
#replace spaces with underscores in all filenames in each subdirectory
rename 's/ /_/g' *
done

cd ..
fi

done

for k in *
do

if [ -d "$k" ]
then
cd "$k"#go into each directory
find ./ -regex ".*[sS]ample.*" -delete#take out the trash
NEWN="$k"#NEWN="directory name"

for m in *
do
EXTE=`echo $m | sed 's/^.*\(....$\)/\1/'`#read file extension into EXTE
if [ "$EXTE" = ".mp4" -o "$EXTE" = ".m4v" -o "$EXTE" = ".mkv" -o "$EXTE" = ".avi" ]
then
mv -n $m "./$NEWN$EXTE"

elif [ "$EXTE" = ".srt" ]
then
#check to see if .srt file is actually real
FISI=`du "$m" | sed 's/\([0-9]*\)\t.*/\1/'`
#is it real subnoscripts or just a few words based on file size?
if [ "$FISI" -gt 10 ]
then
mv -n $m "./$NEWN.eng$EXTE"#if it's legit, rename it
else
#if it's not, delete it
rm $m
fi

elif [ "$EXTE" = ".sub" -o "$EXTE" = ".idx" ]
then
mv -n $m "./$NEWN.eng$EXTE"

elif [ "$EXTE" = ".nfo" -o "$EXTE" = ".NFO" -o "$EXTE" = ".sfv" -o "$EXTE" = ".exe" -o "$EXTE" = ".txt" -o "$EXTE" = ".jpg" -o "$EXTE" = ".JPG" -o "$EXTE" = ".png" -o "$EXTE" = "part" ]
then
rm $m#delete all extra junk files
fi

done

cd ..
fi
done

#turn all the underscores back into spaces
#in directory names first...
rename 's/_/ /g' *

for n in *
do
if [ -d "$n" ]
then
cd "$n"
for p in *
do
rename 's/_/ /g' *#...and files within directories
done
cd ..
fi
done

fi

#---------------------List directories and files

echo
"done"

echo

for i in *
do
if [ -f "$i" ]
then
echo -e "\033[34m$i\033[0m"
elif [ -d "$i" ]
then
echo -e "\033[32;4m$i\033[0m"
cd "$i"

for j in *
do
if [ -f "$j" ]
then
echo -e "\t\033[34m$j\033[0m"
elif [ -d "$j" ]
then
echo -e "\t\033[32;4m$j\033[0m"
fi
done
echo
cd ..
fi

done

echo


https://redd.it/1hz5ec8
@r_bash
Bad Matrix / Share your Bash sh-nanigans

Why? Because, when life in meatspace gets a bit too much, it's important for the sh-oul, to hor-sh around for sh-ts and giggles.

Throwback: a really bad "Matrix Rain" animation, in a very large Bash function, made with tput and urandom and faux-Kanji that may end up spelling swear words (but I don't want to know!) https://www.evalapply.org/posts/bad-matrix/

sh-ow yours!

https://redd.it/1hzryva
@r_bash
I created "Command Runner", a library that helps you setting up a simple CI for your projects.

Hey guys,

that's my first post on reddit and this subreddit in particular, so I hope I get the format right ;)

I wanted to create a simple CI library for my repositories to run reoccurring commands repeatedly and have a nice report after execution. I came up with "Command Runner".



https://github.com/antonrotar/command\_runner



It provides a simple API and some settings to adjust execution and logging. It's basically a thin wrapper around commands and integrates nicely with larger scope tool setups like Github Actions.

Have a look! :)

https://redd.it/1i0b2st
@r_bash
Need Help in Improving my noscript

So , I have a small project where i want to install a few things on my laptop , so i created a noscript to help me out , as a generic noscript.

But the thing is there are still a few thing i could need help with . please share your view and if possible please share it as a PR if you can . will help a lot

the Link to the repo: https://github.com/aniketrath/noscripts

https://redd.it/1i0mtci
@r_bash
Trying to create install noscript for a rails app, struggling with if statements and multi line comments

I am trying to create an installation noscript to normalize development environments for a rails application.

I am struggling with this command:

certbot certonly \
--dns-cloudflare \
--dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \
--dns-cloudflare-propagation-seconds 60 \
-d example.com

I do not understand how to use multiline comments with `\` inside the if statement below. I am properly doing something stupid wrong, but I can't figure it out.

if [ -e ~/.secrets/certbot/cloudflare.ini ]; then
echo -e "A Cloudflare token is already configured to be used by Certbot with DNS verification using Cloudflare. \nWe will try to request a certificate using following FQDN:"
echo $hostname
read -n 1 -s -r -p "Press any key to continue."
echo "We are now creating sample certificates using Let's Encrypt."
sudo certbot certonly \ --dns-cloudflare \ --dns-cloudflare-credentials ~/.secrets/certbot/cloudflare.ini \ --dns-cloudflare-propagation-seconds 60 \ -d $hostname
echo "The certificate has been created."
else
echo -e "Cloudflare is not yet configured to be used for Certbot, \nPlease enter your API token to configure following FQDN:"
echo $hostname
read cloudflaretoken
echo "We are now creating your file with the API token, you will find it in the following file: ~/.secrets/certbot/cloudflare.ini."
mkdir -p ~/.secrets/certbot/
touch ~/.secrets/certbot/cloudflaretest.ini
bash -c 'echo -e "# Cloudflare API token used by Certbot\ndns_cloudflare_api_token = $cloudflaretoken" > ~/.secrets/certbot/test.ini'
fi

https://redd.it/1i1fgsz
@r_bash
Help writing function/pipeline

Hi I'm relatevely new to bash and I use it mainly to process small data files. I've been using these commands to extract and reorder data from .cvs files, I've tried to write a single pipeline with the commands but so far I've been unable to properly add the sed command into the pipeline, everything works fine until the sed command needs to be used but if separate the pipeline before each sed everything works fine. So any help to integrate everything into a single pipeline or even to create a function would be great. Thank you in advance.


awk -F "\"*,\"*" '{print $2}' File1.csv| tail -n +2| paste -sd" " > File2.txt

sed -i 's/ 0 /\n/g' File2.txt

sed -i 's/ /\t/g' File2.txt

https://redd.it/1i0km45
@r_bash
Change colour of double tab suggestions

I have been playing around with customising my bash prompt, just for fun, and it got me wondering if there's a way to alter the colour of the suggestions that appear when pressing double tab. Usually it will display all your options for filling in either the next file/directory, or your options for commands, on a separate line but in the same colour as the rest of the text. can I make it be a different colour to the rest?

https://redd.it/1i1o6rv
@r_bash
My noscript uses more CPU than I think it should

I created the below noscript to turn off the keyboard light on my Lenovo Thinkpad P1 when I'm not typing.

https://gist.github.com/tonsV2/cc97bb6dd3fdd82e2e2961d417803eaa

However I see it at the top of my process list using close to 100% of CPU for a lot longer than I'd expect. Can anyone here tell me how to improve it?

https://redd.it/1i1q6u3
@r_bash