r_bash – Telegram
The source command closes the terminal

I have a noscript venvs.sh:

    #!/bin/bash

BASE_PATH=/home/ether/.venvs
SOURCE_PATH=bin/activate

if [ -z "$1" ]; then
echo "Usage:"
echo "venvs.sh ENV_NAME"
exit 0
fi

if [ ! -d "$BASE_PATH" ]; then
mkdir $BASE_PATH
if [ ! -d "$BASE_PATH" ]; then
echo "BASE_PATH '$BASE_PATH' does not exist."
exit 0
fi
fi

if [ ! -d "$BASE_PATH/$1" ]; then
python3 -m venv $BASE_PATH/$1
fi

FULL_PATH=$BASE_PATH/$1/$SOURCE_PATH

if [ ! -f "$FULL_PATH" ]; then
echo "Environment '$FULL_PATH' does not exist."
exit 0
fi

source $FULL_PATH


and an alias in the .bash_aliases:

    alias venv='source /home/ether/bin/venvs.sh'


Now, when i write venv testenv, the virtual environment named testenv is created and/or opened. It works like a charm.

The problem arises, when i don't specify any parameters (virtual environment name). Then the source command closes the terminal. How can i avoid this? I don't want to close the terminal.

https://redd.it/1no0pid
@r_bash
Possible breaking changes that would actually improve bash. What's your ideas?

I'll start:

Make it so that when i can use `echo -- ...` and echo doesn't print the -- and understand it as to stop reading its options. Instead i have to use printf.

Make it so that i can provide a delimiter to echo other than a space, possibly a string instead of single character. Therefore i can do `echo --delim $'\\n' *`, because sometimes it's usefull to have the files on separate lines. Instead i currently have to do `ls` or `echo * | tr ' ' $'\\n'` in these situations.

Scoped functions/commands definitions? Making callbacks would be better if the callback command doesn't still exists when its containing command returns.

Possilibity of having bash lists inside other lists. Recursive data structures would enable many things (such as lisp).

https://redd.it/1np2dy0
@r_bash
How do I do this with bash?

I have multiple videos and images in one folder. The goal is to use ffmpeg to add thumbnails to the videos.

the command to attach a thumbnail to a single video is

ffmpeg -i input.mkv -attach image.jpg -metadata:s:t:0 mimetype=image/jpeg -c copy output.mkv

The videos named as such

00001 - vidname.mkv

00002- vidname.mkv

00100 - vidname.mkv

01000 - vidname.mkv

and etc

as you can see, I have added number prefixes with a padding of zeros to the video names. The corresponding images are named in a similar manner .

00001.jpg

00002.jpg

00100.jpg

I want to attach the images to the videos based on the prefixes.

00001.jpg is to be attached to 00001 - vidname.mkv, and so on

https://redd.it/1npapqb
@r_bash
Few functions and aliased I use on my ~/.bashrc

Sometimes it's good to post something you wanted to read in a specific /r, so I'm doing it.
Down below there are a couple of functions and aliases that I have on my \~/.bashrc that I find extremely useful and handy. Let me know if those can be improved somehow.
Hope it'll help at least one person!

