r_bash – Telegram
How to repeat a command until it has a non-empty output

For example, if I want to scan my vm until it appears with something like...


sudo nping --tcp -p 22 -c 1 192.168.1.2 | grep "RCVD" | cut -d " " -f 4 | cut -d ":" -f 1

which pings once on the port and the grep and cuts just prunes everything except the host IP if indeed it responds to the ping.


How can I write this so that it will run my one liner until it produces anything other than an empty output, and then output that output normally for further noscripting?


Any help is appreciated, I've tried a fair bit of looking around, I feel like the test [\] feature or a "while true" loop might be useful in my solution, I'm just unsure of the test operators which isn't helped at all by the fact that I still quite haven't sorted out how to articulate this in a noscript quite yet as you can tell. TIA

https://redd.it/z980am
@r_bash
sshto

Tired of opening new sshto tabs for each host? It's not an issue anymore!)
Added two cool commands to sshto Add tabs and Ssh tabs for single host and group commands list.
The first one will add tab to your terminal running sshto with selected host. Or multiple tabs for all hosts in group.
The second one will add tab to your terminal running SSH session to selected host. Or multiple tabs for all hosts in group.

https://preview.redd.it/uei8wr87y83a1.png?width=680&format=png&auto=webp&s=3d8f62c3f86ebc001f7dbcfaab66819f95173f0e

Enjoy)

https://redd.it/z9invz
@r_bash
Message won't show

Hi I'm trying to learn how to assign a message to a variable, but I'm a little confused, I have this:

1 #!/usr/bin/env bash
2
3 ( -f first.txt && -f second.txt && rm first.txt &&
4 DELETE="File deleted") ||
5 DELETE="File not found"
6 echo "${DELETE}"

The second message works once first.txt has been deleted, but after creating another "first.txt" it checks it and deletes it, but no message shows and I can't work out why, I am expecting the message "File deleted", but I'm just getting a blank space instead. What do I have to do to get the first message to actually display?

for clarity the exercise it to set a message to a variable so that I can compile an email alert, doing echo "file deleted" instead of the attempted variable assignment works, but it is the variable assignment I am trying to learn.

https://redd.it/z9m9nq
@r_bash
Change specific line in a file

How do i change a specific line in a file with bash? i just have to set a false value to true in a file.

https://redd.it/z9s0b7
@r_bash
What is the best way to use the trap statement to run a command on ANY exit from the noscript no matter what the condition?

Basically I want to do this..

trap 'rm -r somefile' (what do I put here?)

I want the part between the single quotes to run no matter how the noscript exits... normal end, error in the noscript, control-C, some kind of internal error, whatever... I just want to make absolutely certain a file gets removed when the noscript ends. But I am so confused about what options I should use at the end. I see some places where people use "ERR EXIT" but others where it shows several numbers. I wish there were an "ANY REASON" option but if there is, I haven't stumbled across it yet. What should I be using here?

https://redd.it/z9y1cl
@r_bash
How to Assign Parallel Outputs to Specific Line

Hi, apologies if the noscript isn't clear, I'm having a hard time phrasing this question, hence the reason for this post.

Let's imagine I have a noscript, `parallel.sh`, that spawns a couple of long-running parallel jobs, expensive

#!/bin/bash
# expensive.sh

./expensive.sh 5 & ./expensive.sh 8 & ./expensive.sh 10 &
wait

expensive.sh counts up to the argument provided on a 500ms interval.

#!/bin/bash
# expensive.sh

max=$1

function clearline() {
echo -en "\r\033[0K"
}

echo -n "Counting to $max:"

for i in $(seq 1 $max);
do
echo -n " $i"
sleep 0.5
done

clear
line
echo " Counted to $max"

The output of ./expensive.sh 5 looks like the following:

https://reddit.com/link/za3ym1/video/e3hcqzfyed3a1/player

When running `./parallel.sh` I get the following output:

​

https://reddit.com/link/za3ym1/video/ux2b5jalfd3a1/player

Obviously, that output is terrible.

I'd like for each process to have its own line, so the outputs do not effect each other.

Here is what I'd like the output to look like at various counts:

# At count 3
Counting to 3: 1 2 3
Counting to 8: 1 2 3 4 5 6
Counting to 10: 1 2 3 4 5 6

