r_bash – Telegram
Systemd in a Debian container on ChromeOs accepts bash

Yup, as long as your noscripts executed by a systemd service starts with a bash shebang, things are good. Arrays, and [[ \]\] and other stuff works totally fine.

Even if the documentation states that the noscripts must be sh compatible.

https://redd.it/118fy6j
@r_bash
Ctrl + Meta(Windows) + Right/Left - How come it isn't well known?

So I've accidentaly (I have ctrl + alt + left/right bound as home/end) discovered that ctrl + meta + left/right in bash jumps the cursor between words/commands/arguments/whatever, separated by any non-alphanumerical sign, like whitespaces, slashes, underscores, so for example instead of manually replacing file name in a long path I can easily jump to the last slash and use alt+d to remove the filename and type another.

Why I haven't seen this anywhere in 20 years of using linux? I couldn't find any docs using Google either, and it is soooo useful.

https://redd.it/118jmc6
@r_bash
I need some bash help

I need to convert all my ps1 games to chd files (type of compressed data) and I'm having trouble with part of automating it. What i need to do is used sed to get the file names (or if there is a way to strip extensions then use that) and then for everyone one of those files extract it, go into the folder created run chdman, copy the .chd file back to the main directory with the same name then move on to the next one. How can i run that for every file named that. I could probably do it with the find command and not need to do it through bash but I'd like a file to run that would do this

https://redd.it/118mjtg
@r_bash
What is a good way to learn bash noscripting

So I'm comfortable using bash at the commandline but i know little about noscripting with it. What are some good ways to learn and practice with it

https://redd.it/118nry1
@r_bash
Import select fields from one XML into another?

I have two copies of an XML file. The file is a database for a media library with e.g. name, denoscription, play count, etc.

One copy is "dirty," it's got errors in it, entries for items that don't exist in the library, etc. But it has the play count data that I want to preserve.

The other copy is "clean," it has all the errors corrected and the missing entries removed...but it's missing the play count.

What I want to do is, for each entry in the "clean" list, if that entry exists in the "dirty" list (it always should) and if that dirty entry has a <playcount> field (it might or might not), then import that field from the "dirty" list into the "clean" list.

This feels simple and difficult at the same time. I'm not well versed in XML manipulation. Is this something we can do? I have access to xmlstarlet, or other tools available on raspberry pi legacy (arm Linux/Debian 10.)

https://redd.it/118pi26
@r_bash
ubuntu 20, 22, pi: /sh/bash:- should my 'set' display 1,000 lines of noscript along with the variables already set?

I don't know if this is normal, but I would have thought that I could display all existing variables via the $set command. And it does. But it also shows hundreds of lines of what appears to be noscript code, along with other lines. I've checked on multiple systems here and they all have it, so perhaps it's normal. But it's possible that I've modified my .bashrc or python env or other system that's called in the .bashrc, and accidentally had it read in some huge amount of irrelevant bash shell code or something. Below are some excepts. If this is normal, great, otherwise if it isn't could someone just tell me it's fux0red and I'll go and figure out the rest. I just don't want to invest a couple of hours only to find that this is normal! \[edit: too late. I just spent 2 hours checking before posting this so I don't look like an idiot. Didn't help.\]

`$set`

`BASH=/bin/bash`
`BASHOPTS=checkwinsize:cmdhist:complete_fullquote:expand_aliases:extglob:extquote:force_fignore:globasciiranges:globskipdots:histappend:interactive_comments:patsub_replacement:progcomp:promptvars:sourcepath`
`BASH_ALIASES=()`
`BASH_ARGC=([0]="0")`
`BASH_ARGV=()`
`BASH_CMDS=()`
`BASH_COMPLETION_VERSINFO=([0]="2" [1]="11")`
`BASH_LINENO=()`
`BASH_LOADABLES_PATH=/usr/local/lib/bash:/usr/lib/bash:/opt/local/lib/bash:/usr/pkg/lib/bash:/opt/pkg/lib/bash:.`
`BASH_SOURCE=()`
`BASH_VERSINFO=([0]="5" [1]="2" [2]="2" [3]="1" [4]="release" [5]="x86_64-pc-linux-gnu")`
`BASH_VERSION='5.2.2(1)-release'`
`COLORFGBG='15;0'`
`COLORTERM=truecolor`
`COLUMNS=198`
`DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus`
`DEBUGINFOD_URLS=https://debuginfod.ubuntu.com`
`DESKTOP_SESSION=plasmawayland`


... continues on for a while, then

