r_bash – Telegram
While (loop) with if-statement and timeout after 300 seconds

Hi,


I want to create a while (loop) with an if-statement which timesout after 300 seconds, when the neccessary value isnt provided from the service.


My attempt:
# API Request (GET) - Status
API_STATUS=`curl .....`

# Set timeout value
TIMEOUT=0

while [[ "$API_STATUS" != "1.0" ]];
do
# Increase timeout value by 1
TIMEOUT=$((TIMEOUT+1))

if [[ TIMEOUT != "60" ]]
then
# Wait 5 seconds
sleep 5
API_STATUS=`curl .....`

elif [[ TIMEOUT == "60" ]]
then
echo "$TIMESTAMP Not reachable." >> $LOG
exit
fi
done


Note:
Im executing a curl command to get a value from the backend service. If "1.0" then stop while-loop, if the service isnt reachable for example "120 seconds" wait and then proceed.

https://redd.it/11r91rv
@r_bash
Code golf: highly minified function for injecting ANSI color in bash noscripts

I've been putting together a function for inserting ANSI color sequences in bash noscripts and trying to keep the character count down.

Here's a picture to give you an idea what I'm going for: https://i.imgur.com/6CnnGtN.png

The idea is to replace all those ubiquitous color definition blocks in the noscripts I inherit that leave the first page looking like this (clipped from a recent submission here):

#Regular Color Variables
coff='\033[0m' #No Color
black='\033[0;30m'
#Black
red='\033[1;91m' #Red
green='\033[1;92m' #Green
yellow='\033[0;93m' #Yellow
blue='\033[0;94m' #Blue
purple='\033[0;95m' #Purple
cyan='\033[0;96m' #Cyan
white='\033[0;97m' #White
#BackGround Colors Variables
b
black='\03340m'
b_red='\033[41m'
b_green='\033[42m'
b_yellow='\033[43m'
b_blue='\033[44m'
b_purple='\033[95m'
b_cyan='\033[46m'
b_white='\033[47m'

I've got something that supports all 16 colors on all 16 background colors in the standard 4-bit ANSI palette, as well as bold, underline, and "terminal default" codes (`\e[39m`, `\e[49m`).

