r_bash – Telegram
duplicate a telnet entry

I am trying to replicate something simple like this

telnet IP
#011000 (typed in manually)

I hit the enter key and the command executes

when I try to duplicate that

echo '#011000' | netcat -N IP

this does NOT work. I'm guessing because of the carriage return isn't being sent.

What am I missing to get this to work. I'm guessing the echo isn't sending an "enter" key at the end

I also tried

echo '#011000\r' | netcat -N IP

but that didn't work either.

What am I missing?

https://redd.it/1bokgip
@r_bash
Help with variables

Hi all. I am not a programmer though I have some basics of coding from some years ago. I tried writing a noscript for my computer (Linux) mostly for fun.
I don't know much of bash, but since the noscript was overall easy, I only read the very basics.
The idea of the noscript is to launch some games with MelonDS or mgba.
The command for melonDS is "melonDS path/of/the game".

The noscript:
- lists the games
-asks for input for the name of the game and stores the name in a variable (a)
- if the last three letters are "nds" then executes the command "melonDS path/$a" (where a is the name of the game)
- haven't written the rest yet

The problem is that I don't understand what I might be doing wrong, because the final result is indeed "melonDS /path/of/the game" but melonDS just launches without the actual game. When I run the exact same command from terminal, it works.
Sorry, if this is maybe a noob mistake, that's my real first noscript.

Update:code

Update 2: I noticed that the "d" variable part was wrong. Thanks for pointing out!
Now, I noticed that the name and path is not stored in the correct way. There are the '\' signs, so the final command comes out wrong.
picture

https://redd.it/1bpc37r
@r_bash
Validating input and adding a prefix before executing ansible playbook

I am creating a bash noscript that runs an ansible playbook. So far I have

cd /path/to/playbook
python3 inventory_updater.py

# Check if there are no errors from executing python noscript.
[ $? -eq 0 ] # Is this an appropriate way to check with an if condition and exit code ?

read -p "Enter store numbers separated by commas (e.g., 22345,28750): " store_numbers
ansible-playbook update_specific_stores.yml -e "target_hostnames=$store_numbers"



The targethostnames variable accepts one or more store number. I need to do the following:

1)Make sure that each store number is five digits long and starts with number 2

Something like `$store
number =~ ^20-9{4}$. Prompt user if incorrect.


while [ ! $store_number =~ ^2[0-9{4}$ ]]; do
read -p "Store number must be five digits starting with 2. Please enter a valid store number: " storenumber
done
```
The above validates for one user input but I don't know how to validate for several inputs separated by comma.

2) Add a "store" prefix before each store number. So it would be like store22345

I am expecting the value of "target
hostnames=store22345,store28750" and so on. One input is minimum.

https://redd.it/1bpeuxc
@r_bash
What is the role of bash noscript in Machine Learning?

This is the requirement of a ML intern. Can anyone tell me what is the use cases of bash noscript in ML field?

Thanks in Advance
Qualifications

A bachelor’s, master's, or PhD (ongoing or complete) degree or equivalent from a top university, available to join for an in-office Summer Internship from May 2024.
Prior experience with training, building, and deploying models via Tensorflow/Pytorch (or similar) is mandatory.
Experience with CI/CD pipelines and MLOps for automating model deployments.
Skilled in using Linux, SQL, Git, and BASH noscripting.
Strong knowledge of Python and hands-on.

https://redd.it/1bpnvp0
@r_bash
why does /bin/bash -c 'ssh user@host' work?

I expected keyboard input for ssh running in a child shell to break, and that I would have to do something fancy with wiring the child process input/output to the parent shell's tty, but to my surprise this worked without any fanciness required? Would someone be able to explain what is happening at a low level that enables this to work? Thank you!

https://redd.it/1bptn6k
@r_bash
TIL: not all line continuations are the same

