r_bash – Telegram
change color in css file (working)

i have this simple bash file, to run you do for example "./test.sh accent \\#ffffff", but i want to merge the functions into one. how to do this?

https://preview.redd.it/87w1mp1q6g7g1.png?width=1281&format=png&auto=webp&s=c510c57cc1727e1e94613841bb6dd5281da41d74



https://redd.it/1pnloa1
@r_bash
Asking for help with a command launcher noscript

I'd like to ask a question about an automation strategy which has eluded me.

# What I'm trying to do

I'd like to have a noscript which:

1. can launch a new terminal emulator
2. then run a login shell in the terminal emulator (with all my personal shell initialization)
3. can then run an arbitrary program or command of my choosing
4. then on completion or termination of the program the shell stays alive and interactive
5. also the arbitrary command is added to shell history

Hopefully I explained that well.
Unfortunately something like alacritty -e bash -c 'echo hello' does not fulfill these requirements.
With the above the terminal is closed after program completion and is not run with shell initialization (login shell).

I'll share my solution, but I'm curious if there is an easier way to accomplish the same:

# Current Solution

I also put the code in this repo

I add the following to the end my ~/.bashrc

if [ -n ${INIT_CMD} ]; then
print -s "${INITCMD}"
eval "${INIT
CMD}"
unset INITCMD
fi

then to launch programs I use something like:

#!/usr/bin/env bash

# terminal='alacritty'
# terminal='ghostty'
# terminal='st'
terminal='kitty'

${terminal} -e
$SHELL \
-c 'INIT
CMD="echo hello" $SHELL'

where echo hello is the "program"

Which does require starting up two shells, however, the first shell with -c flag is cheap. The second shell is the login shell.

Thanks in advance if you know a simpler way to accomplish this!

https://redd.it/1po6z4y
@r_bash
How to check if $var is in a list?

Imagine you have:

mylist=("foo" "bar" "baz")



How can you check if $var is in mylist?

I don't want to write a loop for that :-)

https://redd.it/1ppmxqf
@r_bash
Bash Trek: TNG, an Update to a Retro Terminal Game

One or two attentive readers may recall that a couple of months ago, I posted about a Bash version of the old Star Trek terminal game that I'd written, called Bash Trek.

Well - I've adapted it into a new implementation, Bash Trek: TNG which replaces the old typed command interface with mouse control.

Mouse reporting is an underused thing, I think. It's not hard to build simple menus or buttons for simple terminal applications.

https://preview.redd.it/uj9z4g3za18g1.png?width=708&format=png&auto=webp&s=4baa2d60360563fe9c27a2afc6c9f1fb0dd78d5d

https://preview.redd.it/gxmif3e0b18g1.png?width=708&format=png&auto=webp&s=1c685b49377dfcc68684033c7da5397618f2a346

Anyway: if interested, https://github.com/StarShovel/bash-trek-tng



https://redd.it/1pq3ihb
@r_bash
Little help needed! sometimes this noscript exits after the first line

#!/bin/bash



yt-dlp --skip-download --flat-playlist --print-to-file id 'ids.txt' $1

awk '!seen[$0]++' ids.txt | tee ids.txt

awk '{print NR, $0 }' ids.txt | sort -rn | awk '{print $2}' > ids.log

awk '{print "wget `http://img.youtube.com/vi/"$1"/mqdefault.jpg` -O "NR".jpg"}' ids.log

the argument in the first line is a youtube video url or channel url. It downloads the id of the video/videos. Sometimes the code exits here, other times it actually goes to the other lines.

the second line is to filter out duplicate lines. Video ids are uniq, but if you run the code again, it just appends the ids to 'ids.txt'

the third line sorts ids.txt in reverse order. I then use the ids to download video urls in the fourth line. Please help me out. I would also appreciate if you help improve the noscript in other areas. I would like to add a padding of 5 to the output filenames, so that 1.jpg becomes 00001.jpg and 200.jpg becomes 00200.jpg

Thank you very much in advance

https://redd.it/1pqloym
@r_bash
Why doesn't closing this program's stdout not cause a pipeline to finish?

I'm seeing something strange.

This command:
dd if=/dev/zero bs=1024k count=256 | gprog --size-estimate $((1024*1024*256)) | sha256sum
...produces a sha256, when gprog closes its stdout.

This command:
gprog-du-tar --directories /usr | sha256sum
...does not produce a sha256 when gprog closes its stdout. Instead, it waits until gprog's GUI is shut down, well after gprog closes its stdout.

gprog itself is a python noscript, and can be found at:
https://stromberg.dnsalias.org/svn/gprog/trunk/gprog

gprog-du-tar is a bash noscript that wraps gprog, and can be found at:
https://stromberg.dnsalias.org/svn/gprog/trunk/gprog-du-tar

I suspect the problem isn't particularly related to gprog, since that same command exhibits such different behavior when run by itself at a bash prompt, vs. run inside a shell noscript: gprog-du-tar.

So you don't need to visit those links above (in case you'd rather not), here's gprog-du-tar's content:
#!/bin/bash

# this is far from a perfect estimate, but it's usually pretty decent

function usage
{
retval="$1"
case "$retval" in
0)
;;
*)
exec 1>&2
;;
esac
echo "Usage: $0"

echo "--directories A list of directories to du and tar - must be the last option"
echo "--help This stuff"
echo
echo "Tar up a local directory hierarchy and pipe it through gprog, using du to get an estimate of how much data will need"
echo "to be copied."
exit "$retval"
}