clr() {
local in="$*" fgb=30 bgb=40 colors="krgybmcw =" fg bg cs
[[ "$in" ] || { printf "%b" "\e0m"; return; } # reset
[[ "$in" == *-* ] && cs=";22;24"; # normal
[ "$in" == *+* ] && cs=";1"; # bold
[ "$in" == *_* ] && cs+=";4"; # underline
in=${in//-_+ /}
[ "${in,,}" =~ ^[.=rgbcmykw{0,2}$ ]] || return 1
fg=${in:0:1}; [ "$fg" ] || fg=.; bg=${in:1:1}; bg=${bg#.}
[ "$fg" =~ [A-Z ]] && fgb=90
[ "$bg" =~ [A-Z ]] && bgb=100
[ "$fg" == "." ] || { i=${colors%%"${fg,,}"}; cs+=";$((${#i}+fgb))"; }
[[ "$bg" ]] && { i=${colors%%"${bg,,}"
}; cs+=";$((${#i}+bgb))"; }
printf "%b" "\e${cs#;}m"
}

I'm trying to keep it mostly minified so it's a tricky read. The less obvious abbreviations are `in`: input arguments, `cs`: control sequence, `fgb`/`bgb`: foreground/background brightness.

`shellcheck --shell=bash clr.sh` surprisingly had no complaints when I fed it this snippet, I was expecting to be flooded with complaints (so many unquoted expansions and assignments!) so I'm not even sure I'm using it right.

The parameters it accepts:

Parameter | Result
---|---
no arguments | Reset to default formatting
r,g,b,c,m,y,k,w | Red, green, blue, cyan, magenta, yellow, black, white
R,G,B,C,M,Y,K,W | Bright versions of same colors
+ | bold
_ | underline
- | strip formatting (bold/underline)
= | terminal default background/foreground
. | placeholder to separate foreground from background when changing background only

It doesn't care about the order of parameters or the spacing, except that the background color must be preceded by a foreground color (hence the no-op `.` placeholder for leaving foreground alone).

I'm trying to make it so that it outputs the minimal escape sequence for the desired formatting: e.g. no `0;` or `1;` when changing only the color. What I most dissatisfied with so far is having to use `22;24` to strip the bold/underline formatting, but `0;` strips the colors along with the formatting (breaking the "Blue bold not bold" example in the screenshot) and I don't want to make the function have to track any state to know whether it's underlining or bold that needs to be cancelled out.

Any tips? Especially ones that reduce the character count!

[https://redd.it/11ri7f3

@r_bash
aliases to save current dir in one terminal and move to it in a different terminal

I often open a new terminal windows to work in the same directory as my current terminal window and I find it annoying to have to manually cd to it, specially if current directory path is long. There is probably a better way but here is what I came up with:

alias sf='pwd > ~/.folder'

alias lf='cd "$(cat ~/.folder)"'

sf is for save folder, lf for load folder.

I literally just came up with that so I haven't really used it for real beyond testing.

https://redd.it/11rjw8q
@r_bash
Read contents of file and run a command

My scenario:

cat filesystems.txt
#path;major;minor;average
/var;10;10;10
/opt;10;10;10

Then, my bash must read this file (filesystems.txt) and for each line of this file (except first line), do "df -h" for each item.

My goal is to have this:

df -h /var
df -h /opt

​

https://redd.it/11rl8b0
@r_bash
having trouble checking if a drive is mounted!

Greetings everyone,

​

I'm making a little wrapper for pmount to automate the mounting/dismounting of hotplugged LUKS USB devices. I'll post the noscript below.

https://pastebin.com/Tr9ZUEJe

I'm having trouble in particular with the "scan" function. I'm trying to get it to display eligable devices ONLY if they are not already currently mounted. I'm not sure how to do this. Currently, it displays (correctly) all devices which 1.) have a UUID and 2.) have an associated .key file in the $SECRETSDIR/luks folder. I'd like this output to exclude whatever is included in the "currentlymounted" array.

​

I'd really value any input about how to accomplish this, thanks in advance!

https://redd.it/11rk4b0
@r_bash
Emulate "Copy Link To Clipboard" By Copying Text Wrapper - How To?

Is it possible to do the copying in bash by emulating "copy link to clipboard" when the text wrapper is highlighted? Projecting this onto Markdown format of URL it would look like highlighting the [reddit!] segment of [reddit](https://reddit.com) with (https://reddit.com) going to the clipboard. Refer to the linked image for the visual example.

https://i.imgur.com/AvTIOaM.png

https://redd.it/11rjkpg
@r_bash
smenu is so cool! :)

A demo of what kind of menus you can make with smenu, and a small tour of the command line history too, which I have installed on Ctrl-n, since I have FZF on Ctrl-r.

smenu demo

https://redd.it/11rrfcs
@r_bash
My VOIP phone service provides an API to send SMS .. is this how its done using curl

API is

https://www.thevoipcompany.com/send-sms?username=username&password=password&to=destination&subnoscriptionId=subnoscription ID&text=message

My understanding curl could do this with something like this ... correct?

>curl -X POST -d 'username=username&password=password&to=destination&subnoscriptionId=subnoscription id&text=message' https://www.mynetfone.com.au/send-sms

If correct it would be simple to put it into a bash wrapper.

https://redd.it/11rved7
@r_bash
Bash continuous parallelization

I work with academic research, specifically with simulations. The important feature of these simulations in the context of the problem is that some of them take longer than others.

I want to run let's say 1000 simulations, each in a single thread of the processador. If my machine has 6 threads i can do several batches of 6 + one batch of the remaining.

Using something like:

./run.out 1 & ./run.out 2 & ./run.out 3 & ./run.out 4 & ./run.out 5 & ./run.out 6 & wait
./run.out 7 & ./run.out 8 & ./run.out 9 & ./run.out 9 & ./run.out 10 & ./run.out 11 & wait

And that's definetly runs faster than doing one by one, sequentially. But if i have at least process that takes a lot longer than the others, i'm losing precious time.

The final question is: Is there a simple way that can i set a fixed number of processes to be running at the same time, and as soon as one is finishes another one takes it place, that way i'm not bottlenecked by a single slower program.

I will greatly apreciate the help, since solving this would greatly improve my reseach.

https://redd.it/11rxgwt
@r_bash
How to run ffmpeg async to bash

I'm running into an issue where inotifywait stops running after its event fires and runs ffmpeg.

Here's my code:

#!/bin/bash
# This bash noscript uses inotifywait to watch a directory for new mp4 or mkv files and convert them using ffmpeg.


# Set the directory to watch

DIR="/home/stingwraith/Downloads"

# Wait for a new mp4 or mkv file to be created

inotifywait -m $DIR -e create -e movedto |

while read path action file; do
echo "The file '$file' appeared in directory '$path' via '$action'"

# Check if the file is an mp4 or mkv
if [[ $file == *.mp4 || $file == *.mkv || $file == *.ogv ]]; then
# resolution=$(ffprobe -v error -select
streams v:0 -showentries stream=width,height -of csv=s=x:p=0 $DIR/$file)
# if [ ${resolution#*x} -lt 720 ]; then
# Convert the file using ffmpeg
echo "Starting conversion..."
ffmpeg -hwaccel vaapi -hwaccel
device /dev/dri/ renderD128 -i $DIR/$file -c:v libx264 -vf "scale=1024:576" -crf 25 -preset veryslow -r 22 -c:a aac -b:a 192k -ac 2 $DIR/$file.converted.mp4
echo "Conversion completed! Original file deleted!"
# Remove the original file
rm $DIR/$file
# else
#do something
# fi
fi
done




The problem is that while ffmpeg is running an encoding job, if any new files show up, it won't run ffmpeg on them.

Any help is much appreciated!

https://redd.it/11s2ga6
@r_bash
cp: cannot create regular file '/usr/local/bin/arduino-cli': Permission denied

Hello!

I have what is likely a really dumb question. I'm trying to automate the installation of the arduino-cli. I tried using the following line:

sudo curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=/usr/local/bin sh

When i try and run that, I get prompted for a password (which makes sense), and then I see this:

Installing in /usr/local/bin
ARCH=64bit
OS=Linux
Using curl as download tool
Downloading https://downloads.arduino.cc/arduino-cli/arduino-cli_0.31.0_Linux_64bit.tar.gz
cp: cannot create regular file '/usr/local/bin/arduino-cli': Permission denied
Failed to install arduino-cli

It appears to not want me to write to this directory, even with sudo access, so I'm a bit confused as to what the issue is, or how to resolve it. I ran :

ll /usr/local/bin

and received back:

drwxr-xr-x 2 root root 4096 Aug 12 2021 ./
drwxr-xr-x 10 root root 4096 Feb 9 2021 ../

If I use sudo, I assume this means I have write access to this folder but maybe I'm misunderstanding permissions. I assume it's somewhere along these lines but wanted to check before I poked around with it too much and broke something.

Any suggestions you can give me would be greatly appreciated. Thanks!

https://redd.it/11s4oay
@r_bash
ANSI coloring function for bash noscripts, absurdly minified

I've been dabbling with this ANSI coloring function for injecting color into bash noscripts. All feedback is welcome!

Essentially, it works like this: https://i.imgur.com/BXEPxva.png

It supports the entire standard 4-bit ANSI palette with all foreground/background combinations, as well as bold and underline. This is a full table of its formatting and color codes: https://i.imgur.com/EKi9KIm.png


(Kudos to /u/o11c for critiquing the first draft and starting me on the path to shaving 200 bytes off my earlier attempt, even if maybe he's an exalted greybeard raised on monochrome monitors who wouldn't touch this function with a ten foot pole on his machines. I'm only guessing.)

-----

I often inherit noscripts from colleagues and folks on the web with coloring variables similar to these recent submissions:

-----

https://github.com/vaniacer/sshto/blob/master/sshto#L87



| Input | Output
---|---|----
This noscript | `printf "${BLD}${RED}${BBLU}"Hello!${DEF}"` | `\e[1m\e[31m\e[44mHello!\e[0m`
clr function | `printf "$(clr +rb)Hello!$(clr)"` | `\e[1;31;44mHello!\e[0m`

-----

https://github.com/Aj-Seven/Android-Sysinfo/blob/master/sysinfo.sh#L2

| Input | Output
---|---|----
This noscript | `echo "${yellow}${b_blue}foo` | `\e[0;93m\e[44mfoo`
clr function | `echo "$(clr Yb)foo"` | `\e[93;44mfoo`

-----

The function is highly minified, the secret sauce is using `colors="krgybmcw ="` with `i=${colors%%"${1,}"*}` and `${#i}` to find the offset of the color the character represents in the ANSI definitions.

| Black | Red | Green | Yellow | Blue | Magenta | Cyan | White | --- | Term Default
---|---|----|----|----|----|----|----|----|----|----|
My abbreviation | k | r | g | y | b | m | c | w | | =
ANSI fg | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | | 39
ANSI bg | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | | 49
ANSI bright fg | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | |
ANSI bright bg | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | |

I took liberal advantage of the fact that `bg=fg+10` and `bright=normal+60`.

I also wasted seven bytes on unnecessary capitulations toward `shellcheck`, but it passes now.

The ugly shortened variable names represent:

* in: input arguments
* b: basis for color numbering, 30 is the minimum, +60 for bright, +10 for background, +0 through +7 for hue
* bb: background bump, after setting a foreground color add 10 to the next color code to make it a background color instead of a foreground color
* c: single character read from `$in` on the while loop
* cs: control sequence, what goes between the `\e[` and the `m` in the ANSI formatting string

Note the function is defined as `clr() ( ... )` with parentheses instead of the standard `clr() { ... }` braces, this is to run it in a subshell so I can have a function (c2n, color to number) inside my function.

----

clr() (
local in="$*" colors="krgybmcw =" b bb c cs
[ "$in" ] || { printf "\e[0m"; return; }
c2n() { local i=${colors%%"${1,}"*}; printf "%d" "$((${#i}+b+bb))"; }
while c=${in::1}; [ "$c" ]; do in=${in:1}; b=30;
case $c in [A-Z]) ((b+=60));;&
[A-Za-z=]) cs+=";$(c2n "$c")"; (((bb+=10)>20)) && return 1 ;;
-) cs+=";22;24";; +) cs+=";1";; _) cs+=";4";; .) ((bb+=10));;
*) return 1;;
esac
done
printf "\e[%sm" "${cs#;}"
)

