r_bash – Telegram
what is the best bash version to keep noscripts in?

now i myself prefer the last bash version, which is supported in my environment (bash 5.2.15 as for Debian 12), but i'm also testing 5.3 and it got me thinking, which version is the best to write noscripts in

the latest one for perfomance and features
OR
the oldest popular one for support (e.g. 3.4)


https://redd.it/1lyeyv8
@r_bash
When in vi mode, how to start off in command mode?

Okay, so I've been using Bash's vi mode for two decades. One thing I should have inquired about from the start, is this: is it possible to make command mode default, instead of insert mode? I notice that for nearly every command the first thing I do is hit <ESC> to go into command mode, so that I can go up and down the command-history list using <k> and <j>. It'd be great if command mode could somehow be made the default.

Thanks all.

https://redd.it/1lxw2qo
@r_bash
noscript kind of works

For a class I'm taking I need to write a bash noscript that takes two arguments a number and a word. The noscript should create a number of files equal to the number entered and the file should be named (word argument)_(number). the number needs to be at least 2 digits so 9 should be 09.

I've written a noscript that works most of the time, except when the number is 8 or 9. it works for 7 and down, and also from all numbers above 9 until it reaches 9. It also doesn't change if the input is 09 or 9


Here the noscript:

>\#!/bin/bash
\#create variables
NUM=$1
NAME=$2
\#print variables
echo $NUM
echo $NAME
\#make directory to hold files
mkdir $NAME"_Files"
\#make files and place them in directory
while [ $NUM -ge 1 \]; do
if [ "${#NUM}" -le 1 \];then
NUM="0"$NUM
fi
touch $NAME"_Files"/$NAME"_"$NUM
NUM=$((NUM-1))
done

Any help would be appreciated

https://redd.it/1lyv4xy
@r_bash
Using both subcommands and getopts short options? Tips

I have a wrapper noscript where I first used short options with getopts because my priority is typing as little as possible on the CLI. Then I realized some options need more than one required argument, so I need to use subcommands.

How to use both? It probably makes sense to use ./noscript [sub-command] with different short options associated with specific subcommands, so I need to implement getopts for each sub-command or is there an easier or less involved way? I'm thinking I need to refactor the whole noscript to try to reduce as much short options that are only specific to a subcommand as much as possible so that for argument parsing, I first loop through the arguments stopping when it sees one that starts with a - where processed ones are treated as subcommands, then process the rest with getopts. Then for subcommands that take unique short options, use getopts for that subcommands?

Any suggestions are much appreciated, I don't want to make this a maintenance nightmare so want to make sure I get it right.

https://redd.it/1m01rv2
@r_bash
One-encryption

Hi, I was learning some bash noscripting, but then I had a doubt, like, I know how to encrypt and decrypt with openssl:

# Encrypt
echo "secret" | openssl enc -aes-256-cbc -md sha512 -a -pbkdf2 -iter 100000 -salt -pass pass:somePASSWD
# Decrypt
echo "<HASH> | openssl enc -d -aes-256-cbc -md sha512 -a -pbkdf2 -iter 100000 -salt -pass pass:somePASSWD

But that's not what I want now, I'm looking for a one-way encryption method, a way that only encrypts the data and the result is to verify if the user input matches the encrypted information(probably using a if statement for the verification). Example:

#!/usr/bin/env bash

ORIGINAL=$(echo "sponge-bob" | one-way-encrypt-command)

read -rp "What is the secret?" ANSWER
if "$(echo $ANSWER | one-way-encrypt-command)" = "$ORIGINAL" ; then
echo "Yes you're right!"
else
echo "Wrong!"
fi

https://redd.it/1m01yan
@r_bash
Bash 5.3 - first 'huh?' moment.

Hello.

Trying out some of the new features in bash 5.3, and have come across my first 'huh?' moment.

% export TEST=aaabbb
%
% echo $( sed 's/a/b/g' <<< $TEST ; )
bbbbbb

% echo ${ sed 's/a/b/g' <<< $TEST ; }
sed: couldn't flush stdout: Device not configured