# At count 6
Counted to 5
Counting to 8: 1 2 3 4 5 6
Counting to 10: 1 2 3 4 5 6

# At finish
Counted to 5
Counted to 8
Counted to 10

I have a feeling this may be just a tricky problem with escape sequences, but I'm wondering if there is an easier way.

https://redd.it/za3ym1
@r_bash
Is this messy?

awk '/VM_pool|<name>/ { gsub (/<|>|\\047/, " "); print $(NF-1) }' $path

https://redd.it/za5qfv
@r_bash
explain this please ..
https://redd.it/zad6e2
@r_bash
Question

Hello. Super super noob to BASH here. Trying to run some basic noscript and confused on my return.

#!/bin/bash

echo "What is your word?"

read readinput

if [[$readinput == "coffee"]]; then

echo "This is correct"

else

echo "This is incorrect"

fi

Confused on why I'm receiving:

./test.sh: line 6: [[coffee: command not found

This is incorrect

Even if I type "coffee" as the input. Could someone explain why this is my return please. For reference, the start of the if statement is line 6.

https://redd.it/zaf4wy
@r_bash
Pyenv / Python doesn't launch when outside of home directory

Unless I specify the full path, Python will only launch in home.

Home:

$ python
Python 3.9.6 (default, Jul 14 2021, 17:03:23)
GCC 5.4.0 20160609 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

OK, let's try it in a directory:

$ cd example
$ python
bash: .pyenv/shims/python: No such file or directory

OK, so Python must be defined relative to the home directory. That's why it doesn't launch. Let's check:

$ which python
/home/me/.pyenv/shims/python

Nope, so it's got the full path to the executable. Does it launch if I call it that way?

$ /home/me/.pyenv/shims/python
Python 3.9.6 (default, Jul 14 2021, 17:03:23)
GCC 5.4.0 20160609 on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Yep. So, what's going wrong here?

https://redd.it/zaiu2n
@r_bash
Want to output command in if statement comparation

Given this line:

if $( echo $? ) -eq 0 ; then echo true; else echo false; fi


I want to print to terminal the execution of "echo $?" as well as the true or false result.

I tried this and it does the job but then the comparison operator fails as it expects one element.

if $( echo $? >/dev/tty) -eq 0 ; then echo true; else echo "false"; fi

0
-bash: : -eq: unary operator expected
false


so any idea about how to solve this?

[https://redd.it/zak19i

@r_bash
Trying to replace a string inside a javanoscript file with sed

Hello all,

I have a pretty big javanoscript file, that is being generated on build, and I want to change one of the string parameters depending on the OS, but for some reason I am having some difficulty figuring it out. I am running this .sh file after my build command, so first the javanoscript file is being generated and then I am trying to change that string. Here is the little noscript I am trying to run:



case "$OSTYPE" in
darwin*) echo "OSX" ;;
linux*) echo "LINUX" ;;
msys*)
echo "WINDOWS"
cd someDir
  old="..someOldString"
  new="..someNewString"
  sed -i "s/${old}/${new}/g" index.js
  ;;
*) echo "unknown: $OSTYPE" ;;
esac

&#x200B;

Any idea what I am doing wrong?

Thank you

https://redd.it/zaswnz
@r_bash
Help improve my noscript

Hi there,

Intro:

fairly new to shell noscripting and I came up with this (after many searches and much rtfming) noscript to automate my openconnect vpn server selection and connecting. Basically what it does is that it pings each server and the using awk take out the average speed of each server then tests to see which server is faster and connects to it.

Question:

I dont think I did such a good job in that if statement and overall I think the noscript is kinda nasty.
How can I make it better and more professional?
Should I have used a fore loop? (cause I couldnt make it work)

Thanks to everyone who spends their time reading and helping a newbie :)

The Script:
#!/usr/bin/env bash

# Test the ping of kerio servers and select the one with the lowest ping

# VARs
SERV1=$(ping -c 4 c1.kmak.us | tail -1| awk -F '/' '{print $5}')
SERV2=$(ping -c 4 c2.kmak.us | tail -1| awk -F '/' '{print $5}')
SERV3=$(ping -c 4 c3.kmak.us | tail -1| awk -F '/' '{print $5}')
SERV4=$(ping -c 4 hdk.csw5.kmak.xyz | tail -1| awk -F '/' '{print $5}')
SERV5=$(ping -c 4 sja.c24.kmak.xyz | tail -1| awk -F '/' '{print $5}')
SERV6=$(ping -c 4 wen.c25.kmak.xyz | tail -1| awk -F '/' '{print $5}')