Did you know bash's `type` builtin will automatically reformat your functions with its standards for indentation and line breaks? Here's the same function as `type` outputs it for easier reading:

bash-5.1$ type clr
clr is a function
clr ()
{
( local in="$*" colors="krgybmcw =" b bb c cs;
[ "$in" ] || {
printf "\e[0m";
return
};
function c2n ()
{
local i=${colors%%"${1,}"*};
printf "%d" "$((${#i}+b+bb))"
};
while c=${in::1};
[ "$c" ]; do
in=${in:1};
b=30;
case $c in
[A-Z])
((b+=60))
;;&
[A-Za-z=])
cs+=";$(c2n "$c")";
(((bb+=10)>20)) && return 1
;;
-)
cs+=";22;24"
;;
+)
cs+=";1"
;;
_)
cs+=";4"
;;
.)
((bb+=10))
;;
*)
return 1
;;
esac;
done;
printf "\e[%sm" "${cs#;}" )
}

Again, all feedback is welcome! I'm not trying to encourage adoption of this thing, it's more of a puzzle of brevity and optimization. I learned a lot from /u/o11c the first time I showed it. (If you think you can use it, have at it.)

I especially don't like using `;22;24` to clear underline and bold formatting, but `\e[0m` clears the colors as well as the formatting, and I don't want to have to preserve any state to know which one needs clearing.

