r_bash – Telegram
Bashkit V1

The first public release of BashkitV1 is now available with the URL

https://bashkit.wuage.io

and from the main branch of the Bashkit git repository

(https://github.com/Wuageorg/bashkit).

Bashkit is the Wuage’s shell noscripting framework. It is a collection of pure bash functions along with a lightweight noscripting model that, we believe, remediate some of the Bash quirks.

Out of the box, It supports advanced yet classical logging (including NDJSON), fail fast noscripting pattern (less to none `set -e` side effects), coloured outputs and more. It can easily be extended by adding modules to accommodate various use cases.

For more information on the features of Bashkit that are new to this type of shell noscripts, see the file `doc/manual.pdf’. There is also Unix-style man pages for every released function. The man pages are the definitive denoscriptions of the framework’s features.

The framework is released under APACHE LICENSE, VERSION 2.0.

The release tar file includes the formatted documentation (pdf).

Please use https://github.com/Wuageorg/bashkit/issues to report bugs with this version. It complements at least bash 5.x and can not be used without.

Happy noscripting!

The Wuage Team

Jan. 2023

https://redd.it/10moz5g
@r_bash
Echo commands IN variable

How would I place echo commands inside a variable? I am attempting to send an email with postfix under certain circumstances. For example:

OUTPUT=$(MP4Box -info $INPUTFILE 2>&1)
HTML="<pre style=\"white-space: pre-wrap; word-break: keep-all; background-color: whitesmoke;\"><code>${OUTPUT}</code></pre>"

if [[ $(MP4Box -info $INPUT
FILE 2>&1) =~ "Incomplete" ]];
then
(
echo "From: from@email.com";
echo "To: send@email.com";
echo "Subject: Transfer Interrupted";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo -e "The mp4 transfer was interrupted and did not complete." \
"The output is as follows:\n\n" \
"${HTML}"
) | sendmail -t
elif [ $(MP4Box -info $INPUT_FILE 2>&1) =~ "Movie Info" ];
then
(
echo "From: from@email.com";
echo "To: send@email.com";
echo "Subject: Transfer complete.";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo -e "The mp4 transfer is complete." \
"The output is as follows:\n\n" \
"${HTML}"
) | sendmail -t
else
(
echo "From: from@email.com";
echo "To: send@email.com";
echo "Subject: Check on Transfer.";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";
echo -e "Something could be wrong. Manually check the mp4." \
"The output is as follows:\n\n" \
"${HTML}"
) | sendmail -t
fi

I would like to simplify this with variables. When I try, I get the postfix error No recipient addresses found in message header. For example:

OUTPUT=$(MP4Box -info $INPUTFILE 2>&1)
HTML="<pre style=\"white-space: pre-wrap; word-break: keep-all; background-color: whitesmoke;\"><code>${OUTPUT}</code></pre>"
EMAILHEADER=$(echo "From:
from@email.com";
echo "To:
send@email.com";
echo "Subject: ${SUBJECT}";
echo "Content-Type: text/html";
echo "MIME-Version: 1.0";
echo "";)

if [[ $(MP4Box -info $INPUT
FILE 2>&1) =~ "Incomplete" ]];
then
(
$EMAILHEADER
echo -e "The mp4 transfer was interrupted and did not complete." \
"The output is as follows:\n\n" \
"${HTML}"
) | sendmail -t
elif [ $(MP4Box -info $INPUT_FILE 2>&1) =~ "Movie Info" ];
then
(
$EMAILHEADER
echo -e "The mp4 transfer is complete." \
"The output is as follows:\n\n" \
"${HTML}"
) | sendmail -t
else
(
$EMAILHEADER
echo -e "Something could be wrong. Manually check the mp4." \
"The output is as follows:\n\n" \
"${HTML}"
) | sendmail -t
fi

I know I still have to define $SUBJECT and will get to that, but I at least want to get the basics of this working first.

I have one additional question... how can I break up the HTML= line (within the noscript)? It's long and I want to make it two lines to make the noscript more readable. So instead of:

HTML="<pre style=\"white-space: pre-wrap; word-break: keep-all; background-color: whitesmoke;\"><code>${OUTPUT}</code></pre>"

I want something like:

HTML="<pre style=\"white-space: pre-wrap; word-break: keep-all;" \
"background-color: whitesmoke;\"><code>${OUTPUT}</code></pre>"

But this fails for me (doesn't print out in the email).

https://redd.it/10mwah8
@r_bash
cannot run a noscript in /app directory

Trying to run a bash noscript. when i save it in my home, it can run just fine but when I move the noscript to a /app location, the noscript does not run. All permissions are ok but when i run the noscript, it does not run and also does not return an error. I also noticed that the color code I have in the noscript in my home is not appearing in when i have a noscript in the /app location. please help if you have any idea what the issue is here.

https://redd.it/10mvsvh
@r_bash
text editor in bash!

https://github.com/Agb43/text-editor.git

&#x200B;

I decided to write a terminal based text editor in bash for fun. Considering that i've never taken a coding class in my life, i'm pretty proud of it, even if inserting whitespace via blank echos is incredibly bad im sure.

https://redd.it/10n6u6m
@r_bash
Changing placeholder denoscription via sed

Hi! As an input for sed program I have smth like this: {bool some flag denoscription} (it's a part of an input string to be more precise) where:

- bool is a placeholder type
- some flag denoscription is it's denoscription

How do I replace all such placeholders to this {{some_flag_denoscription}}? I don't know how to replace spaces between 3 denoscription words with underscores. What sed technique should I use? I wanna transform some flag denoscription in a such way not to broke the whole placeholder. Another difficulty is that this placeholder can appear in any place in a command invocation example like some_executable --dark {bool whether to enable dark more} --colorize {bool whether to colorize files}. I need to know how to do this to fix my noscript.

https://redd.it/10nblyy
@r_bash
Beginner having trouble using variables as literal strings

Hello,

I'm new to Bash, and I've been hammering my way through issues with the help of ChatGPT, but it's running me in circles on this one.

I'm using Curl to make a request to Pushover's API. Many of the options that I call Curl with are exactly the same, so I thought that I'd put them in a variable to make things easier. I know that In an actual request, I would have to include more options, but I wanted to trim this example down.



po_token="ExampleToken"
po_user="ExampleUserKey"
curl_ops='-s -o "/dev/null" --connect-timeout "5" --retry "6" --retry-delay "600" -F "token=$po_token" -F "user=$po_user" "https://api.pushover.net/1/messages.json"'
curl $curl_ops



It seems that everything in my "curl_ops" variable is being passed as a single argument instead of a literal string, and I'm certain it has to do with my usage of quotes, but I have no idea how to fix it. The only noscripting language that I'm familiar with is AutoIt, and Bash seems to handle this kind of thing very differently. I was hoping someone here may have some suggestions.

Thanks

https://redd.it/10nbdv5
@r_bash
how to supress this

hi

im using psexec example PSEXEC \\\\workstation64 x:\\runme.bat


noscripts runs fine how ever i want to supress the displaying of the exit code here is the output

at exited on srv1 with error code 0.

https://redd.it/10nl7yc
@r_bash
Is there a difference between " tr a-z n-za-m " and " tr 'a-z' 'n-zam' " ?

Both provide the same output, but I'm seeing online people are recommending using " tr '[a-z\]' '[n-za-m\]' "

Why is that?

https://redd.it/10ny8a4
@r_bash
Linux bash practice site

HI, I am preparing for OSCP and hence I am learning bash.
Can someone please suggest me a best place to practice bash(For linux).
Any online link would be helpfull

https://redd.it/10o0z6g
@r_bash
What is wrong with this noscript?

Trying to grep an array of strings from a file on a remote server but the noscript does seem to do it, I might be missing quotes or the use of array is wrong here?

PARAMS=(“abc” “xyz” “ijk”)
REMOTEFILE=“pathtoFile”
NODES=(“srv1” “srv2”)

for srv in “${NODES@}”
do
ssh $srv << EOF
grep -f “${PARAMS@}” $REMOTEFILE >/path/remotefile.txt
EOF
done

https://redd.it/10nxxc0
@r_bash
Cannot backup a directory with tar. When i am trying to run the noscript it says "Cannot stat: No such file or directory". What is the problem? How to resolve it? Or maybe there is some other way to backup a directory. Please help

https://redd.it/10o3h0k
@r_bash
How can I modify this bash command that will also output the content of the given file.
https://redd.it/10o2qi5
@r_bash
What is the fastest way to read groups of N lines from stdin / a pipe?

Im trying to figure out a good, efficient and reliable way to read groups of N lines at a time from stdin or (more generally) from a pipe. Id like to accomplish this without needing to save (or wait for) the entire contents of stdin into a variable. So far the only reliable way Ive found to do this involves something like:

nLines=8 # change as needed
while true; do
outCur="$(kk=0; while (( $kk < $nLines )); do read -r </dev/stdin || break; echo "$REPLY"; done)"
[ -z $outCur ] && break || echo "$outCur"
done # | <...>

That said, if reading the entire pipe, there are some options that are much faster than read. I wrote a little speedtest to test a few:

fgg() { while read -r; do echo "$REPLY"; done | wc -l; }
fhh() { cat | wc -l; }
fii() { head -n $1 | wc -l; }
fjj() { echo "$(</dev/stdin)" | wc -l; }
fkk() { wc -l </dev/stdin; }
fll() { mapfile -t inLines </dev/stdin; printf '%s\n' "${inLines@}" | wc -l; }
fmm() { inLines="$(</dev/stdin)"; echo "${inLines}" | wc -l; }

# note: fll and fmm dont meet the "dont save the full stdin to a variable" criteria

nLines=100000

for fun in fgg fhh "fii $nLines" fjj fkk fll fmm; do
declare -f "${fun% }"
echo
time seq 1 $nLines | $fun
echo
echo '----------------------------------'
echo
done

Which, on my test system, produces

fgg ()
{
while read -r; do
echo "
$REPLY";
done | wc -l
}

100000

real 0m5.780s
user 0m3.655s
sys 0m4.306s

----------------------------------

fhh ()
{
cat | wc -l
}

100000

real 0m0.086s
user 0m0.077s
sys 0m0.028s

----------------------------------

fii ()
{
head -n $1 | wc -l
}

100000

real 0m0.085s
user 0m0.107s
sys 0m0.000s

----------------------------------

fjj ()
{
echo "$(</dev/stdin)" | wc -l
}

100000

real 0m0.124s
user 0m0.117s
sys 0m0.039s

----------------------------------

fkk ()
{
wc -l < /dev/stdin
}

1000001

real 0m0.085s
user 0m0.094s
sys 0m0.000s

----------------------------------

fll ()
{
mapfile -t inLines < /dev/stdin;
printf '%s\n' "${inLines[@]}" | wc -l
}

100000

real 0m2.241s
user 0m1.064s
sys 0m2.331s

----------------------------------

fmm ()
{
inLines="$(</dev/stdin)";
echo "${inLines}" | wc -l
}

100000

real 0m0.185s
user 0m0.180s
sys 0m0.039s

This suggests (on my test system) in a worst-case scenario (i.e., many many inputs and most of the time spend looping the `read` command), `read` is about 75x slower than optimal, loading the lines into an array is \~25x slower than optimal (+ more memory usage), and saving the full input into a (non-array) variable is \~2x slower than optimal (+ more memory usage).



From this test, using head -n in a loop seems like the obvious optimal answer, but when I try this I get....strange behavior...

seq 1 1000 | {
head -n 10
head -n 10
head -n 10
}

produces


0
1
2
3
4
5
6
7
8
9
284
285
286
287
288
289
290
291
292
293
540
541
542
543
544
545
546
547
548
549

Playing around with different sized input lines suggests that on every subsequent call after the 1st on the same pipe, head -n throws away the 1st kilobyte worth of input lines. Perhaps it is trying to "skip" reading a 1kb pipe header of some sort, but since it isnt at the start of the pipe it throws away data instead? If anyone knows how to stop head -n from doing this
please let me know. Or if there is some other quick way that I havent tested here please do suggest it.

Thanks in advance.

https://redd.it/10odhbo
@r_bash
Random Joke generator

Small bash file to tell a random joke. (100+ available!)

&#x200B;

\#!/bin/bash

\#mmartin.,29/01/23, Purpose:Print a random joke

awk '

BEGIN{ srand() }

rand() * NR < 1 {

line = $0

}

END { print line }' \~/Documents/VariousTexts/jokes.txt

exit

&#x200B;

Download from my github

github.com/myklmar/noscripts with jokes.txt file.

https://redd.it/10oc0a4
@r_bash