# Connecting VARs
CMD1=$(echo "password" | doas openconnect c1.kmak.us:443 --user=xm --passwd-on-stdin)
CMD2=$(echo "password" | doas openconnect c2.kmak.us:443 --user=xm --passwd-on-stdin)
CMD3=$(echo "password" | doas openconnect c3.kmak.us:443 --user=xm --passwd-on-stdin)
CMD4=$(echo "password" | doas openconnect hdk.csw5.kmak.xyz --user=xm --passwd-on-stdin)
CMD5=$(echo "password" | doas openconnect sja.c24.kmak.xyz --user=xm --passwd-on-stdin)
CMD6=$(echo "password" | doas openconnect wen.c25.kmak.xyz --user=xm --passwd-on-stdin)

# Test
if (( $(echo "$SERV1 < $SERV2" | bc -l) )) && (( $(echo "$SERV1 < $SERV3" | bc -l) )); then
echo "$CMD1"
elif (( $(echo "$SERV2 < $SERV1" | bc -l) )) && (( $(echo "$SERV2 < $SERV3" | bc -l) )); then
echo "$CMD2"
elif (( $(echo "$SERV4 < $SERV5" | bc -l) )) && (( $(echo "$SERV4 < $SERV6" | bc -l) )); then
echo "$CMD4"
elif (( $(echo "$SERV5 < $SERV4" | bc -l) )) && (( $(echo "$SERV5 < $SERV6" | bc -l) )); then
echo "$CMD5"
elif (( $(echo "$SERV3 < $SERV6" | bc -l) )); then
echo "$CMD3"
elif (( $(echo "$SERV6 < $SERV3" | bc -l) )); then
echo "$CMD6"
else
echo "failiure in server resolve"
fi

PS: It works but I still am not so sure if the test is done right

https://redd.it/zaxkm4
@r_bash
Quite the promising noscript
https://redd.it/zb04ny
@r_bash
Why trailing space always present after running my function?

Hello! I have the following function:

parse_see_also_links() {
declare line="$1"

echo "$line" |\
sed --regexp-extended "s/^> See also: *(.+) *\.\$/[\1]/; s/ +/ /g;"
}

For `parse_see_also_links "> See also: vim emacs ."` I wanna get `[vim emacs]` and not `[vim emacs ]`. How to fix code not to get the last space?

https://redd.it/zb7j17
@r_bash
Advent of Code

Anyone else doing Advent of Code in BASH? I just finished day 3 with pure BASH. And I'm looking for more coding buddies to compare code, learn, challenge each other, share/contribute to other's projects or just bs. I have a small Discord channel with a group of programming buddies, where our goal is the above. A place to learn, share, compete, grow, etc. Join the Discord if you're interested — Open Source Force

https://redd.it/zbbshf
@r_bash
Help with regex, I think?

Here is what I'm working with so far:

#!/bin/bash

# 1942_stu_ovl.png
# 1942_udb_ovl.png
# 1943_stu_ovl.png
# 19xx_bp_ovl.png
# 2020bb_bp_ovl.png
# 3wonders_bp_ovl.png
# aerofgt_bp_ovl.png
# airbustr_bp_ovl.png
# aliensec_bp_ovl.png
# aliens_udb23_nono6493_edit_ovl.png

games=()

while read -r png; do
game="$(basename "${png%%_*}")"
# [[ "$game" =~ ${games[*]} ]] || \
games+=("$game")
done < <(find . -type f -name "*.png" | sort -u)

echo "${games[@]}"


If I run this as-is, I get output with the duplicate games listed twice (1942 1942 1943 (...) xybots zaxxon zaxxon). If I uncomment the [[ "$game" =~ ${games[*]} ]] || \ line, I get no output at all. What I want is each game listed once (1942 1943 19xx (...) xevious xybots zaxxon).

Ultimately I'm trying to build .cfg files with one-or-more .png's defined for each one, but right now I'm still trying to get a unique list of game names.

The ending config files would look such as -- zaxxon_ovl.cfg:

overlays = 3

overlay0_overlay = "./zaxxon_bp_ovl.png"
overlay0_full_screen = true
overlay0_descs = 0