https://redd.it/11sjtcu
@r_bash
Measure the speed of source

The follow one show me the speed of the source "zero":

dd if=/dev/zero of=filename bs=1M count=1024 status=progress

But if my if my harddisk are not much faster than my source, I will measure not realy exact. Therefore, I would like to specify another target, which is probably faster. I think I have seen something that was written in dev 0 or similar. What do you take there and how do you specify that?

https://redd.it/11snkbm
@r_bash
How to make a CLI?

I’m wondering how you make custom commands like docker command or mysql command or git command.

How do you make that custom keyword that you use to execute the command?

I would like to make a dummy CLI for a small hobby project just to see how it’s done.

Could someone point me in a direction of what to read?

https://redd.it/11soloo
@r_bash
-X option for compgen works as not expected

Here is my completion:

#!/usr/bin/env bash

__md_to_clip__complete() {
declare current="$2"
declare previous="$3"

case "$previous" in
--output-directory|-od)
mapfile -t COMPREPLY < <(compgen -o dirnames -- "$current")
;;
--special-placeholder-config|-spc)
mapfile -t COMPREPLY < <(compgen -o filenames -X '!*.yaml' -- "$current")
;;
*)
mapfile -t COMPREPLY < <(compgen -W "--help -h
--version -v
--author -a
--email -e
--no-file-save -nfs
--output-directory -od
--special-placeholder-config -spc" -- "$current")
;;
esac
}