while [ "$#" -ge 1 ]
do
if [ "$1" = --directories ]
then
shift
break
elif [ "$1" = --help ]
then
usage 0
else
echo "$0: Illegal option: $1" 1>&2
usage 1
fi

shift
done

if type -path gtar > /dev/null 2>&1
then
tar=gtar
else
tar=tar
fi

estimate=$(for i in "$@"
do
du -skx "$i"
done | \
count -c | \
python3 -c '
import sys
total = 0
for line in sys.stdin:
total += int(line.split()[0]) * 1024
print(total)')

echo "Estimate: $estimate bytes" 1>&2

"$tar" --create --sparse --one-file-system "$@" | gprog --size-estimate "$estimate" --noscript "gprog-du-tar $*"

Any suggestions? I'm a bit baffled by why the same program would give such a different result based on how its run.

https://redd.it/1pqz5nj
@r_bash
Can someone help me understand why?
https://redd.it/1pr235a
@r_bash
How do I accomplish this?
https://redd.it/1prh09a
@r_bash
what is the best way to measure the time of a command ?

i wana measure the time of `python3 prog.py< in >out`

i know how to do that in c/c++ but i don't wana overhead

so what is the best way to do that in the terminal ?

(in milliseconds)

thanks

https://redd.it/1ps5gju
@r_bash
Are there any set of good practice assignments out there for learning bash

Title, thought of this when going through a bash course on YouTube. Edit: something for sed/awk will be useful as well

https://redd.it/1psquhh
@r_bash
Just discovered per-directory history. What else exists?

I came across this project: https://github.com/martinec/bash-per-directory-history

It's not seen much activity over the years and I can think of a few things for improvement. Just wanted to know if any knows of any alternatives that is more maintained before I start modifying this.

Meanwhile, what other cool stuff like this exists for bash?

https://redd.it/1psvweu
@r_bash
Methods to consider aborting everything if one of the steps below fails?



#!/usr/bin/env bash

docker network create "network.development.ch_api"
docker volume create "redis_certs.development.ch_api"

docker build \
--file "${PWD}/docker/development/caddy_server/Dockerfile" \
--tag "caddy_server.development.ch_api" \
--quiet .
docker build \
--file "${PWD}/docker/development/express_server/Dockerfile" \
--tag "express_server.development.ch_api" \
--quiet .
docker build \
--file "${PWD}/docker/development/postgres_server/Dockerfile" \
--tag "postgres_server.development.ch_api" \
--quiet .
docker build \
--file "${PWD}/docker/development/redis_certs/Dockerfile" \
--tag "redis_certs.development.ch_api" \
--quiet .
docker build \
--file "${PWD}/docker/development/redis_server/Dockerfile" \
--tag "redis_server.development.ch_api" \
--quiet .

docker run \
--detach \
--env-file "${PWD}/docker/development/.env" \
--interactive \
--name "redis_certs.development.ch_api" \
--network "network.development.ch_api" \
--tty \
--volume "redis_certs.development.ch_api:/home/tests/tls:rw" \
"redis_certs.development.ch_api"

docker container wait "redis_certs.development.ch_api"

docker cp "redis_certs.development.ch_api:/home/tests/tls/ca.crt" "${PWD}/certs/docker/development/redis/ca.crt"

docker cp "redis_certs.development.ch_api:/home/tests/tls/client.crt" "${PWD}/certs/docker/development/redis/client.crt"

docker cp "redis_certs.development.ch_api:/home/tests/tls/client.key" "${PWD}/certs/docker/development/redis/client.key"

docker run \
--detach \
--env-file "${PWD}/docker/development/.env" \
--interactive \
--name "redis_server.development.ch_api" \
--network "network.development.ch_api" \
--publish 41729:41729 \
--restart unless-stopped \
--tty \
--volume "redis_certs.development.ch_api:/etc/ssl/certs:ro" \
"redis_server.development.ch_api"

docker run \
--detach \
--env-file "${PWD}/docker/development/.env" \
--interactive \
--name "postgres_server.development.ch_api" \
--network "network.development.ch_api" \
--publish 47293:47293 \
--restart unless-stopped \
--tty \
"postgres_server.development.ch_api"

docker run \
--detach \
--env-file "${PWD}/docker/development/.env" \
--interactive \
--name "express_server.development.ch_api" \
--network "network.development.ch_api" \
--publish 34273:34273 \
--restart unless-stopped \
--tty \
--volume "redis_certs.development.ch_api:/home/node/ch_api/certs/docker/development/redis:ro" \
"express_server.development.ch_api"

docker run \
--detach \
--env-file "${PWD}/docker/development/.env" \
--interactive \
--name "caddy_server.development.ch_api" \
--network "network.development.ch_api" \
--publish 80:80 \
--publish 443:443 \
--restart unless-stopped \
--tty \
"caddy_server.development.ch_api"


- Take a look at this noscript above
- It creates a docker network and a volume
- Then it builds a few images
- Then runs a container to generate certs
- Copy certs back to local machine and then runs a few other containers dependend on the above one
- Let us say that one of these steps fail. Now obviously if the network exists or volume does or even the image exists or if you attempt running the container with the same name twice, it is most certainly going to fail
- Let us say you want to abort everything and undo whatever was done if one of the steps fail
- Let's talk about the methods to handle such a case

# Put an if statement on every command

if docker run .... then
success
else
abort
fi


- This does the job but is going to look very ugly for like 25 invocations above

# Set -Euox pipefail

- I was warned somewhere that you should not do this because it causes unpredictable behavior

## Questions

- What are my options here?
- If someone presses Ctrl + C in the