r_bash – Telegram
run bash noscript on remote server, I want the output on the local machine

Hi all,

I am trying to build a bash noscript that will go to a remote server within out domain. The noscript will run different small noscripts base on the selection the user makes from a case block of code. EG:

do a df -h, uptime and say cat /etc/fstab file. But I would like the output to display on the screen of the server I am working from. I have seen different ways, but none has worked for me. I have also seen the use of Localhost but not sure how this one works. Any links or feedback would be appreciated. thanks

https://redd.it/1242t11
@r_bash
Please, help me write a bootstrap noscript to display ec2 instance metadata on a web page



I am a noob when it comes to coding/noscripting. I need to write a bootstrap noscript that will display the ec2 instance ID and availability zone on my web page. So far what I have is:

\#!/bin/bash

sudo yum update

sudo yum install httpd -y

sudo systemctl start httpd

sudo systemctl enable httpd

sudo aws s3 cp s3://mybucket/index1.html /home/ec2-user/index2.html

INSTANCE-ID=curl http://169.254.169.254/latest/meta-data/instance-id

INSTANCE-AZ=curl http://169.254.169.254/latest/meta-data/placement/availability-zone

sed 's/instanceID/$INSTANCE-ID/' index2.html

sed 's/AZ/$INSTANCE-AZ/' index2.html

The curl command doesn't seem to be working, and the sed command keeps skipping over the $INSTANCE-ID part. Also, When I run the last four lines in the ec2 CLI it just echos the entire html file and only run's "sed 's/AZ/$INSTANCE-AZ/' index2.html".

Again, I'm trying to replace "instanceID" and "AZ" (Both inside of my html doc) with the values of $INSTANCE-ID and $INSTANCE-AZ (from my bootstrap noscript INSTANCE-ID=curl..., INSTANCE-AZ=curl...)

If there's a better way please let me know.

https://redd.it/1245ezx
@r_bash
Having a hard time incrementing a variable, can someone show where I am wrong at? Thanks

So I set a variable to a uid that I pull from a server. I then want to increment this by 1 and echo that out.

