r_bash – Telegram
Error when trying to setup crontab while launching an EC2 instance via user data

* I want to set up a bunch of cron jobs when I launch an EC2 instance
* I followed this guide [user data](https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/user-data.html)
* I wrote a bash noscript which I would like to execute as part of the user data at launch

Unfortunately the noscript below does not work for some reason (Amazon Linux 2 instance)


#!/usr/bin/env bash

set -e
set -E
set -o pipefail
set -u
set -x

IFS=$'\n\t'

crontab -r
crontab -l | { cat; echo "0 0,4,8,12,16,20 * * * bash /home/ec2-user/utils/src/rds/dump-rds-to-s3.sh > /tmp/dump-rds-to-s3.log 2>&1"; } | crontab -
crontab -l | { cat; echo "0 0,4,8,12,16,20 * * * bash /home/ec2-user/utils/src/elasticache/dump-elasticache-to-s3.sh > /tmp/dump-elasticache-to-s3.log 2>&1"; } | crontab -

It gives me an error saying
```
no crontab for root
```
When I run the noscript manually after launch, it gives me the error
```
No crontab for ec2-usser
```
Can someone kindly tell me how to add cron jobs automatically via bash noscript at launch time inside an EC2 instance via user data?

https://redd.it/16adynn
@r_bash
How to read value of variable in this case?

Hello folks.

I have this bash noscript (im a beginner) where i have 3 directorys: A B and C.
These directorys have values, after how many days the files inside get deleted.

A = 1
B = 3
C = 2