cd(){
if [[ $1 =~ ^-[0-9]+$ ]]; then
local n=${1#-}
local path=""
for(( i=0;i<n;i++ )); do
path+="../"
done
builtin cd "$path"
else
builtin cd "$@"
fi
}

rename(){
local n="$*"
PROMPT_COMMAND="echo -en '\033]0;${n}\007'"
}

export PATH="$HOME/.local/bin:$PATH"
alias execute="chmod +x"
alias editsh="vim ~/.bashrc"
alias sourcesh="source ~/.bashrc"
alias uu="sudo apt update && sudo apt upgrade -y"
alias desk="cd ~/Desktop"
alias down="cd ~/Downloads"
alias udb="sudo updatedb"
alias release="lsb_release -a"

https://redd.it/1nq77o6
@r_bash
What are ways to setup an isolated environment for testing shell noscripts?

I want to check that my shell noscripts won't fail if some non-standard commands are missing (e.g. qemu-system-*). To solve this problem with the least overhead only tools like schroot, docker or lxd come to mind. I think that potentially I could change in some way environment variables like PATH to emulate missing commands. However, I also want to prevent harming my FS while testing noscripts (protect myself from accidental sudo rm -rf --no-preserve-root /).

What are your thoughts?

https://redd.it/1nqhwpb
@r_bash
LLaMA en macOS

* Estoy trabajando en un conjunto de noscripts Bash para manejar modelos LLaMA en macOS. Aquí va un ejemplo de cómo valido dependencias con bash. ¿Alguien más ha intentado algo parecido?
* $MAIN\_BINARY" \\
* \-m "$MODELO\_PATH" \\
* \--ctx-size 31000 \\
* \--n-predict 500 \\
* \--color \\
* \--temp 1.2 \\
* \--threads 6 \\
* \--prompt "$(cat "$PROMPT\_SISTEMA") $(cat "$PROMPT\_FILE")" 2>&1 | grep -v 'llama\_' | tee >(tee -a "$LOG\_PATH") >> "$TEMP\_OUT"

https://redd.it/1nqxdhm
@r_bash
xargs for functions

I love the power of `xargs`. But it doesn't work with Bash functions. Here is `fargs`, which works with functions.

# Usage: source ~/bin/lib.sh
# This is a libary to be sourced by noscripts, such as ~/.bashrc:

# fargs - xargs for functions
# No space in xargs options. Bad: -n 2. Good: -n2 or --max-args=2
# All bash functions and local env vars will be accessible.
# otherwise, works just like xargs.
fargs() {
# Find the index of the first non-option argument, which should be the command
local cmd_start_index=1
for arg in "$@"; do
if [[ "$arg" != -* ]]; then
break
fi
((cmd_start_index++))
done

# Extract xargs options and the command
local opts=("${@:1:$((cmd_start_index - 1))}")
local cmd=("${@:$cmd_start_index}")
if [[ ${#cmd[@]} -eq 0 ]]; then cmd=("echo"); fi

# xargs builds command strings by passing stdin items as arguments to `echo`.
# The resulting strings (e.g., "my_func arg1") are then executed by `eval`.
# This allows xargs to call shell functions, which are not exported to subshells.
eval "$(xargs "${opts[@]}" bash -c 'printf "%q " "$@"; echo' -- "${cmd[@]}")"
}

https://redd.it/1nqij6u
@r_bash
Expect driving me crazy! Timeout or Loop or Nada

I try to read the firmware version of a device from a spawned ssh connection. So far so good. I have a block that basically puts "We are in" just as a debug print to see, wether the login works. Next block:

# Detecting firmware version
set deviceInfoRequested "no"
send "\r"
expect {
-re "#" {
if {"\$deviceInfoRequested" eq "no"} {
set deviceInfoRequested "yes"
send "show device\r"
after 500
expcontinue
}
}
-re {Firmware Version: ([0-9]+)\.([0-9]+)} {
set major \$expect
out(1,string)
set minor \$expectout(2,string)
puts "Detected Firmware version: \$major.\$minor"

# Compare numerically
if { (\$major > 4) || (\$major == 4 && \$minor > 2) } {
puts ">>> using newer Syntax for gateway"
set raritanVersion "new"
} else {
set raritanVersion "classic"
puts ">>> using old Syntax for gateway"
}
flush stdout
}
timeout {
puts "Timeout waiting for firmware version"
set raritanVersion "unknown"
exit 1
}
} # Detecting firmware version
set deviceInfoRequested "no"
send "\r"
expect {
-re "#" {
if {"\$deviceInfoRequested" eq "no"} {
set deviceInfoRequested "yes"
send "show device\r"
after 500
exp
continue
}
}
-re {Firmware Version: (0-9+)\.(0-9+)} {
set major \$expectout(1,string)
set minor \$expect
out(2,string)
puts "Detected Firmware version: \$major.\$minor"

# Compare numerically
if { (\$major > 4) || (\$major == 4 && \$minor > 2) } {
puts ">>> using newer Syntax for gateway"
set raritanVersion "new"
} else {
set raritanVersion "classic"
puts ">>> using old Syntax for gateway"
}
flush stdout
}
timeout {
puts "Timeout waiting for firmware version"
set raritanVersion "unknown"
exit 1
}
}

Without the if deviceInfoRequested it loops forever as each new prompt starts with # logically. If I put the # at the end after the Firmware Version detection block (which has no line exp_continue), it first sends show device, than successfully prints "Detected Firmware version: 4.4" and than waits for 5 seconds before the noscript exits... Why, if exp_continue is not used, does it not escape the expect {} block? Of course there are further blocks that follow... But it just times out. I tried send "show device\r" before the expect block, but than it just does not detect it. Also strange: My first expect block waits for either "Welcome" or "#" to detect: Yes I am logged in. This is where it puts "We are in". If I write something like:

first expect block > Prints "We are in" directly (before printing the rest that SSH still receives)
puts "HI"
after 5000
next expect block

The ovserved behavior is not what you might expect: After "We are in" NOTHING is printed for 5 seconds straight. Only than the rest of the login prompt is even printed an donly than we see the HI. Is it just me or is expect a horrible mess? How Shall I implement the logic to detect the firmware here? Direct SSH commands I would prefer, but the device does not support it. I need to rely on expect. I have written a screen handler that does the same that expect does... But it is sooo complicated and hard to debug that I wanted to try something simpler and yet lightweight.

By the way: The "flush stdout" was just a test to see if it makes a difference in behavior/order. Nada. Not at all.

Please tell me you understand what is going on here and why it either just times out, or loops forever or sleeps before printing to stdout although I defined it the other way around. I am not sure what is going on
From naïve to robust: evolving a cron noscript step by step

A “simple” cron noscript can bite you.

I took the classic example running a nightly DB procedure and showed how a naïve one-liner grows into a robust noscript: logging with exec, cleanup with trap, set -euo pipefail, lockfiles, and alerts.

If you’ve ever wondered why your noscript behaves differently under cron, or just want to see the step-by-step hardening, here’s the write-up.

https://medium.com/@subodh.shetty87/the-developers-guide-to-robust-cron-job-noscripts-5286ae1824a5?sk=c99a48abe659a9ea0ce1443b54a5e79a

Feedbacks are welcome. Is there anything I am missing that could make it more robust ??

https://redd.it/1ns4uba
@r_bash
Why use chmod?

Is there a reason to use chmod +x noscript; ./noscript instead of simply running bash noscript?

https://redd.it/1nsn976
@r_bash
Can I get some reviews or opinions on this noscript that I made?

So, I recently made a noscript for me to blink the scroll key like a heartbeat whenever I received a notification from example: Whatsapp or Discord, could I get some honest opinions about it? I decided there would be no better place to share this than good ol' Reddit. Here's the link to the Github repo:

https://github.com/Squary5928/notifled

(Btw, I'm kind of in a haste because I have to install Windows XP and fix a plugin on my minecraft server, hence the short denoscription)

https://redd.it/1nsprj5
@r_bash
Wrote a utility that makes working with symlinks a little easier.

I know there are many out there that does this. Here is my version. Any feedback on improvements feature/code wise would be helpful.


Thanks.

https://github.com/ctrl-alt-adrian/symlinkit


https://redd.it/1nt1qd6
@r_bash
Black magic quoting issue

Usually I can muddle through these on my own, but this one has really got me stumped. How can I get a window noscript into mpv's command line if it has spaces in it?

I can't find a way to do it where the noscript doesn't just wind up being whatever comes before the first space (no matter how many single quotes or backslashes I use, etc.); the best I've got so far is to replace the spaces with something _isn't_ a space, but _looks like_ one (the "En Quad" character) but I'd rather do it "the right way" (not to mention, to figure out _how_ to do it in case I run into something like this in the future where sed isn't an option).

This is the noscript I've been using to test...Reddit's editor inserted a bunch of backslashes and extra whitespace when I pasted it in, which I tried to revert.

I realize the way I'm building up the command line (at the end, with the $command_line variable) looks silly when it's reduced to its core for testing, but there's _a lot_ more logic in the real noscript and building the command line this way is integral to the overall process, so it's not something I'm willing to change.

```sh
#!/bin/bash

set -x

## En Quad / U+2000 / &#8192
#special_space=$'\u2000' ## En Quad (8-bit clean but requires BASH)
special_space=" " ## En Quad (the literal character)

case ${1} in
underscores)
window_noscript="Underscores:_Title_with_no_spaces."
;;
backslashes)
window_noscript="Backslashes:\ Title\ with\ backslashed\ spaces."
;;
spaces)
window_noscript="Spaces: Title with spaces."
;;
special)
raw_noscript="Special: Title with special spaces."
window_noscript=$(echo "${raw_noscript}" | sed -e "s/ /${special_space}/g")
;;
'')
${0} underscores &
${0} backslashes &
${0} spaces &
${0} special &
exit 0
;;
esac

##
## From here down is the "real" part of the noscript
##

command_line="mpv"
command_line="${command_line} --idle"
command_line="${command_line} --force-window"

## This is what I would have expected to need, but it
## doesn't work either
#command_line="${command_line} --noscript=\"${window_noscript}\""
command_line="${command_line} --noscript=${window_noscript}"

${command_line}

## EOF
########
```

https://redd.it/1nty619
@r_bash
I have a copy of this book, is it worth studying this end to end?
https://redd.it/1nux1tf
@r_bash
bash noscript that can detect all individual keystrokes?

I'm talking all individual keystrokes. Obviously, if you can open a pipe in a raw form, then stroking a glyph key will generate byte of data into the pipe. But what about the arrow keys? In the Linux console/GNOME Terminal, they generate ANSI escape codes, which, again, in raw read mode should be immediately available. But then, there are the modifier keys.

Is there any way that a bash noscript can reopen the terminal such that even stroking Alt, or Ctrl, or Shift individually can be detected?

https://redd.it/1nw4j9j
@r_bash
Space in file (Space in this filename bandit lvl2 .)
https://redd.it/1nwtofq
@r_bash
Read systemd env file

I have a systemd environment file like:

foo=bar


I want to read this into exported Bash variables.

However, the right-hand side can contain special characters like $, ", or ', and these should be used literally (just as systemd reads them).

How to do that?



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