% echo ${| sed 's/a/b/g' <<< $TEST ; }
bbbbbb

Can anyone explain why the 2nd version doesn't work?

Thanks

fb.

https://redd.it/1m0i8um
@r_bash
you guys could really like this simple function!!!!

maybe i'm a really really specific kind of user but sometimes i genuinely forget whether i wanna go to a directory or a file

if you use bash completions as a file manager, you could also replace $EDITOR with $PAGER

c() {
if -f "$1" ; then
"${EDITOR:-vi}" "$1"
else
cd "${1:-$HOME}"
fi
}

https://redd.it/1m0tn71
@r_bash
Handling bash settings across distros

Recently I have started keeping track of my dotfiles as I work with more and more machines, I thought it appropriate to start tracking them and syncing them across my machines. Simple enough.

However, bash is proving to be specially hard to do this with. Most of my dotfiles are programs I install and configure from scratch (or at least parting from virtually identical defaults), however, with bash, I have to worry about profiles, system configs differing across distros, etc...

Basically, I have 3 machines, one is on Fedora, another is on Tumbleweed and another is on Debian. Each of these is doing COMPLETELY different things in /etc/bash.bashrc or /etc/bashrc and the default .bashrc is also doing completely different things. And that is without even considering profile files and other files like .bash_logout and such.

How can I sync my .bashrc files without having to manually manage system files in each system (and any potential future system). Or simply, how have you solved this issue for your own setup? Do I just sync whatever I create and disregard system configs? Any advice?

https://redd.it/1m1dfpx
@r_bash
I dare you to hack my secure login system ;)

Note: Run with $ ./login.sh, not $ sh `login.sh`, or else it will not run with bash and will break. But you probably already knew that.

#!/bin/bash
clear
echo "CC0 1.0 (https://creativecommons.org/publicdomain/zero/1.0/)"
echo "########################"
echo "# #"
echo "# Login Terminal #"
echo "# #"
echo "########################"
echo ""
select item in "Login" "Register"; do
case $REPLY in
1)
clear
echo "LOGIN"
echo "#####"
echo ""
echo -n "Username: "
read uname
if test -d "$uname"; then
cd $uname
echo -n "Password: "
read -s pwd
pwdsm=$(< pwd)
pwdsu=$(echo $pwd | sha256sum)
if $(echo $pwdsm | cut -f 1 -d " ") = $(echo $pwdsu | cut -f 1 -d " ") ; then
clear
echo Login Succesful!
break
else
echo Error: Incorrect password.
break
fi
else
echo Error: Incorrect Username.
break
fi
;;
2)
clear
echo "REGESTER"
echo "########"
echo ""
echo -n "Username: "
read uname
mkdir $uname
cd $uname
echo -n "Password: "
read -s pwd
echo $pwd | sha256sum > pwd
echo ""
break
;;
esac
done

https://redd.it/1m2eumm
@r_bash
imagemagick use image from clipboard

#!/bin/bash

DIR="$HOME/Pictures/Screenshots"
FILE="Screenshot_$(date +'%Y%m%d-%H%M%S').png"

gnome-screenshot -w -f "$DIR/$FILE" &&
magick "$DIR/$FILE" -fuzz 50% -trim +repage "$DIR/$FILE" &&
xclip -selection clipboard -t image/png -i "$DIR/$FILE"
notify-send "Screenshot saved as $FILE."


This currently creates a file, then modifies it, saves it as the same name (replacing)

I was wondering if it's possible to make magick use clipboard image instead of file. That way I can use --clipboard with gnome-screenshot. So I don't have to write file twice.

Can it be done? (I am sorry if I am not supposed to post this here)

https://redd.it/1m2v043
@r_bash
In hunt of productivity tools in bash (to list in devreal.org)

Modern software development feels like chasing smoke, frameworks rise and fall, GUIs shift faster than we can learn them, and the tools we depend on are often opaque, bloated, or short-lived.