I keep gettingsyntax error: invalid arithmetic operator (error token is "

​

My code so far is as:

uid=$(kubectl command | awk) <-- ultimately returns a value such as 2000
echo "UID = ${uid}"newuid=$((uid++))echo "New uid is $uid"

Any thought why I am recieving this error? I am 100% confident I am not getting a decimal and its returning 2000.

I created the below to test and it works as expected

uid=5000
echo "UID = ${uid}"
newuid=$((uid++))
echo "New uid is $uid"

This works when I set the uid to a number in the noscript instead of pulling it in via a variable.


I feel it has something to do with a character that kubectl is returning that is not showing in the echo commands.

https://redd.it/1246xtg
@r_bash
how to send keystrokes

how can i register any other keystroke like <ENTER> key command in bash noscript

https://redd.it/124g02n
@r_bash
Need help with simple brightness noscript.

Hey there, I am new to Linux and Bash and i was making a simple noscript to get my brightness level as a percentage but i keep getting the following output:

0%

With the expected output:

50%

It is impossible that the issue lies with the cat command since I do not need root privileges to cat the file and the file includes an integer:

$ cat /sys/class/backlight/intelbacklight/brightness
530
$ cat /sys/class/backlight/intel
backlight/maxbrightness
1060

Anyways, here is my noscript:

#!/bin/bash
brightness=$(cat /sys/class/backlight/intel
backlight/brightness)
maxbrightness=$(cat /sys/class/backlight/intelbacklight/maxbrightness)
brightness
percentage=$(($brightness/$maxbrightness*100))
echo "$brightness
percentage%"

Thanks in advance!

https://redd.it/124hfaa
@r_bash
a chatgpt discussion

Chat gpt seems to be pretty good at bash, I think the definite preference is python but it seems to be pretty good at building skeleton noscript that you can qa and unit test.

Gives pretty good explanations too.

Just don't get it using dd, it's a partition destroying liability.

How have you found it ?

https://redd.it/124h7gj
@r_bash
Why can't I use !! in bash noscripts or in the bashrc ?

Ideally I would like be able to set an alias that does the same thing as !!, or call a noscript that does the same thing as !! . However i get "command not found" . The only way I am able to repeat a command is by directly typing !! into the prompt. Is there a way to get around this ?

https://redd.it/124vjzu
@r_bash
Trying to find hex in bin file

I'm trying to search a bin file for "1E FA 80 3E 00 B8 01 00 00 00"

I can find 1E

grep -obUaP "\x1E" "$file"

and I can find FA

grep -obUaP "\xFA" "$file"

But trying to find 2 bytes doesn't work:

grep -obUaP "\x1E\xFA" "$file"

I'm actually trying find and replace the 2 bytes that come after "1E FA 80 3E 00 B8 01 00 00 00".

https://redd.it/125d3up
@r_bash
Why can't the shell access the ssh authentication agent launched by the noscript?

I pieced together a simple noscript that allows me to backup my Windows 10 PC (using WSL) to my NAS which is running Ubuntu. I managed to get it working but during debugging one thing I found confusing was that after the noscript ran if I wanted to add another key to ssh-agent the shell could see ssh-agent running but it couldn't interact with it. (see below for console log)

I am aware that when I launch the noscript it creases a subprocess of the current shell but why did the last command I typed fail to add the key to the authentication agent? Are processes launched in a subprocess not accessible by the shell?

&#x200B;

me@grimlock:\~$ cat backup2computron.sh#!/bin/bash

me@grimlock:\~$ cat backup2computron.sh

\#!/bin/bash

&#x200B;

&#x200B;

if [[ -n $(pidof ssh-agent) \]\]

then

echo "ssh-agent is already running"

else

eval `ssh-agent -s`

ssh-add /home/me/.ssh/grimlock_rsa_4096

fi

&#x200B;

cp /mnt/c/Users/me/AppData/Local/Google/Chrome/User\\ Data/Default/Bookmarks /mnt/c/Users/me/Documents/data/backup

rsync -haP --append-verify --no-perms --omit-dir-times --delete -e "ssh -i /home/me/.ssh/grimlock_rsa_4096" /mnt/c/Users/me/Documents/data/* me@10.0.0.1:/mnt/dataPool/data/backup/

&#x200B;

me@grimlock:\~$ ./backup2computron.sh

Agent pid 839

Identity added: /home/me/.ssh/grimlock_rsa_4096 (me@grimlock)

sending incremental file list

me@grimlock:\~$ pidof ssh-agent

839

me@grimlock:\~$ ssh-add /home/me/.ssh/id_ed25519

Could not open a connection to your authentication agent.

me@grimlock:\~$

https://redd.it/125iwsh
@r_bash
Writing haversine function in bash

I'm trying to convert the python haversine function [here:](https://stackoverflow.com/a/4913653)

def haversine(lon1, lat1, lon2, lat2):
# Calculate the great circle distance in kilometers between two points
# on the earth (specified in decimal degrees)

# convert decimal degrees to radians
lon1, lat1, lon2, lat2 = map(radians, [lon1, lat1, lon2, lat2])

# haversine formula
dlon = lon2 - lon1
dlat = lat2 - lat1
a = sin(dlat/2)**2 + cos(lat1) * cos(lat2) * sin(dlon/2)**2
c = 2 * asin(sqrt(a))
r = 6371 # Radius of earth in kilometers. Use 3956 for miles. Determines return value units.
return c * r

To pure bash. I know, I know, why? Just because really. Anyway I got this:

#!/bin/bash

# Haversine function implementation
# Usage: haversine lat1 lon1 lat2 lon2

# Convert degrees to radians
deg2rad() {
echo $(echo "scale=10; $1 * 0.017453292519943295" | bc -l)
}

# bc has arctan, but not arcsin so, stolen from http://advantage-bash.blogspot.com/2012/12/trignometry-calculator.html

asin() {
if (( $(echo "$1 == 1" | bc -l) ));then
echo "90"
elif (( $(echo "$1 < 1" | bc -l) ));then
echo "scale=3;a(sqrt((1/(1-($1^2)))-1))" | bc -l
elif (( $(echo "$1 > 1" | bc -l) ));then
echo "error"
fi
}

# Haversine formula
haversine() {
R=6371 # Earth radius in km
dLat=$(echo "$3 - $1" | bc -l)
dLon=$(echo "$4 - $2" | bc -l)
lat1=$(deg2rad $1)
lon1=$(deg2rad $2)
lat2=$(deg2rad $3)
lon2=$(deg2rad $4)

a=$(echo "scale=10; (s($dLat/2))^2 + c($lat1) * c($lat2) * (s($dLon/2))^2" | bc -l)
c=$(echo "scale=10; 2 * asin(sqrt($a))" | bc -l)
d=$(echo "scale=10; $R * $c" | bc -l)

echo $d
}

haversine $1 $2 $3 $4


Just some math. The problem I have when I run it with something like `haversine 22.70416 -118.29725 19.57834 -155.07947` I get an error that tells me `asin not defined` right near the end. It's defined right there, right above the haversine function. I'm not sure what it is I'm missing.

https://redd.it/125y1yk
@r_bash
program to print out biggest number from a set of numbers

i am trying to make a program to print out like:

How many numbers: 5

1

2

3

4

5

Largest number is 5

&#x200B;

So my question is, do I need to use like an array method for the set of numbers

https://redd.it/1260aue
@r_bash
How to check in bash if python noscript is running?

I use android (termux) scheduler to trigger following scheduler.sh

#!/data/data/com.termux/files/usr/bin/bash

# load EVs
source "$HOME/storage/shared/Books/AppsCfg/Termux/envfile"

python "
$MEMS/Binv/noscripts/binvscheduler.py"

Most of the time it works great (according to schedule android triggers scheduler.sh which triggers binvscheduler.py noscript), but sometimes on device wake up binvscheduler.py gets double triggered. I added create lock file logic into python noscript and it catches 99% of double triggers except one case... (i can't even understand how it's possible. maybe somehow 2 binvscheduler.py run exactly same moment so python can't create lock file fast enough. Not sure)

Anyway, how do i check inside bash (in
scheduler.sh) if binvscheduler.py is already running or not?

https://redd.it/12625u0
@r_bash
What is the meaning of ! -d

I'm sure this has been asked else where so sorry if so. But i'm with a collogue and we can't figure out what this is. Would any one happen to know Really appreciate ti!

https://redd.it/1263vrz
@r_bash
I couldn't find a way to check the difference between the current git branch and its parent so I decided to DIY

Hey everyone! As the noscript suggests, I created a small noscript to help me check the difference between my current branch and its parent by keeping a table that points to the parents. here is my repo

https://github.com/A-Siam/diffwatch

I am by no means a noscripting wizard so any suggestions or contribution is much appreciated.

Also, I don't know if my noscript is even a good idea :D if there is any good alternative to my hacky noscript please let me know.

cheers.

https://redd.it/1266mmd
@r_bash
How to delete a matching line and it's line break in a file

I have a .conf file where I use echo to append a line of text with a line break. But if I later want to remove the line I can't remove it and the line break.

To append the line to the end of file I use:

echo 'drive_db_test_url="127.0.0.1"' >> "$file"

Then I can remove the line with the following but it leaves the line break:

sed -i 's/drive_db_test_url=\"127\.0\.0\.1\"//' "$file"

I've tried things like but they don't work:

sed -irz 's/drive_db_test_url=\"127\.0\.0\.1\"\n//' "$file"

And I've seen all sorts of ways to remove all line breaks in a file, using tr, sed or awk but I don't want to remove all line breaks.

https://redd.it/126au5r
@r_bash
2>&1

You know what gets my head in?

In Linux/Bash when we want something to run in the background we would do something like this: ./yourCommand > /dev/null 2>&1 &

I am taking issue with 2>&1. "&1" refers to a variable as we can see by the "&", the standard bash variable "standard output" so far so good :> all makes sense.

But the "2" here refers to another variable "standard error" why then we don't write &2>&1?

Would ./yourCommand > /dev/null 2>1 & be valid syntax and work?

https://redd.it/126ogzm
@r_bash
A helper function to tell if a noscript is sourced?

I write a fair amount of noscripts and I often have strong opinions on whether those noscripts should be sourced or executed. It's usually because I don't want to screw over future me because I've written a noscript that is a little aggressive with the environment.

For those noscripts I have a block of code I run:

(return 0 2>/dev/null) && sourced=1 || sourced=0
if [[ $sourced -ne 0 ]]; then
echo "Don't source this noscript. Run it directly"
return 1
fi

I also hate copy and pasted code, but because this relies on the behavior return has depending on if a noscript is sourced or executed, I can't really put this is a helper function and call that helper function. Instead I do this (mildly crazy) thing where I have a bunch of noscripts stored in variables and then I just eval those in the noscript.


So I have a utiltiy noscript with something like:

# shellcheck disable=SC2034
BASH_COMMON_SCRIPT_UNSOURCEABLE=$(cat <<'EOM'
(return 0 2>/dev/null) && sourced=1 || sourced=0
if [[ $sourced -ne 0 ]]; then
echo "Don't source this noscript. Run it directly"
return 1
fi
EOM
)

And then I use this in a noscript kinda like:

CONFIGROOT_DIR_SCRIPT="$( cd "$( dirname "$( readlink -f "${BASH_SOURCE[0]}" )" )" >/dev/null 2>&1 && git rev-parse --show-toplevel 2>/dev/null )"
if [ -f "${CONFIGROOT_DIR_SCRIPT}/util/noscripts_as_variables.sh" ]; then
# shellcheck disable=SC1091
source "${CONFIGROOT_DIR_SCRIPT}/util/noscripts_as_variables.sh"
fi
eval "${BASH_COMMON_SCRIPT_UNSOURCEABLE}"

And yes I'm aware of the perils of eval'ing an env var.


Is it possible to do this in a more sensible way?

https://redd.it/126tv9y
@r_bash
CSV to variables

I'm trying to parse a csv to variables to use it in "update all wordpress/nextcloud/..." noscripts

The CSV looks like this:

nicename,application,directory
blog1,wordpress,/var/www/wordpress1
blog2,wordpress,/var/www/wordpress2
cloud,nextcloud,/var/www/nextcloud

How do i parse these into variables like $blog1.directory (using the nicename)?

https://redd.it/126rxvi
@r_bash