r_bash – Telegram
Hello guys newbie question related to grep here

Hello guys I have a text that is all in the English alphabet except two words which are in Cyrillic alphabet, so how do I get an output on only these two words with grep what I'm doing so far egrep '[A-Za-z0-9\]|\\W' when i add -v nothing in output. Thanks in advance

https://redd.it/118v1uu
@r_bash
Question about incrementing a variable

Hi and thank you for the help!

I am a newbie, sorry for the simple question, but I have to do the noscript this morning, so I cannot test and try too much.

I need to do a noscript that assign a number (that start at 5) and increment by 1 at each new user created. Users are also a variable (I have around 500 users to create with the noscript that also need an assign number).

What would be a good way to define my $NUMBER variable?

Can I do something like that:
$USER
NUMBER=$(i=5; while(($i­>5)) do echo $i((i++)) done)


Thank you again and sorry for the newbie question.. Any help would be greatly appreciated!

https://redd.it/1192vdk
@r_bash
Rename files with unique file names

I inherited a filesystem with a rather large number of files in a lot of directories and subdirectories. Most are fine, but it looks like a dozen or so have been “managed” by what I can only guess was a noscript gone awry.

In some of the directories I have a bunch of unique .pdf files with oddly sequenced file names; specifically, they’re all a series of 1’s followed by the .pdf suffix. For example:

1.pdf
11.pdf
111.pdf
1111.pdf

111111111111111111111111111111111111111.pdf
…and so on.

I managed to not care until I learned that a Java app some folks use that references these files chokes and crashes on the longer file names. Changing the Java app which is older than dirt isn’t likely anytime soon.

My command of regular expressions is very tenuous at best and I’m struggling to find these files and safely rename all of them to short and unique file names while keeping them in their current directories. Bonus if I can keep them in the same listing order because I’m not sure if we care about that.

I’ve dabbled with for loops and the rename or mv command but I’m not smart enough to not do damage.

https://redd.it/119812p
@r_bash
I’ve been passed a bash noscript and have no idea where to start

I’m not looking for someone to explain this, rather point me in the right direction (unless you have time and feeling willing).

I’m no AWS expert and have adopted an EC2 instance which has user data (a bash noscript) for when the server spins up.

I haven’t a clue what any of it is doing in order to debug. Where do I start?

https://pastebin.com/qeKtkHNq

https://redd.it/11994gn
@r_bash
spec char on bash

got a version of bash that ignores special chars z(-./) in progs or at bash prompt. ics4 android craig notebook zielmicha android emacs. it's a full gnu emacs except for this. there is a github for it

https://redd.it/119zn06
@r_bash
API key variable won't work with curl

If I have:

#!/bin/bash

APIKEY="myapikey-123-45"

curl --location '
https://api.website.com/v2/endpoint' \
--header 'X-API-Key: ${API
KEY}' \
--header 'Content-Type: application/json' \
--data '@test.json'

I get:

{"errors":{"Invalid Authentication":"Provided API Key Header Authentication credentials were invalid"}}

But if I have:

#!/bin/bash

curl --location 'https://api.website.com/v2/endpoint' \
--header 'X-API-Key: myapikey-123-45' \
--header 'Content-Type: application/json' \
--data '@test.json'

It works fine. Why? How do I get curl to recognize the API_KEY variable?

https://redd.it/11a1v8r
@r_bash
I F***ed up

I was fixing some stuff in a small cs app and needed to remove './bin', but forgot the '.' and removed /bin. I used sudo, and I have no idea why.

I just snagged /bin from a kali VM. I have no idea what in total is lost. I'll only know when I need to run a command, and I can't run it because it's gone.
I have a custom backup manager and created a backup of my entire $HOME directory because I was ready to reset my OS.

https://preview.redd.it/e048fjiupzja1.png?width=818&format=png&auto=webp&v=enabled&s=760de143c9cb3933d5b50855e6a7aa744a54fe43

I am not anywhere near a pro with bash, so I have no idea if this will have bad long-term effects on my machine.

Will bash be okay even though I've just performed brain surgery on it?

https://redd.it/11a6j07
@r_bash
AWK wildcard, is it possible?