I think the terminal is where real development will happen. The long-lasting inventions on how to work with the computer will be made in the terminal. Now even more, with AI, it is easier for an agent to execute a command than to click buttons to do a task.

*I am creating a list productivity applications* in "devreal.org". Do you know of any applications that meet the criteria? Do you have any good idea to add to the project?

* [https://devreal.org](https://devreal.org)

https://redd.it/1m2y6br
@r_bash
Ncat with -e

Hi all


I have used **netcat (nc)** in the past,
and then switched to **ncat**, which is newer, has more features,
and was created by the person who also created nmap.

I wrote this command for a simple server that runs a noscript file per every client that connects to it:

ncat -l 5000 -k -e 'server_noscript'

The `server_noscript` file contains this code:

read Line
echo 'You entered: '$Line

and to connect, the client code is:

ncat localhost 5000


It works good, but has a small problem:

After I connect as a client to the server and then enter a line,
the line is displayed back to me, by the `echo 'You entered: '$Line` command, as expected,
but the connection is not closed, as it should.
(the `server_noscript` file ends after that line)

Instead,
I can press another [Enter], and nothing happens,
and then I can press another [Enter], which then displays (on the client side) **"Ncat: Broken pipe."**,
and then the connection is finally closed.

See it in this screenshot:

https://i.ibb.co/84DPTrcD/Ncat.png


Can you please tell me what I should do in order to make the Server disconnect the client
right after the server noscript ends?


Thank you

https://redd.it/1m37kxa
@r_bash
Bash one liner website

Sorry for the weird post. I remember visiting a website in the early 2010s which was a bit like twitter, but for bash one liners. It was literally just a feed of one liners, some useful, some not, some outright dangerous.

I can't for the life of me remember the name of it. Does it ring a bell for anyone?

https://redd.it/1m3dgc8
@r_bash
How to make "unique" sourcing work?

(Maybe it works already and my expectation and how it actually works don't match up...)

I have a collection of noscripts that has grown over time. When some things started to get repetitive, I moved them to a separate file (base.sh). To be clever, I tried to make the inclusion / source of base.sh "unique", e.g.if

`A.sh` sources `base.sh`
B.sh sources base.sh AND A.sh

B.sh should have sourced base.sh only once (via A.sh).

The guard for sourcing (in base.sh) is [ -n ${__BASE_sh__} ] && return || __BASE_sh__=.

While this seems to work, I now have another problem:

`foobar.sh` sources `base.sh`
main.sh sources base.sh and calls foobar.sh

Now foobar.sh knows nothing about base.sh and fails...

# Update

It seems the issue is my assumption that [ -n ${__BASE_sh__} ] and [ ! -z ${__BASE_sh__} ] would be same is wrong. They are NOT.

The solution is to use [ ! -z ${__BASE_sh__} ] and the noscripts work as expected.

\--------------------------------------------------------------------------

# base.sh

#!/usr/bin/env bash

# prevent multiple inclusion
-n ${__BASE_sh__} && return || BASEsh=.

function errcho() {
# write to stderr with red-colored "ERROR:" prefix
# using printf as "echo" might just print the special sequence instead of "executing" it
>&2 printf "\e[31mERROR:\e[0m "
>&2 echo -e "${@}"
}

# `foobar.sh`

#!/usr/bin/env bash

SCRIPT
PATH=$(readlink -f "$0")
SCRIPTNAME=$(basename "${SCRIPTPATH}")
SCRIPTDIR=$(dirname "${SCRIPTPATH}")

source "${SCRIPTDIR}/base.sh"

errcho "Gotcha!!!"

# `main.sh`

#!/usr/bin/env bash

SCRIPT
PATH=$(readlink -f "$0")
SCRIPTNAME=$(basename "${SCRIPTPATH}")
SCRIPTDIR=$(dirname "${SCRIPTPATH}")

source "${SCRIPTDIR}/base.sh"

"${SCRIPT
DIR}/foobar.sh"

# Result

❯ ./main.sh
foobar.sh: line 9: errcho: command not found

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