An unquoted slash `\` can be used to continue some command onto the next line:

$ echo abc \
> def
abc def

Both `||` and `&&` act like that:

$ echo abc &&
> echo def
abc
def
$ ! echo 123 ||
> echo 345
123
345

But there is something more to the last two: you can put multiple newlines OR comments in-between:

$ echo abc &&
>
> # some
> # comment
>
> echo def
abc
def

Or in a more practical code:

[[ $repack_early3 == n ]] ||
# The symlink is no longer needed
rm "$initrd_main/lib/modules/$kernel/kernel"

https://redd.it/1bq0enr
@r_bash
command help

I want to use a simple one-liner something like this:


whois $domainname | grep "Expir" | xargs $domainname = example.com

Something I can quickly change (or make into an useful alias).
I'm on the hunt for a new domain name, and want to avoid the long wall of text that comes from each whois search.


Is this possible without making it complicated and using noscript files etc?

https://redd.it/1bq8wii
@r_bash
Bash noscript help to launch docker container

I’m running Debian 12 and I have a Windows 11 docker container that I use with FreeRDP. I would like to have an “app” for lack of a better term that when clicked would start the windows container and start FreeRDP. I’ve written an easy little bash noscript that starts both the container and FreeRDP. I’m running into trouble checking to see if the windows container is already running. If it is, then just launch FreeRDP. If not, launch both.

I also need to have the noscript automatically stop the Windows container when I close out of FreeRDP.

Anyone have any ideas about the best way to do this?

https://redd.it/1bqbad2
@r_bash
Help with quotes and variables

Hi all, I am new to bash. I have a problem to which I haven't found a solution yet. I am starting to learn right now, so forgive me if my knowledge of bash isn't the best at all.

What I want to achieve is this output:

command $d


Where "command" is a command and $d is a variable. The variable d is based on another variable, called a.
It's something like this: d="/home/name/$a".

The problem is that variable a contains single quotes, like this: a='name of the file'

So, as you can guess the idea is to launch a command with the path of the file. However, the single quotes make it very difficult because when under the double quotes ("") are interpreted differently. How can I overcome this problem?


https://redd.it/1bqlxcj
@r_bash
My BASH game: bashBlitz.
https://redd.it/1br6a1h
@r_bash
So what is your bash coding environment?

It's a quiet Saturday on the subreddit, so I was curious.

My setup is:

Neovim → conquer of completion (coc.nvim)coc-sh Current version 1.2.2

I do want to learn lua to set up those nvim language server protocol to skip coc. Just not that good on learning new things.

I do not have any other bash related plugins. Ok, I use nerdtree to navigate sourced files, but I don't think that counts.

My PS4='•\[\e[1;33m\] ${BASH_SOURCE[0]##*/} ${LINENO}\[\e[m\] '

So I usually have the code in my right pane and the execution of the code with bash -x on the left, to see where I messed up.

Any workflow, cool plugins you might want to share?

https://redd.it/1brm1iy
@r_bash
Building Signal for RaspberryPi. Look over the start of this noscript?

Using THIS blog post as a start I put this together. This will build but some things are hard coded and Id like to "detect" more. Like pulling a "stable" branch for Signal or always installing the latest nvm. I'm also struggling to use sed correctly to edit the packages.json file to add the "AppImage" part.

Any help is welcome. Hope theres a few folks out there that want this.

​

​

#!/bin/bash
## This noscript based on Andrea Fortunas blog post here: https://andreafortuna.org/2019/03/27/how-to-build-signal-desktop-on-linux/
## NVH Homepage: https://github.com/nvm-sh/nvm
## Signal-Desktop releases: https://github.com/signalapp/Signal-Desktop/releases


sudo apt install build-essential -y
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.39.7/install.sh | bash
git clone https://github.com/signalapp/Signal-Desktop.git --branch v7.4.0-beta.2 .local/share/Signal-Desktop ; exit

####EXIT TERMINAL HERE (i think to reset bash enviornment)

cd ~/.local/share/Signal-Desktop/

#sed -i 's/original/new/g' package.json

nvm use ; nvm install 20.9.0 ; npm install --global yarn ; yarn install --frozen-lockfile ; yarn generate ; yarn build-release

mkdir ~/.local/share/applications

cat > ~/.local/share/applications/signal-desktop.desktop <<EOF

Desktop Entry
Name=Signal
Exec=env LANGUAGE=tlh ~/.local/share/Signal-Desktop/release/linux-arm64-unpacked/signal-desktop --no-sandbox %U
Terminal=false
Type=Application
Icon=signal-desktop
StartupWMClass=Signal
Comment=Private messaging from your desktop
MimeType=x-scheme-handler/sgnl;x-scheme-handler/signalcaptcha;
Categories=Network;InstantMessaging;Chat;
EOF


##REMOVAL
#rm -R ~/.local/share/applications/signal-desktop.desktop ~/.local/share/Signal-Desktop ~/.nvm ##.config/Signal/

&#x200B;

Thanx

https://redd.it/1bru7yk
@r_bash
Printf -v var

Works


printf -v var “%s” “hi”
echo “$var”



Works


printf “%s” “hi” | tee somefile
cat somefile


Why this one doesnt work?


printf -v var “%s” “hi” | tee somefile
echo “$var” # var is empty
cat somefile # file is empty




https://redd.it/1brz5a9
@r_bash
Notifies me through dunst, when volume changes.

It would be nice, if someone could help me, because I don't know a lot about noscripting.

I think this problem is not too difficult.

I want to use wireplumber to show me the volume level in percent through

"wpctl get-volume "@DEFAULT_AUDIO_SINK@".

And I want the message to get displayed though the notifier dunst.

https://redd.it/1bs6iec
@r_bash
Duplicated HTTPD Log parsing output issue.

I've written a bash noscript that works almost perfectly but I am having an issue with the output. The issue is that each of the data for a filename is being duplicated.

The purpose of the noscript is to parse all of the Apache2 httpd log files in a directory and to count how many times an IP address accesses wp-login.php. It then should output the file name and Ip counts below the filename. While this is working, the output is duplicated for each filename. I've tried so many ways to fix this, but I'm pretty sure it's beyond my skillset. Here is the noscript:

&#x200B;

#!/bin/bash

&#x200B;

log_directory="/var/log/apache2/domlogs"

log_files=$(find "$log_directory" -type f ! -name '*bytes*' -size +0 -not -name 'ftp*' -not -name '*imapbytes*' -not -name '*popbytes*' | sort -u)

&#x200B;

for logfile in $log_files; do

filename=$(basename "$logfile")

wp_login_accesses=$(awk '$7 == "/wp-login.php" {print $1}' "$logfile")

ip_counts=$(echo "$wp_login_accesses" | sort | uniq -c)

sorted_counts=$(echo "$ip_counts" | sort -nr)

total_count=$(echo "$sorted_counts" | wc -l)

&#x200B;

if [ "$total_count" -ge 5 ]; then

top_5=$(echo "$sorted_counts" | head -n 5)

echo "Top 5 IP Addresses in $filename:"

while IFS= read -r line; do

printf "%s\n" "$line"

done <<< "$top_5"

echo

fi

done

https://redd.it/1bsci1e
@r_bash
Hex encode

Is it possible to hex encode a string using bash one liner or common built in programs on linux instead of installing some program to do it?

https://redd.it/1bspjxa
@r_bash
Several questions about my shell noscript:

xsetroot -name "$(free -h | awk '/^Mem:/ {print $3 "/" $2}') $(uptime --pretty | sed -e 's/up //g' -e 's/ days/d/g' -e 's/ day/d/g' -e 's/ hours/h/g' -e 's/ hour/h/g' -e 's/ minutes/m/g' -e 's/, / /g') $(date +"%a, %B %d %l:%M%p"| sed 's/ / /g') $(amixer get Master | tail -n1 | sed -r 's/.*\[(.*)%\].*/\1/')"

1. How do I replace uptime with the load given via the uptime command?
2. How do I make the date ISO-8601 format, and the time 24 hour time?
3. How do I add disk space (/)?
4. Is it possible to add IPv6, wireless, and ethernet, so that it looks the same as i3's:

`wireless _first_ {`

`format_up = "W: (%quality at %essid, %bitrate / frequency) %ip`

`format_down = "W: down"`

`}`

&#x200B;

`ethernet _first_ {`

`format_up = "E: %ip (%speed)"`

`format_down = "E: down"`

`}`

https://redd.it/1bswqs2
@r_bash
Having fun trying to figure out how much Perl do I have in my Ubuntu system

Here's what I got:
file /usr/bin/* | grep 'Perl noscript text' | awk '{print $1}' | sed 's/.$//' | xargs wc -l | sort -n

I'm sure it's not all but I'm getting over 60k LoC
(I was curious about this because it's a language that I hope I'll never learn and so these files will forever be a black box to me :) )


https://redd.it/1bt55t8
@r_bash
Is this a minor shellcheck omission?

Assigning a variable that starts with a digit inside an arithmetic context.

#!/bin/bash
1num=$((10+1))
((num=1+2))
((2num=10+2))
echo $num "$2num"


Shellcheck 0.10.0 bash 5.1.16. 2num on line 4 isn't marked as an error by shellcheck.

$ shellcheck --color=never test.sh

In test.sh line 2:
1num=$((10+1))
^------------^ SC2282 (error): Variable names can't start with numbers, so this is interpreted as a command.

For more information:
https://www.shellcheck.net/wiki/SC2282 -- Variable names can't start with n...
$ bash -x test.sh
test.sh 2 1num=11
test.sh: line 2: 1num=11: command not found
test.sh 3 (( num=1+2 ))
test.sh 4 (( 2num=10+2 ))
test.sh: line 4: ((: 2num: value too great for base (error token is "2num")
test.sh 5 echo 3 num
3 num


What does value too great for base error actually mean?

https://redd.it/1bt54vz
@r_bash
Fetching value from .json file

I've got a .json file that contains the following:

"shared":
{
"delimiter": "|",
"queueFileName": "~/Documents/SendEmailQueue.txt"
}

Then I've got a noscript that fetches the queueFileName value via:

queueFileName=$(jq -r '.shared.queueFileName' Backup.json)

In an attempt to figure out what's happening, specifically the noscript is not finding the file in question, I added the following bit of code:

if -f "${queueFileName}"
then
echo "Queue file, \"${queueFileName}\", is present"
if -s "${queueFileName}"
then
echo "... and it's not empty!"
else
echo "... but empty!"
fi
else
echo "Queue file, \"${queueFileName}\", not found"
fi

When I run the noscript, the following is displayed:

Queue file, "~/Documents/SendEmailQueue.txt", not found

However... if I change the "queueFileName" value in the .json file, (removing the "\~/" shortcut), to this;

"/home/babbayagga/Documents/SendEmailQueue.txt"

The following is displayed in the terminal window:

Queue file, "/home/babbayagga/Documents/SendEmailQueue.txt", is present
... and it's not empty!

It's as if the "\~/" shortcut is not being expanded. Does anyone know why this might be happening?

Thanks!

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