I have a file.txt with contents below:

02/23/2023 | 06:56:31 | 1| COM| Q| T| | 02/23/2023 | 07:25:00 | 07:30:00
02/23/2023 | 06:56:31 | 2| Ord Sh| Q| T| | 02/23/2023 | 07:25:00 | 07:30:00
02/22/2023 | 07:10:02 | 3| c.CS| Q| D1| | 02/23/2023 | 00:00:01 | 00:00:01
02/21/2023 | 19:50:02 | 4| p Inc| Q| D2| | 02/23/2023 | 00:00:01 | 00:00:01
02/21/2023 | 19:50:02 | 5| s Cl A | Q| D3| | 02/23/2023 | 00:00:01 | 00:00:01

I would like to search the 6th column for 'D'
Expected result:

02/22/2023 | 07:10:02 | 3| c.CS| Q| D1| | 02/23/2023 | 00:00:01 | 00:00:01
02/21/2023 | 19:50:02 | 4| p Inc| Q| D2| | 02/23/2023 | 00:00:01 | 00:00:01
02/21/2023 | 19:50:02 | 5| s Cl A | Q| D3| | 02/23/2023 | 00:00:01 | 00:00:01

I've tried several variations of the command below, but I just can't figure out the proper way to do the wild card. Is it even possible?


awk -F "|" '$6 == "D"' file.txt

https://redd.it/11abkw5
@r_bash
Grep whole word

I've done this before so I don't understand why I'm having such a hard time getting grep to match a whole word and not part of a word.

I'm trying to match /dev/nvme1n1 and not /dev/nvme1n1p1 or /dev/nvme1n1p2 etc.

# num=1
# nvme list | grep -e /dev/nvme${num}
/dev/nvme1n1 22373D800812 WDBLACK SN770 500GB <-- I want only this line
/dev/nvme1n1p1 22373D800812 WD
BLACK SN770 500GB
/dev/nvme1n1p2 22373D800812 WDBLACK SN770 500GB
/dev/nvme1n1p3 22373D800812 WD
BLACK SN770 500GB

I've tried all the regex flavors grep supports trying to get it match /dev/nvme${num}\\b or "/dev/nvme${num} " ending in a space. But nothing works.

None of these return anything:

# nvme list | grep -e '/dev/nvme'$num'\b'
# nvme list | grep -e /dev/nvme$num'\b'
# nvme list | grep -e "/dev/nvme$num\b"
# nvme list | grep -e /dev/nvme$num\\b
# nvme list | grep -G /dev/nvme$num\\b
# nvme list | grep -P /dev/nvme$num\\b
# nvme list | grep -E /dev/nvme$num\\b
# nvme list | grep -e "/dev/nvme${num}\b"
# nvme list | grep -E "/dev/nvme${num}\b"
# nvme list | grep -P "/dev/nvme${num}\b"
# nvme list | grep -G "/dev/nvme${num}\b"
# nvme list | grep -G "/dev/nvme${num} "
# nvme list | grep -P "/dev/nvme${num} "
# nvme list | grep -E "/dev/nvme${num} "
# nvme list | grep -e "/dev/nvme${num} "
# nvme list | grep -w /dev/nvme${num}
# nvme list | grep -w /dev/nvme$num
# nvme list | grep -w nvme$num

What am I missing?

https://redd.it/11aine7
@r_bash
Show curl progress bar from noscript within noscript

If I have in a noscript:

echo $URL

curl --progress-bar \
-H "X-API-Key: ${APIKEY}" \
-X POST \
--data-binary "$MEDIA
PATH" \
$URL \
| cat

And I run the noscript, it shows the progress bar.

But if I call on that noscript with another noscript:

echo -e "\nUploading to SA...\n"
( . /usr/bin/transfernotify-sauploader.sh )

It will echo the $URL but it won't show the progress bar. How do I get the progress bar to display in the main noscript (which is a systemd service)?

https://redd.it/11avz2k
@r_bash
I need help with this conditional. For some reason its not working the way is intended.

Please don't belittle me, I feel pretty dumb already.

