r_bash – Telegram
sed modify first ocurrence

Hello, in nginx vhost i need add string in the same line of server_name.

Example:

server {

server_name HERE domain1.com

}

to this I have been able to solve with the following:

​

sed 's/\\bserver_name\\b/& domain2.com/'

Inside the vhost file there are several server_name and I just have to modify the first occurrence I tried with sed '1 s/\\bserver_name\\b/ domain2.com' but it does not work.


Any helps?

https://redd.it/10r37uk
@r_bash
How to store multiline string in variable preserving indents and newlines?

Hi,

How do I store a multiline hardcoded string line like
sentence1\n
sentence2\n
\tsentence3


If I write it like e.g.
var="
sentence1\n
sentence2\n
\tsentence3
"


There appears to be a leading whitespace at each new line after sentence1.

Just for clarity as the string is long, I cannot write
sentence1\nsentence2\n\tsentence3
in one line

Thanks in advance

https://redd.it/10r85hb
@r_bash
Grep for a string only if it appears in the last field, without removing anything else from the string?

Let's say I'm trying to grep through these:

1 /dir1/dir2/dir3/banana
2 /dir1/dir2/banana/file
3 /dir1/dir2/bananafile

I want to grep for banana, and I want it to return rows 1 and 3 only. Line 2 should not be returned because banana does not appear after the last slash.

I also want the full lines displayed, so chopping the line up with awk would not help.

Anyone have any ideas? I'm sure this is very easy with a regex.

https://redd.it/10rc8so
@r_bash
Is there something with test -v?

I swear I did it right yesterday:

So, I exported a variable export XDG_BIN_HOME=$HOME/.local/bin

There was no way I managed the test to fire in a noscript like this:

#!/bin/bash
if -v XDG_BIN_HOME ; then
echo It is set
fi

And I did check my spelling several times. I wondered, last night if it was the .sh extension I had on the test noscript: testv.sh.

But, today it worked.

Do any of you have any clue as to what can have caused the malfunctioning. I feel I can't trust test -v now, and well, the rework from that to if [ x"$XDG_BIN_HOME" = x ] ; then ..., isn't that big, but it is annoying.

And, I can't understand how the builtin test could have been unset.

GNU bash, version 5.1.4(1)-release (x86_64-pc-linux-gnu) under tmux 3.1c, inside alacritty 0.12.0-dev (87c38aa9)

https://redd.it/10rrle6
@r_bash
Calling all awk experts

I'm working on a noscript to handle bulk new user adding. The goal is to have a single noscript that will be portable between our various Linux systems and our BSD systems (bonus points if it also works on Solaris). The code I'm fixing already assumes bash, so that's good.

Linux has newusers, and BSD has useradd -f but the file formats are different, and many of the fields will be left blank (e.g. UID and GID, which are generated by the system). The gecos field is a bit complicated, so it makes it necessary to use awk, instead of something simple like cut. See below:

Input

$ cat sample
mmouse:"Mickey Mouse mmouse@foo.bar", Downtown Office
mmouse1:"Minnie Mouse mmouse1foo.bar", Work from Home
smcduck:"Scrooge McDuck", Corporate


I have no illusions of doing both linux and freebsd in a single awk command, so I've already got an OS Detection function which chooses which version of the awk command to use

The date in the FreeBSD output is generated by date +%d-%m-%Y

The passwords used here are temporary passwords, and force a change on the first login. I need to preserve them long enough to send an email. For simplicity (and portability), I'm using the following to generate passwords: openssl rand -base64 8 | head -c 8

Desired output (FreeBSD)

mmouse::::02-02-2023::"Mickey Mouse mmouse@foo.bar", Downtown Office:/home/mmouse:bash:JDF9iQXh
mmouse1::::02-02-2023::"Minnie Mouse mmouse1@foo.bar", Work from Home:/home/mmouse1:bash:ryVDgJ9N
smcduck::::02-02-2023::"Scrooge McDuck ceo@foo.bar", Corporate:/home/smcduck:bash:X9kfz7x0

Desired output (Linux)

mmouse:JDF9iQXh:::"Mickey Mouse mmouse@foo.bar", Downtown Office:/home/mmouse:/bin/bash
mmouse1:ryVDgJ9N:::"Minnie Mouse mmouse1@foo.bar", Work from Home:/home/mmouse1:/bin/bash
smcduck:X9kfz7x0:::"Scrooge McDuck ceo@foo.bar", Corporate:/home/smcduck:/bin/bash


What I have so far

today=$(date +%d-%m-%Y)

# Build the file to the format that FreeBSD expects
#
awk -F':' -v today="${today}" '{print $1"::::"today"::"$2":/home/"$1":bash:password how?"}' input > outputFreeBSD