Sources = $(find /Test/Source/* -maxdepth 0 -type d)

for o in $Sources; do

SD = $(basename -a $o)
SDC = `echo $SO | cut -d" " -f1`
echo $SDC

Done

This gives me the folders A B and C.
Now i want to create syntax for reading the corresponding timevalues. I dont want to use if questions and if possible no Array.

I thought of something like this :

echo $$SDC

This reads the Folder "A" but puts $ infront. So it makes $A which in return prints 1. Sadly it doesnt work exactly like that. But maybe someone has a idea on how to write this command.

Thanks in advance

https://redd.it/16an65u
@r_bash
WTF happened to "history -w/-r".

I am missing the ability to write the shell buffer to history and read the history from the history file...

https://redd.it/16b2d50
@r_bash
Default behavior for no explicit options?

If we do ls | pr -5 (equivalent to ls | pr --column 5), how does the terminal know the -5 splits the columns into columns of 5. I've read through man pr and info pr but there is no information given on the default behavior of not having an explicit option.

Do you know any other commands that take in a default option?

https://redd.it/16b5nzn
@r_bash
Inserting a picture into a PDF heavily increases its size. Why?

This is a part of my noscript, that works with 8 pictures. I isolated this part to test it and check how can I prevent it to create heavy PDFs.

Objective: a noscript that creates an A4 blank PDF, and insert a picture without modifying the picture or its dimensions or resolution.

The picture sizes 170 KB. The blank A4 PDF sizes 136 KB. Both should create a ~306 KB. Instead, it creates a 2.9 MB PDF. It puts it in the right place and keeps the original resolution of the picture, but somehow it bloats it.

What am I missing?

#!/bin/bash

#### CONSTANT VALUES
imgpath=pic1.png # 170 KB
dpi=300
output
page=testing.pdf

# Image dimensions in pixels
widthpixels=1181
height
pixels=787

# Dimensions of A4 paper in pixels
a4widthpixels=2479 # 21 cm
a4heightpixels=3508 # 29.7 cm

#### BLANK A4 PDF CREATION
convert -size ${a4widthpixels}x${a4heightpixels} xc:white -density $dpi $outputpage
echo "the PDF size is: "$(du -h $output
page) # 163 KB

#### FUNCTION: integer from a float (there's no half pixel)
int() { echo $(env printf %.0f $(echo "scale=0;$1" | bc)); }

#### IMAGE PLACEMENT VARIABLES
# Margins and spacing
margintop=$(int $(echo "($a4heightpixels - $heightpixels) / 2" | bc -l))
marginbottom=$margintop
marginright=$(int $(echo "($a4widthpixels - $widthpixels) / 2" | bc -l))
marginleft=$marginright

# Specific image placement
xoffset=$(int $(echo "($a4widthpixels - $widthpixels) / 2" | bc -l))
yoffset=$(int $(echo "($a4heightpixels - $heightpixels) / 2" | bc -l))
echo "x,y="$xoffset","$yoffset

#### FINAL PDF CREATION
convert -density $dpi $outputpage $imgpath -geometry +$xoffset+$yoffset -composite $outputpage
echo "the PDF size is: "$(du -h $output
page) # 2.9 MB

https://redd.it/16b6uh4
@r_bash
Once I reach 70% of the udemy courses, I start to get bored and impatient to finish the course. how do I deal with this issue?

noscript. Im currently learning nginx and bash shell noscripting. 80% through nginx course and 50% through bash course and I'm getting impatient. i want to finish nginx by tommorow. I've around 2 hrs of content remaining. And bash noscripting by next 2 weeks. How do I control myself? I am asking this because learning fast fast you learn less things. I want to be patient. how do I make myself patient. If I make my goal to finish nginx by next week and bash by this month, I'll really learn a lot of good stuffs. but how tf do I keep my patience? I'm in hurry to colelct the certificate and post in linkedin lol.

https://redd.it/16ci10g
@r_bash
What level does using mindepth or maxdepth for a find command start at?

I am writing a bash noscript and a little unsure about the min/max depth option for the find command.

I have this find command and want to add maxdepth and mindepth to it

find /local/applogs/archive/ -type d -mtime +366 -exec echo {} \;


And this is the full depth of the directory, what number would I use for the depth options?

/local/applogs/archive/host/comp/2021/01/

The point of the find will eventually be to find any empty directories that are over a year old and remove them using rmdir which should make sure they are empty. Except it should only remove old, empty year and month (2021 and 01) directories.

Would I start the depth count from / or from the directory I am beginning my search (archive)?

​

https://redd.it/16cgem5
@r_bash
A working alternative to someprogram & disown & exit

I have trawled around looking for a solution for what seems should be fairly trivial.
It's worth noting that I am using wayland (**insert rant about not being able to generalize over compositors, this is the one thing making me think of switching back to X).


I have tried all manner of things including fetching the id of the current window, launching the program, disowning, & then closing the window with the id, but after running disown the call to exit or whatever after is never ran leaving the term open.


Any suggestions at all?
Still a bit of a bash novice, any reading materials also appreciated. Thanks.

https://redd.it/16cm8d8
@r_bash
writing a single line of a process output to a file

Hello, i want to write a single line of a process to a file , basically i want a command to write a single line to a file when there is a new notification. i tried different things without success:

dbus-monitor "member='Notify',interface='org.freedesktop.Notifications'" | grep-e "member=Notify" | while read line;do
echo "new line" >> ca.txt
done

​

dbus-monitor "member='Notify',interface='org.freedesktop.Notifications'" | grep-e "member=Notify" | while read line;do
tee -a ca.txt
done

​

dbus-monitor "member='Notify',interface='org.freedesktop.Notifications'" | tee company.txt >(grep "member=Notify" >> ad.txt)

but it seems the grep output isn't redirected to tee or echo . when i add -m1 flag to grep it writes the single line to the file but after 3 times the command exits but i want it to keep running . does anyone knows what im doing wrong?

https://redd.it/16cpa3v
@r_bash
Exit command failure

The following is a noscript to read the news:

```
#!/bin/bash

day=$(date +%d | cut -c 2 | awk '{print $1 - 1}')
month=$(date +%m | cut -c 2)
year=$(date +%Y)

xdg-open https://www.somenews.com-$day-$month-$year/ > /dev/null 2>&1

exit
```

i'd like the terminal to close after i execute the noscript but it doesn't.


but if i make an `alias` in my bashrc:

`alias news_item="yesterday_news.sh && exit"`


then it works as expected and closes the terminal.

What's causing it?



----

terminal=alacritty
web_browser=vivaldi

https://redd.it/16d1gd4
@r_bash
Why does the prompt combine with the last line of stdout?

Sometimes this happens

user@DESKTOP:~$ printf "$(cat file1.txt)"
dog
cow
catuser@DESKTOP:~$

The last line of the stdout cat would combine with the prompt user@DESKTOP:~$?

What would cause the prompt not to be on a new and separate line? I assume sometimes the terminal glitches out and I have to accept it (and restart it) but I suspect there is a reasoning behind this.

https://redd.it/16d1k41
@r_bash
Script doesn't work in cron

This noscript in /home/media works and does what it needs to. It doesn't work when setup as cron.

I tried cd /home/media before the loop but couldn't find mkv files.

How do I make it work ?

OS is ubuntu server.

#!/bin/bash

for F in **/*.mkv; do
###
some code
###
done
exit 0

https://redd.it/16d4dsq
@r_bash
why test makes this noscript to fail?

Please consider these two noscripts:

run.sh:

#!/bin/bash
set -euo pipefail
. "$(dirname $(realpath $BASHSOURCE))"/init-sudo-noscript.sh

`init-sudo-noscript.sh`

[ ${
#BASHSOURCE@} -eq 1 ]\
&& echo "must be sourced by another noscript."\
&& exit 10

$EUID -ne 0 \
&& echo "must be executed as root."\
&& exit 20

This is correct and it is what I expect to happen:

$ ./run.sh
must be executed as root.
$ echo $?
20

But this I can't understand:

$ sudo ./run.sh
$ echo $?
1

I know the problem is [ $EUID -ne 0 ] because the noscript works when I remove it.

I also understand set -e makes the noscript to exit on any error.

What I don't understand is why the first guard condition ([ ${#BASH_SOURCE[@]} -eq 1 ]) doesn't exit with 1 when it fails but the second does.

Does anybody understand what is happening here?

https://redd.it/16d7hg0
@r_bash
Can this be done with the level of single line simplicity I'm trying to accomplish?

I just started learning bash and I'm trying to make a noscript resolve with the smallest amount of code possible. The problem is as follows:

>Create a new noscript called calculate-average.sh. The noscript should accept exactly 3 command-line arguments and calculate the average. The result should not round the value to the nearest integer.

The issue I'm having is not how to solve the problem with multiple lines but with one. This is where I've gotten so far:

echo $(((($1+$2+$3)/3) | bc -l))

So far the addition and the division work fine but when it comes to printing the result as a float (for cases with uneven numbers), that last bit of code keeps getting ignored for some reason. Is there a way to do it or do I forcefully need to resort to 2 lines of code?

https://redd.it/16d8r76
@r_bash
Help on find file with max number in filename and replace

I’m playing with bash on Mac but got some challenges which I haven’t found the way to solve. Please give me some suggestions. Thanks!

I have list of following file in folder:
abcen.srt
abcen1.srt
abcen2.srt

abc
enn.srt

- How can I find the file with max “n”?
- If I want to replace “abc
en.srt” with “abcenn.srt” (if it exists), how can I achieve this?

https://redd.it/16de4p3
@r_bash
Export variables from shell noscript to expect noscript

Hi i have problem with export variables from
ports..sh
I want to automate installing something and i need ports from inside of the container. If i use only for reading (echo commands) its work well, but when i try to catch thats ports in expect.sh i throw error.

How to do that correctly?


expect.sh
Expect works well without variables

#!/usr/bin/expect


spawn bash -c {....command....}

source ports.sh

set timeout 600

expect {
...expect noscript...
"$port1\r"
"$port2\r"
"$port3\r
}
}

exit




ports.sh

#!/bin/bash

netstat_output=$(netstat -tuln)

port1=""
port2=""
port3=""

while IFS= read -r line; do

ip_address=$(echo "$line" | awk '{print $4}' | awk
-F: '{print $2}' | awk -F. '{print $NF}')


if [[ $ip_address =~ ^[0-9]+$ ]]; then

if [[ $ip_address =~ ^8 ]]; then
port1="$port1 $ip_address"
elif [[ $ip_address =~ ^9 ]]; then
port2="$port2 $ip_address"
elif [[ $ip_address =~ ^10 ]]; then
port3="$port3 $ip_address"
fi
fi
done <<< "$netstat_output"

# echo "Ports 8..."
export port1="${port1# }"

# echo "Ports 9..."
export port2="${port2# }"

# echo "Ports 10..."
export port3="${port3# }"


Error


/ # ./autoUpdate.sh
spawn bash -c [command]
can't read "(netstat -tuln)": no such variable
while executing
"netstat_output=$(netstat -tuln)"
(file "ports.sh" line 3)
invoked from within
"source ports.sh"
(file "./autoUpdate.sh" line 6)
/ # source ports.sh
bin/sh: ports.sh: line 24: syntax error:
unexpected redirection
/ #

https://redd.it/16ecvd1
@r_bash
Integer express expected

So in my .sh file I have a command called

"(curl ${HOSTIP}:9200/cluster/health | jq .numberofdatanodes)"

this is checking elastic's health. So I always get error unknown command integer expression expected and unknown command '.number\
of_data_nodes' even though this command curl ${HOST_IP}:9200/_cluster_health does return an object containing {"number_of_data_nodes":1} there's also other stuff returned too but lets keep it to this for now. I'm not sure why it can't recognize it

https://redd.it/16eefyg
@r_bash
run service as root but get current user info?

I don't know if I'm supposed to be posting in Gnome or Wayland subreddit, but I'm a noob, so please help me out if you can.

I'm trying to run a systemd service that gets idle time and assigns it to a variable:

getidletime=/usr/bin/dbus-send --print-reply --dest=org.gnome.Mutter.IdleMonitor /org/gnome/Mutter/IdleMonitor/Core org.gnome.Mutter.IdleMonitor.GetIdletime

However, idle time is user specific, so it always fails. I've tried sudo -u $USER and sudo -u <specific user> both inside and outside the backticks. Any ideas?

https://redd.it/16etxvv
@r_bash
Converting from string to json in a .sh file

If I have a variable called

res='{number_of_data_nodes:1}'

how can I convert this to a JSON object so it prints like a json object and not a string

https://redd.it/16f88zx
@r_bash
Why does this attempt to read/write to arrays not work?

!#/bin/bash

declare -a testarray
count=0
testvariable0=dog ; testvariable1=cat

while $count -lt 2
do
testarray$count+="$testvariable$count"
((count++))
done

echo ${testarray0}
echo -e
echo ${testarray1}

I'm going to write a function that i'm going to reuse a lot. The function will be pointed at an arbitary position in an array, so I thought that I could replace the specifying number with the contents of a variable.

To test this, I made this short looping noscript.

Expected output:

dog

cat

actual output:

0

1

What gives?

https://redd.it/16g4e2z
@r_bash