But this noscript I wrote is to check all the subdirectories, and check if there is a .rar file, and only extract it if there is not an .mkv, .mp4 or .avi file. The thing is that my conditional always seems to evaluate to true.

The reason for the conditional is so I can run the noscript again and not to extract again the the rar files if they have already been extracted. However, when I run the noscript on a folder where there is a .rar file and no .mkv, it exctacts it, but when I run the noscript again, it will attempt to extract it again even though the condition states not to run the unrar command if one of the other 3 filetypes are already there.

Can someone take a quick look at the code and see what am I doing wrong? Thanks in advance.

#!/bin/bash

# Find all .rar files in the current directory and all
subdirectories
find . -type f -name '.rar' | while read rarfile; do
# Get the directory containing the .rar file
dir=$(dirname "$rarfile")
# Check if a .mp4, .mkv, or .avi file is in the same directory
if ! ls "$dir"/
.mp4 "$dir"/.mkv "$dir"/.avi 1> /dev/null 2>&1; then
# If no .mp4, .mkv, or .avi file is found, extract the .rar file
echo "Extracting $rarfile..."
unrar x "$rarfile" "$dir"
fi
done





Edit: I tried the following changes uding compgen instead and I still get same results.


#!/bin/bash

# Find all .rar files in the current directory and all subdirectories
while read -r rarfile; do
# Get the directory containing the .rar file
dir=$(dirname "$rarfile")
# Check if a .mp4, .mkv, or .avi file is in the same directory
if ! compgen -G "$dir/.{mp4,mkv,avi}" >/dev/null; then
# If no .mp4, .mkv, or .avi file is found, extract the .rar file
echo "Extracting $rarfile..."
unrar x "$rarfile" "$dir"
fi
done < <(find . -type f -name '
.rar')

https://redd.it/11az0u0
@r_bash
What would a noscript like this look like?

In short, I want a noscript that, when executed, changes the contents of my clipboard.

&#x200B;

For example, if https://reddit.com/r/bash is copied to the clipboard, I want my noscript to change it to https://teddit.net/r/bash (in other words, if reddit.com is there, change it).

&#x200B;

I want the same for other websites as well (https://youtube.com/watch?v=dQw4w9WgXcQ becomes https://piped.kavin.rocks/watch?v=dQw4w9WgXcQ)

&#x200B;

&#x200B;

Also, how do you make a noscript executable just by pressing specific keys?

https://redd.it/11b0n1d
@r_bash
Create case statement dynamically

I've been trying to put together a function to access and edit gists using the github gh command. I've succeeded in getting the correct format for the case options but just trying to piece it all together is a bit troublesome.

The code snippet is:

paste <(seq $(gh gist list --limit 15 | wc -l); gh gist list --limit 15 | awk '{ print " gh gist edit "$1 " ;;" }') | pr -2 -t -s")"

This outputs code similar to this:

1) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

2) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

3) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

4) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

5) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

6) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

7) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

8) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

9) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

10) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;

11) gh gist edit XXXXXXXXXXXXXXXXXXXX ;;


I have tried to figure out how to basically inject this code into a case statement. I've found a bit of code that seems like it should accomplish what im looking for but I can't figure out how to get it to run correctly.

The current noscript is:

#!/bin/bash
set -x
paste <(seq $(gh gist list --limit 15 | wc -l); gh gist list --limit 15 | awk '{ print " gh gist edit "$1 " ;; # " $2 }') | pr -2 -t -s")" > .gist.txt
. <( awk -F= 'BEGIN { print "gistlist() {"
print "case \"$choice\" in" }
{ print "$0" }
END { print "esac"
print "}" }' .gist.txt )

clear & paste <(seq $(gh gist list --limit 15 | wc -l); gh gist list --limit 15 | awk '{ print ") gh gist edit "$1 " ;; # " $2 }') | pr -2 -t -s" "
read -p "pick gist to edit: " -r choice
"$gistlist"

What can I change to make this work the right way or what could be a better way to work this. As is It shows up to 15 gists and will change with new gists added/removed. Once we can get that working, I should be able to add the option to use `gh view` and `gh delete` with the selected gist bit one step at a time. Any help is greatly appreciated