This works, but without the password, and I can't figure that part out. If I add a system(...) call in the middle of the print statement, it blows up. I have to be able to generate a pass for each user, and preserve the value of that pass so I can email it to the user (using the email address in the gecos, which will no doubt be another headache). I don't want to include it as a static entry in the input file, because I want to have some semblance of security with something that's generated and used only once.

e: I guess as long as the username
gecoswithpassword file contains the password (until the noscript cleans up after itself at the end), I can pull the username and password fields to send the emails, but I still have to get the password thing figured out.


I'm also okay with doing this as two steps - e.g. one awk to build user:gecos:pass and then another to build the one expected by the system's useradd command.


e2:

For those curious about the OS detection, I shouldn't have been so grandiose - bash is the one who has the detection and I just read that variable

if [ "$OSTYPE" == "linux-gnu"* ]; then
linux
elif [ "$OSTYPE" == "freebsd"* ]; then
freebsd
elif [ "$OSTYPE" == "darwin"* ]; then
echo "go home MacOS, you're drunk"
exit
else
return
fi

https://redd.it/10s60l3
@r_bash
Bash output to a csv file is missing header.

Hello folks, I am new to bash, so bear with me.
In our server, I am trying to run a sql command and write the output to a csv file, something like this:

echo "select top 10 * from myTable" | tql >result.csv

This is writing data to the csv file, as expected - but for some reason, it is not writing the column names. How can I get the column names and the data in one csv file?

https://redd.it/10s9nic
@r_bash
Split multiple videos in folder if longer than 30 minutes

Hello,

i have about 450 videos and a few are longer than 30 minutes.
is there any way that a noscript automatically "scan" all videos in the folder and split those, who are longer than 29:59:59 into part1.. part2?


Greetings!

https://redd.it/10sgcd2
@r_bash
Difference between separating commands by ; or && or a new line in a noscript, and pressing return on the command line?

Somehow pressing return in a bash terminal session has a gross effect that ; or having a new line in a noscript does not.

I have the following result, reproducible and in multiple projects:

This works consistently:

$ rm -Rf .direnv/ && direnv allow
$ poetry install
Installing dependencies from lock file

Package operations: 23 installs, 0 updates, 0 removals
... much more ...

But none of these do:

$ rm -Rf .direnv/ && direnv allow && poetry install
Errno 2 No such file or directory: 'python'

$ rm -Rf .direnv/ ; direnv allow ; poetry install
Errno 2 No such file or directory: 'python'

$ rm -Rf .direnv/ ; direnv allow ; sleep 10; poetry install
Errno 2 No such file or directory: 'python'

$ cat ./clear.sh
#!/bin/bash

rm -Rf .direnv/ && direnv allow
poetry install

$ ./clear.sh
Errno 2 No such file or directory: 'python'

$ source ./clear.sh
Errno 2 No such file or directory: 'python'

$ bash ./clear.sh
Errno 2 No such file or directory: 'python'

Now direnv is of course doing something fancy with bash behind the scenes to achieve its effects, but how does it "know the difference" between ; or a new line in a noscript, and pressing return in the terminal?

I just wanted to write alias clean='rm -Rf .direnv/ && direnv allow && poetry install', I wasn't trying to cause any trouble ;-), is there some workaround?

Thanks in advance!