overlay1_overlay = "./zaxxon_stu_ovl.png"
overlay1_full_screen = true
overlay1_descs = 0

overlay2_overlay = "./zaxxon_udb_ovl.png"
overlay2_full_screen = true
overlay2_descs = 0


The only thing it needs to do is count the PNG's and list them -- the full_screen and descs values are always constant. Any ideas?

https://redd.it/zbbn0k
@r_bash
what this shell command mean: wait $${!} ?

Hello, I found this line of shell noscript in a docker-compose file.

/bin/sh -c 'while :; do sleep 36000 & wait $${!}; nginx -s reload; done & nginx -g \\"daemon off;\\"'

Could you guys explain me what it does? I know it overrides the default CMD used in the Nginx Dockerfile to start the Nginx server and I know its purpose is to wait for another service called "certbot" that generates and signs certicates thanks to Letsencrypt and saves these certs in a shared folder /etc/certache/certs. But I dont see how ? especially I dont understand those characters : $${!}

The docker-compose file in question:
/////////
web:
image: nginx:1.23
container_name: nginx
restart: always
ports:
- 80:8080
- 443:8443
volumes:
- ./nginx-templates:/etc/nginx/templates
- ./certcache/certs:/etc/certcache/certs:ro
environment:
- NGINX_HOST=${DOMAIN_NAME}
- PMA_HOST=phpmyadmin
command: "/bin/sh -c 'while :; do sleep 36000 & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
depends_on:
- phpmyadmin
- certcache
//////

Thanks you !

https://redd.it/zbj15f
@r_bash
Need help with parameter expansions in my CLI tool.

I'm trying to debug my CLI tool ([https://github.com/membersincewayback/gen](https://github.com/membersincewayback/gen)) and address unset variables, however I just realized one of my parameter expansions isn't working how I thought it was supposed to as below:

#!/usr/bin/env bash
FILENAME="${!#}"
EXT="${FILENAME##*.}"

The intention is to set `FILENAME` to the last argument (working) and `EXT` to anything following the final "." (not working as expected). The problem is, if there is no "." it sets `EXT` == `FILENAME` which has undesired effects on the rest of the noscript. (i.e. creates possible conflict if `FILENAME` matches an existing template.)

Any help would be appreciated!

https://redd.it/zbjpo5
@r_bash
making menu: some doubts about the best way I'm doing it. Am I right? Is there a better way to do this?

Hi,

I'm making a noscript with a big menù for config my homeserver (see the code at the end of post)... now I'm questioning myself if there is a better way to write the menu. Questions are:

1. the `INITIAL_MANDATORY_SERVER_CONFIG_MENU` list, could be written in a different place, for example in the constraints part of the noscript, at the beginning? It seems no..
2. `INITIAL_MANDATORY_SERVER_CONFIG_MENU` could be written in a different way, for example `INITIAL_MANDATORY_SERVER_CONFIG_MENU=( "INIZIALIZZA" "INSTALL AND UPGRADE STABLE SOURCES" ...)` ? It seems no...
3. Otherwise, should I organize the menu in a complete different way?

thanks for any advice.

&#x200B;

&#x200B;

INITIAL_MANDATORY_SERVER_CONFIG_MENU=(
"INIZIALIZZA"
"INSTALL AND UPGRADE STABLE SOURCES"
"INSTALL NEW STABLE SOFTWARES"
"INSTALL DOCKER + DOCKER-COMPOSE"
"CREATE USERS"
"CONFIG WIREGUARD ADMIN"
"CONFIG WIREGUARD HOME"
"CONFIG WIREGUARD PARENTS"
"CONFIG SERVER RSYNC"
"CONFIG CRON JOBS"
"CONFIG NetworkUpsTools"
"CONFIG COCKPIT"
"RACKNERD VPS MANAGEMENT SCRIPTS"
"CREATE SWAPFILE"
"LAST GLOBAL CONFIGs"
)
PS3='CHOOSE INITIAL_MANDATORY_SERVER_CONFIG_MENU_VOICE: '
select INITIAL_MANDATORY_SERVER_CONFIG_MENU_VOICE in "${INITIAL_MANDATORY_SERVER_CONFIG_MENU[@]}"
do
case $INITIAL_MANDATORY_SERVER_CONFIG_MENU_VOICE in

https://redd.it/zc775q
@r_bash