complete -F __md_to_clip__complete md-to-clip

I expect just YAML files to be suggested for `--special-placeholder-config|-spc` options. But even I have *.yaml files in the current directory, nothing is suggested after `md-to-clip --special-placeholder-config <TAB>`. I don't understand what's wrong. The doc [says](https://www.gnu.org/software/bash/manual/html_node/Programmable-Completion-Builtins.html) that:

> A leading ‘!’ in filterpat negates the pattern; in this case, any completion not matching filterpat is removed.

What I am missing?

https://redd.it/11spfi1
@r_bash
How to lint Bash code piece put in a Markdown file?

For instance, I've written Bash completion and placed it in a README file. Here are several questions:

- Is it possible to lint such Bash code (if it's inside README)?
- Is such wish is right at all? I just feel that all code put in README should be checked somehow not to permit some mistakes after copy-pasting written code from .sh files to Markdown.
- Is markdown suitable for a such task at all? Maybe there is something better?

shellcheck can't extract code from Markdown files and check it.

P. S. Yeah, it's possible to write a solution by yourself to extract code and then lint it, but are there any ready to go tools?

https://redd.it/11sq7kv
@r_bash
Baby in bash asking for feedbacks :)

Hello,
I'm a junior developer and since I use to work on both Mac and Windows, I was annoyed that I couldn't get my hand on a Windows alternative to the `fortune` command that would give you a quote, either by calling it or by setting up to display the quote each time you open a terminal.
So I tried to do something and got it to work.

But as mentioned above, I don't know bash so I'm not sure if this piece of command is good enough, reason I'm here to ask for some feedbacks and explanations if you got time.

screenshot

Repository

Thank you for your time and your help :).


Seb.

https://redd.it/11su6yv
@r_bash
-A directory vs -o dirnames in completion

I am confused, there are too very similar options: -A directory and -o dirnames. What's the point of having them both in complete built-in? When they can be combined and why? Why Bash developers decided to implement them both, instead of one thing?

https://redd.it/11sx6vs
@r_bash