* edit

I got a bit closer with:

#!/bin/bash
set -x
paste <(seq $(gh gist list --limit 15 | wc -l); gh gist list --limit 15 | awk '{ print " gh gist edit "$1 " ;; # " $2 }') | pr -2 -t -s")" > .gist.txt
. <( awk -F= 'BEGIN { print "gistlist() {"
print "case \"$choice\" in" }
{ print "$0" }
END { print "esac"
print "}" }' .gist.txt )

# Within your while loop (or wherever else you want):
clear & paste <(seq $(gh gist list --limit 15 | wc -l); gh gist list --limit 15 | awk '{ print ") gh gist edit "$1 " ;; # " $2 }') | pr -2 -t -s" "
read -p "pick gist to edit: " -r choice
#"$gistlist"
paste <(seq $(gh gist list --limit 15 | wc -l); gh gist list --limit 15 | awk '{ print " gh gist edit "$1 " ;; # " $2 }') | pr -2 -t -s")" | awk -F= 'BEGIN { print "case \"$choice\" in" }
{ print $0 }
END { print "esac"}'

https://redd.it/11bpkhv
@r_bash
Why is this only triggering on Saturday and not

This was working on a previous installation of Linux Mint. I have since migrated to MxLinux. So much more lightweight. I transferred some bash noscripts over but now it triggers only on Saturdays a 1600. when before it triggered M-F as expected. I removed everything that is not needed below the time checks as it is not necessary. I have checked my date and time on my OS to ensure it is correct.

The actions under the initial time and date check are performing 7 days a week instead of the days of the week of 1-5 also. ZSH is also needed due to needing some to read decimals which bash cannot correctly complete.

&#x200B;

The end of day notify triggers only on Saturday at 1600. I have changed nothing from one OS to another during the migration.

#!/usr/bin/env zsh
export TERM=xterm-256color
eodnotify=15:59
while true ; do
if [ "$(date +"%T")" > '09:30:00' ] && [ "$(date +"%T")" < '16:00:00' ] && [ "$(date +"%u")" -ge 1 ] && [ "$(date +"%u")" -le 5 ]; then
<actions 1>
if <actions 1 check>; then
<actions>
fi
elif [ "$(date +%H:%M)" == *"$eodnotify"* ];then
<actions>
fi
done

https://redd.it/11bw6vg
@r_bash
Shellcheck says my code is okay when it isn't

Why does shellcheck say the following is okay,

#!/usr/bin/env bash
for drive in /dev/sata[0-9]{1,2}; do echo "$drive"; done

when it does not work as I expected:

# for drive in /dev/sata[0-9]{1,2}; do echo "$drive"; done
/dev/sata[0-9]1
/dev/sata[0-9]2

I've got it working by using:

#!/usr/bin/env bash
for drive in /dev/sata*; do
if [[ $drive =~ /dev/sata[1-9][0-9]?$ ]]; then
echo "$drive"
fi
done

Which returns the names of my 4 sata drives (and excludes the partitions on each drive):

/dev/sata1
/dev/sata2
/dev/sata3
/dev/sata4

But I'm curious why shellcheck led me down a dead end?

https://redd.it/11c2355
@r_bash
How many shell noscripts are supporting your app or mission critical services?

You know: those quick and dirty ones you develop to meet a deadline yet they live on and are impossible to remove?

I ask not only out of curiosity but also to offer some solutions.

https://redd.it/11c3ef0
@r_bash
How can I display a message on terminal in a bash noscript when the user launches ctrl+c on the gnome-terminal in a way that does not interrupt the bash noscript execution?

How can I display a message on terminal in a bash noscript when the user launches ctrl+c on the gnome-terminal in a way that does not interrupt the bash noscript execution? That means the noscript should not come to a halt or change the execution flow or results. Except for the message on terminal, it should just run as if no ctrl+c had been used.

Thanks

https://redd.it/11cbjch
@r_bash
Why is sed not applied to the output in this noscript?

ssh -T root@somehost << EOL
wget https://somesite | tee -a wget.log | sed '/blah/d'
EOL

https://redd.it/11cmm2p
@r_bash