`XDG_SESSION_TYPE=wayland`
`XDG_VTNR=2`
`XKB_DEFAULT_LAYOUT=us`
`XKB_DEFAULT_MODEL=pc105`
`_=pi`
`__git_printf_supports_v=yes`
`_backup_glob='@(#*#|*@(~|.@(bak|orig|rej|swp|dpkg*|rpm@(orig|new|save))))'`
`_xspecs=([tex]="!*.@(?(la)tex|texi|dtx|ins|ltx|dbj)" [freeamp]="!*.@(mp3|og[ag]|pls|m3u)" [gqmpeg]="!*.@(mp3|og[ag]|pls|m3u)" [texi2html]="!*.texi*" [hbpp]="!*.@([Pp][Rr][Gg]|[Cc][Ll][Pp])" [lowrite`
`r]="!*.@(sxw|stw|sxg|sgl|doc?([mx])|dot?([mx])|rtf|txt|htm|html|?(f)odt|ott|odm|pdf)" [rpm2cpio]="!*.[rs]pm" [localc]="!*.@(sxc|stc|xls?([bmx])|xlw|xlt?([mx])|[ct]sv|?(f)ods|ots)" [hbrun]="!*.[Hh][R`
`r][Bb]" [vi]="*.@([ao]|so|so.!(conf|*/*)|[rs]pm|gif|jp?(e)g|mp3|mp?(e)g|avi|asf|ogg|class)" [latex]="!*.@(?(la)tex|texi|dtx|ins|ltx|dbj)" [view]="*.@([ao]|so|so.!(conf|*/*)|[rs]pm|gif|jp?(e)g|mp3|mp`
`?(e)g|avi|asf|ogg|class)" [madplay]="!*.mp3" [compress]="*.Z" [pdfjadetex]="!*.@(?(la)tex|texi|dtx|ins|ltx|dbj)" [pbunzip2]="!*.?(t)bz?(2)" [lrunzip]="!*.lrz" [gunzip]="!*.@(Z|[gGd]z|t[ag]z)" [oowri`
`ter]="!*.@(sxw|stw|sxg|sgl|doc?([mx])|dot?([mx])|rtf|txt|htm|html|?(f)odt|ott|odm|pdf)" [epiphany]="!*.@(?([xX]|[sS])[hH][tT][mM]?([lL]))" [acroread]="!*.[pf]df" [znew]="*.Z" [kwrite]="*.@([ao]|so|s`
`o.!(conf|*/*)|[rs]pm|gif|jp?(e)g|mp3|mp?(e)g|avi|asf|ogg|class)" [xemacs]="*.@([ao]|so|so.!(conf|*/*)|[rs]pm|gif|jp?(e)g|mp3|mp?(e)g|avi|asf|ogg|class)" [gview]="*.@([ao]|so|so.!(conf|*/*)|[rs]pm|gi`


(this last one goes on for \~900 characters), then there's the start of a few hundred lines of noscript:

`CompWordsContainsArray ()`  
`{`  
   `declare -a localArray;`
   `localArray=("$@");`
   `local findme;`
   `for findme in "${localArray[@]}";`
   `do`
       `if ElementNotInCompWords "$findme"; then`
           `return 1;`
       `fi;`
   `done;`
   `return 0`
`}`
`ElementNotInCompWords ()`  
`{`  
   `local findme="$1";`
   `local element;`
   `for element in "${COMP_WORDS[@]}";`
   `do`
       `if [[ "$findme" = "$element" ]]; then`
           `return 1;`
       `fi;`
   `done;`
   `return 0`
`}`
`__expand_tilde_by_ref ()`  
`{`  
   `if [[ ${!1-}
== \~* ]]; then`
       `eval $1="$(printf ~%q "${!1#\~}")";`
   `fi`
`}`
`__get_cword_at_cursor_by_ref ()`  
`{`  
   `local cword words=();`
   `__reassemble_comp_words_by_ref "$1" words cword;`
   `local i cur="" index=$COMP_POINT lead=${COMP_LINE:0:COMP_POINT};`
   `if [[ $index -gt 0 && ( -n $lead && -n ${lead//[[:space:]]/} ) ]]; then`
       `cur=$COMP_LINE;`
       `for ((i = 0; i <= cword; ++i))`
       `do`
           `while [[ ${#cur} -ge ${#words[i]} && ${cur:0:${#words[i]}} != "${words[i]-}" ]]; do`
               `cur="${cur:1}";`
               `((index > 0)) && ((index--));`
           `done;`
           `if ((i < cword)); then`
               `local old_size=${#cur};`
               `cur="${cur#"${words[i]}"}";`
               `local new_size=${#cur};`
               `((index -= old_size - new_size));`
           `fi;`
       `done;`
       `[[ -n $cur && ! -n ${cur//[[:space:]]/} ]] && cur=;`
       `((index < 0)) && index=0;`
   `fi;`
   `local "$2" "$3" "$4" && _upvars -a${#words[@]} $2 ${words+"${words[@]}"} -v $3 "$cword" -v $4 "${cur:0:index}"`
`}`
`__git_eread ()`  
`{`  
   `test -r "$1" && IFS='`
`' read "$2" < "$1"`
`}`
`__git_ps1 ()`  
`{`  
   `local exit=$?;`
   `local pcmode=no;`
   `local detached=no;`
   `local ps1pc_start='\u@\h:\w ';`
   `local ps1pc_end='\$ ';`
   `local printf_format=' (%s)';`
   `case "$#" in`  
       `2 | 3)`
           `pcmode=yes;`
           `ps1pc_start="$1";`
           `ps1pc_end="$2";`
           `printf_format="${3:-$printf_format}";`
           `PS1="$ps1pc_start$ps1pc_end"`


I've tracked some of it down to [git-prompt.sh](https://git-prompt.sh), which I don't have installed anywhere - yet it's clearly that code. Other parts of the code resemble /usr/share/bash-completion/bash\_completion, which makes me think that perhaps this is all normal!

Is it?

https://redd.it/118u0pu
@r_bash
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