https://redd.it/10sj81o
@r_bash
Trouble adding escape character for {

I have a noscript that I've written to download files via rclone. My noscript goes through the file list and adds escapes to [ and \] without issue. I've recently found out that { and } also cause problems, so I'm trying to add escape characters before those as well. I copied the code that I used for [ and \], and while it worked fine for }, it doesn't work on {. Does anyone know what I could do to make it work?

sed -i 's/\\\]/\\\\&/g' "$LOGFOLDER"/Current.txt # Escape \] - This one works

sed -i 's/\\[/\\\\&/g' "$LOGFOLDER"/Current.txt # Escape [ - This one works

sed -i 's/\\}/\\\\&/g' "$LOGFOLDER"/Current.txt # Escape } - This one works

sed -i 's/\\{/\\\\&/g' "$LOGFOLDER"/Current.txt # Escape { - This one does not work

https://redd.it/10snxpd
@r_bash
How to handle nested arguments?

Hello, I am sure this is a simple question for most of you here and I would really appreciate your help.

Lets say I want to run a program with an argument which itself is a program with arguments? For example:

gdb python main.py 123

I want to make gdb take (pyhon main.py 123) as an argument. Will this work like I imagine? I am asking because it is ambiguous: One could also say that gdb takes 3 arguments python, main.py and 123. How would you do this?

https://redd.it/10spuf4
@r_bash
Linux Shell question

Hi, I'm currently taking an OS class where we're learning how to work in the Linux shell on a virtual machine and am struggling with finding the log file created by the noscript command. The HW is basically just us creating and removing files for practice, then submitting the log file to prove we did it. I have no issues following the file manipulation instructions... but I can't find the log file. I enter in the noscript command, follow the instructions, type exit and.... I have no idea how to find the log file to turn in? I don't know where it's at on my computer, or if I even am creating files on my computer. When I do the ls -l command, the files I've created all get listed.

​

I'm running the shell on Oracle VM VirtualBox Manager. I downloaded this and the shell from here: http://cs.westminstercollege.edu/\~greg/osc10e/vm/index.html

​

Sorry for asking such a stupid and basic question. I just started this class and am already so lost lol.

https://redd.it/10sxvn5
@r_bash
How do I prevent my noscript from executing sudo?

I've got a noscript that runs noscripts from other files using "sh some-file.txt". I don't want to block my whole noscript if "some-file.txt" contains "sudo" or some input request. How can I prevent that?

I would like "sh some-file.txt" to just fail if sudo is requested. Is it possible? I spent several hours trying to achieve that with no luck :(

https://redd.it/10t1djf
@r_bash
how to get url from text, or why 'grep -o http' doesnt work

i have text

something:
https://url.domain/sub somemoretext

how to get this url using tools like grep/awk etc.?

i tried grep -o http\
but it didnt work, but seemd to be great idea

result must be: `https://url.domain/sub`

​

solve: use quotes... grep -o 'http.* '

https://redd.it/10td8li
@r_bash
Sed can do amazing things!

I've wrote my renders for TlDr-like project (named Command Line Interface Pages or CLIP), but more standardized. They are written in Bash + Sed + Awk. It was more a challenge, but to be honest I use what I've written as I feel it's more convenient than TlDr clients and more flexible. To view sed command examples (from `common/` directory) just write the following command:

clip-view -r tldr-colorful sed

Currently 4 renders available but the 5th and the most complicated will be finished soon. Command output looks like this:

https://preview.redd.it/6sakva70w6ga1.png?width=1920&format=png&auto=webp&v=enabled&s=b26167ee623a0061fa88b152196c5aef7019c70f

Requested pages are automatically cached to speed up example's loading time.


This command will be packaged as .deb package soon. ;) Stay tuned.

https://redd.it/10tiw1t
@r_bash
AWK noscript to generate Entity Relationship diagrams

Hello, everyone!

I wrote a small AWK noscript that generates ER diagrams for the SAP CAP model: https://github.com/rabestro/sap-cds-erd-mermaid/

Although this is a tiny and simple noscript, it managed to generate mermaid diagrams for the entities of our project. It does not support all the features in the CDS format. In any case, this noscript can be improved, and it may be helpful to someone.

https://redd.it/10tln0k
@r_bash
noscripts for sys admins!

Made quite a few noscripts for server management. These are all in production use for my TrueNas home lab. Thought id create a repo and share. There's also a noscript for updating a Minecraft server and starting it up again but I have yet to add it. For all the home labbers of the bash community https://github.com/Agb43/server-admin-noscripts.git

https://redd.it/10tqd5i
@r_bash
I get errors in if statement "[: : integer expression expected"

I'm trying to write simple code that notifies me when battery is low. Here's my code

#!/bin/bash

# maximal energy in battery
enf0=$(cat /sys/class/power_supply/BAT0/energy_full)

# notify at these levels
btlow=30
bthigh=90

# check every n seconds
time=30

while [ 1 ]
do

# current energy, charging/discharging, energy level in %
enc=$(cat /sys/class/power_supply/BAT0/energy_now) ;
state=$(cat /sys/class/power_supply/BAT0/status) ;
lvl=$(echo "scale=2; $enc * 100 / $enf" | bc) ;

if [ $lvl -lt $btlow ] && [ "$state" == 'Discharging' ]
then
notify-send "Low battery" "Battery level: $lvl%"
fi

sleep $time

done

but when I run it I get error `line 25: [: -lt: unary operator expected`, I read [this](https://stackoverflow.com/questions/50349389/shell-noscript-lt-unary-operator-expected) but when I change this line to `if [ "$lvl" -lt "$btlow" ] && [ "$state" == 'Discharging' ]` then I get different error `line 25: [: : integer expression expected`

https://redd.it/10trm1l
@r_bash
cp: no such file or directory

Hi all,

I'm using FreeBSD and trying to migrate my jails from iocage to bastille. Really not familiar with bash so figured I would try and noscript it rather than do it manually as a learning exercise.

On the cp command below I get the error:

cp: /tank/iocage/images/db_2023-02-05.*: No such file or directory

Here is the full noscript. I would appreciate any help.

​

#!/usr/bin/bash

jails=$(/usr/local/bin/iocage list -qh | awk '{print $1}')
today=`date "+%Y-%m-%d"`

for jail in ${jails[@]}; do
filename=$jail
filename+="_"
filename+="$today"

iocage stop $jail
iocage export $jail
iocage set boot=0 $jail
cp "/tank/iocage/images/$filename.*" /usr/local/bastille/backups/
bastille import "$filename.zip"
done

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