r_bash – Telegram
search and replace (if not equal)

I am hoping someone can help

I need to create a simple bash noscript

read through /etc/hosts

if IP does not equal "X.X.X.X" then rename it to Y.Y.Y.Y

I can run that whenever I would like and just change the IPs as necessary

Similarly, I want to do the same with printers.conf with a twist

if printer name starts with A, then change IP to X.X.X.X if printer name starts with B then change IP to Y.Y.Y.Y

Can anyone help with that, please

thank you

https://redd.it/12ex3mq
@r_bash
Properly attach tmux session Jupyter Lab

Lately I have been running long Python noscripts in Jupyter lab on a remote server. However, when the server disconnects for some reason I lose all the progress, and I need to start over. Sorry in advance, I'm really new to this so I might sound plain stupid.

So I learned that a work around for this is to use tmux, in which I operate a noscript in the background? However, I am having trouble with it.

So first I open the ssh connection with the server in bash. Then I create a new tmux session (I have no other tmux sessions). Then I type the python environment I want to use and jupyter lab to open it in the browser.

Okay so I am running all the cells and then de-attach the tmux using ctrl+b and d. All good (I assume).

But then when I disconnect, and then try to connect again by attaching the tmux, I only get the log info. How am I supposed to open Jupyter lab again properly? I cannot type in that window. (Excuse my ignorance)

Anyways, I have tried creating a new pane using 'ctrl+b and %' and typing in the environment and jupyter lab to open. And also another window. I also just used the link it provides in the log window. In all cases, when I go to the noscript, it is not running at all, and there is no output?? I don't understand what I really need to do, or what went wrong.

I've also lost so much time on this lol, so currently I am running my code in the middle of the night, with my laptop on, the old fashion way. I'm going to sleep in a bit hoping the server didn't disconnect. But I feel like there gotta be something I am missing or just doing wrong. Does anyone know? I can provide more information. Thanks for the help anyway!

https://redd.it/12f3tam
@r_bash
yt-dlp file name variable

Hello I am unable to get the file name created in the previous line by yt-dlp. I guess I could slice it when I input the url but I would like to build on this noscript later to do be able to handle whole playlists. Hoping a bash wizard out there has a good solution. Thank you.

​

#! /bin/bash

timestamp=$(date +%Y-%m-%d:%H:%M)

echo "enter youtube url"
read url
yt-dlp "$url" --list-formats
echo $file
echo "Enter the resolution of the clip or leave blank for audio only"
read res


# Uses || logical operator that should the first command fail runs the second one to only get audio.
yt-dlp -f "bvheight=$res+ba" "$url" -o '%(id)s.%(ext)s' || yt-dlp -f "ba" "$url" -o '%(id)s.audio.%(ext)s'

# can not get the file name created in line 14
ffmpeg -i "$%(id)s.audio.webm" -c:a libvorbis -b:a 64k "$file.audio.ogg"

https://redd.it/12frno2
@r_bash
what am I doing wrong

This is a function that will install a window manager based on a ps3 prompt


these are the variables
#wm's (wms_inst)

PS3="Would you like to install any's: "
options=("SpectrWM" "CWM" "Qtile" "Hyprland" "AwesomeWM" "Skip")

this is the function

#installs window managers

wms_inst () {
select opt in "${options[@]}"
while True
do
case $opt in

"SpectrWM", "spectrWM", "Spectrwm", "spectrwm")
nix-env -iA nixpkgs.spectrwm
;;

"CWM", "cWM", "Cwm", "cwm")
nix-env -iA nixpkgs.cwm
;;
"Qtile", "qtile")
nix-env -iA nixpkgs.qtile
;;

"Hyprland", "hyprland")
nix-env -iA nixpkgs.hyprland
;;

"AwesomeWM", "awesomeWM", "Awesomewm")
nix-env -iA nixpkgs.awesome
;;

"Skip", "skip")
read -p "Do you want to select another Y/N " wmselect;
if [ "$wmselect" = "y" ]; then
done
fi
;;
*) echo "invalid option";;
esac
done
}

It is failing at this part
`select opt in "${options[@]}"
while True
do`
there is an error with the select and the while loop but I cannot figure it out.
does anyone know the answer?

https://redd.it/12fst5l
@r_bash
Need to unhide output

I have an embedded system I'm poking around in and from what I gather it's running bash. I'm taking a wild guess based on looking through the firmware.

I can get out of the application and drop to a shell however the following happens: blinking cursor, can type anything but when I hit enter it drops the blinking cursor to a new line and no output from the command is posted to the screen.

Using the Up, down, left, right keys produce characters such as, "^[D" when the left key is pressed, etc.

CTRL +ALT + DEL restarts the machine.

Enter moves the blinking cursor to the next line.

ESC produces "^[".

Tab works and moves the cursor over like it would in Windows.

What I'm looking for is a way to get the terminal/she'll to echo the output of the command or show the command prompt so I can see what's going on.

Any ideas? Excuse me if I'm in the wrong sub.

[https://redd.it/12g1knv

@r_bash
confusion over read ...or maybe printf?

So I'm doing some synthetic testing in order to better understand how read works, but now I think I'm getting tripped up with how printf works? The only thing I know for certain is I am confused.

I'm feeding the two arguments 'hello\' 'world' to printf with the format %s\t%s\n. The 'hello\\' is deliberate as to negate the '\\t' escape sequence. The input stream is fed into the read command and I echo the variable. I'm expecting this to be pretty straight forward, with the output being something like 'helloworld', wherein the tab has been negated and the string is without any whitespace. After which I was expecting to add the -r option to read and see something like hello\ world because the backslash would be ignored as an escape character. When I actually run the command though...

bash-5.2$ read readitem < <(printf '%s\t%s\n' 'hello\' 'world') && echo "$readitem"
hello world

The \\t escape sequence is seemingly not negated, and the '\\' escape character itself is gone. Compare this to negating a newline in the format %s\n%s\n...

bash-5.2$ read readitem < <(printf '%s\n%s\n' 'hello\' 'world') && echo "$readitem"
helloworld

And it works as I expect, with the \n character being negated. Why am I able to escape the newline but not the tab?

https://redd.it/12g8wvz
@r_bash
bash returns me an error when reading a string with "/" from CSV file

This error `sed: -e expression #1, character 20: unknown option for s'` happens when `sed` reads a `/`.

I have a code that reads a CSV file, where there are cells with `/`. Here's a small portion of the code:

while read col1 col2
do
name1 = $col1 # here are cells that include "/"
name2 = $col2
for i in ${!name1[@]}
do
sed -i s/$name_place/"${name1[i]// /\\ }"/ file.noscript
# + more stuff here
done
done < $CSVfile

How can I fix it so the output includes the `/`?

https://redd.it/12gmok0
@r_bash
need help implementing s feature into a noscript

I have been working on a noscript that allows me to choose a host in my ~/.ssh/config file in my termux installation on my phone. The noscript works great as is.

The noscript allows for pushing a file or folder to the selected remote host or pulling a remote folder to my phone. I've gotten the push files section to allow for switching between selecting [a] file or folder.

I want to be able to do the same with the pull section. I can currently either set it for files or folders which is ok but I know it can be done just haven't figured out how yet.

Also another thing I have been trying to implement is selecting multiple files or folders and getting it to properly transfer I currently get an error when selecting multiple files that seems to come from fzf output it looks like this

`rsync: [sender] change_dir "/data/data/com.termux/files/home/.shortcuts/home\#012/data/data/com.termux/files/home/.shortcuts/love\#012/data/data/com.termux/files/home/.shortcuts" failed: No such file or directory`

Here is the current noscript any suggestions or help is very much appreciated

#!/bin/bash

function get-remote-hosts {
grep -iw host ~/.ssh/config | grep -v "exec" | cut -d ' ' -f 2
}

function backup-files {
mapfile -t REMOTE_HOSTS < <(get-remote-hosts)

if [ ${#REMOTE_HOSTS[@]} -eq 0 ]; then
printf "No remote hosts found in ~/.ssh/config\n"
return 1
fi

printf "Select a remote host:\n"
select remote_host in "${REMOTE_HOSTS[@]}"; do
printf "Select an option:\n"
select option in "Push files to remote directory" "Pull files from remote directory" "Exit" ; do
case "$option" in
"Push files to remote directory")
if command -v fzf >/dev/null 2>&1; then
FZF_DEFAULT_COMMAND='find "$HOME"/ /sdcard/ -maxdepth 2 -type f | grep -iv music | sort -f'
local_source_dir=$(eval "$FZF_DEFAULT_COMMAND" | sort -f | fzf --bind 'ctrl-d:reload(find "$HOME"/ /sdcard/ -maxdepth 1 -type d | sort -f),ctrl-f:reload(find "$HOME"/ /sdcard/ -maxdepth 2 -type f | grep -iv music | sort -f)' -m --layout=reverse --reverse)
else
local_source_dir=$(dialog --stdout --noscript "Select a directory" --dselect "$HOME/" 14 48)
fi

exit_status=$?
if [ $exit_status -ne 0 ]; then
printf "Error: Failed to select local directory\n"
return 1
fi

temp_file=$(mktemp)
ssh "$remote_host" 'find $HOME/ /media/nowhereman/nowhereman/ -maxdepth 1 -type d | sort -f' | sort -f > "$temp_file"

if command -v fzf >/dev/null 2>&1; then
remote_dest_dir=$(cat "$temp_file" | fzf --reverse)
else
remote_dest_dir=$(dialog --stdout --noscript "Select a remote directory" --fselect "$temp_file" 14 48)
fi

rm "$temp_file"

exit_status=$?
if [ $exit_status -ne 0 ]; then
printf "Error: Failed to select remote directory\n"
return 1
fi
if rsync -avz --progress "$local_source_dir" "$remote_host":"$remote_dest_dir"; then
printf "Files pushed successfully\n"
else
printf "Error: Failed to push files\n"
return 1
fi
break
;;

"Pull files from remote directory")
temp_file=$(mktemp)
ssh "$remote_host" 'find $HOME/
/media/nowhereman/nowhereman -maxdepth 1 -type d' | sort -f > "$temp_file"

if command -v fzf >/dev/null 2>&1; then
remote_source_dir=$(cat "$temp_file" | grep -iv pictures | fzf --reverse)
else
remote_source_dir=$(dialog --stdout --noscript "Select a remote directory" --fselect "$temp_file" 14 48)
fi

rm "$temp_file"

exit_status=$?
if [ $exit_status -ne 0 ]; then
printf "Error: Failed to select remote directory\n"
return 1
fi

if command -v fzf >/dev/null 2>&1; then
local_dest_dir=$(find "$HOME"/ /sdcard/ -maxdepth 1 -type d | sort -f | fzf --reverse)
else
local_dest_dir=$(dialog --stdout --noscript "Select a directory" --dselect "$HOME/" 14 48)
fi

exit_status=$?
if [ $exit_status -ne 0 ]; then
printf "Error: Failed to select local directory\n"
return 1
fi

if rsync -avz --progress "$remote_host":"$remote_source_dir" "$local_dest_dir"; then
printf "Files pulled successfully\n"
else
printf "Error: Failed to pull files\n"
return 1
fi
break
;;
"Exit")
break
;;
*)
printf "Invalid option. Please select a valid option.\n"
;;
esac
done
break
done
}

PS3="𝝅) "

# Execute the backup-files function
backup-files

https://redd.it/12hbdwl
@r_bash
Delete only files in a directory with a specific extension when there isn't also a file with the same name, but a different extension?

Basically, Skyrim can't delete old *.skse files when it updates or deletes its *.ess save files, leaving lots of orphaned *.skse files that are now useless (only the most recent is used for anything and can sometimes break a savefile if it goes missing). Currently over 10,000 files in the directory, which is quite annoying to prune by hand. :/

So, how would I go about mass-deleting *.skse files that don't have a matching *.ess? The types of filenames I'm looking at are...

Quicksave0C385D2FF153686F6B6574686961Tamriel00104020230407173815221.skse
Quicksave0C385D2FF153686F6B6574686961Tamriel00104120230407173849221.skse
Quicksave0C385D2FF153686F6B6574686961Tamriel00104420230407174210221.skse
Quicksave0C385D2FF153686F6B6574686961Tamriel00111820230407181617231.skse
Quicksave0C385D2FF153686F6B6574686961Tamriel00115620230407185421241.ess
Quicksave0C385D2FF153686F6B6574686961Tamriel00115620230407185421241.skse

So, as an example, I'd like to be able to delete the first four *.skse files since they have no matching *.ess files, but keep the last two because they properly pair.

I could, I guess, copy the paired files (there's only a few dozen, though finding them in the list would be painful) somewhere safe, then just rm the remaining, but I'd rather be able to slap a noscript in ~/.local/bin to run whenever I happen to think of it...

Thank you in advance.

https://redd.it/12hj8we
@r_bash
Sshto update

Hi, I've played a bit with this new filter feature and it's proved to be very handy. But I've noticed few bugs:
1. It creates dupes in the list if same hosts scattered through subgroups
2. 'Select hosts' didn't work for filtered group
3. 'Edit conf' didn't work for filtered group
These are fixed, but who knows how many remains) I've also add 'quick commands' section to the CONTENTS view to be able to start filter from there. And renamed CONNECT button in first menu to CONNECT/SELECT to correspond new feature.
The link to sshto

Enjoy)

https://redd.it/12htrl2
@r_bash
grep and force header (show filename) behaviour. How to format the output?

I have very basic noscript along the lines

for i in $(run some command that gives a list of files)
do grep -H $i ^pattern
done

which is giving me the output

/home/user1/textfile1.txt:pattern is matched in this line
/home/user1/textfile2.txt:pattern is a match here in this line
/home/user1/textfile33.txt:pattern is a word in this file

That is, the filename and the matched grep are concatenated altogether on one line with a colon field separator in between.

What I'd like, without too much more effort, is to put the output on separate lines more like

/home/user1/textfile1.txt
pattern is matched in this line
/home/user1/textfile2.txt
pattern is matched here in this line
/home/user1/textfile33.txt
pattern is matched

Can grep do this? Or do I need to get complicated with the noscript and do something stupid like run grep twice or use an if to print file name if grep returns a result?

Also, using sed to change that colon into a line break isn't going to work where either the filename/path or the matched line include a colon. Grep doesn't appear to have an option to change the field separator from colon to anything else, either (very interesting design choice, I think).

https://redd.it/12i4iqz
@r_bash
Well, experts, show me the way. Not able to get the construction right off if loop

Here is a short program, which restricts the search within the directory(how trivial it may sound to ya)

#!/usr/bin/env bash

1

2 check_git="git -C $PWD rev-parse"

3

4 usage()

5 {

6 echo Provide name with regex delimeters.

-- 7 echo Example : $(basename $0) \"*bash*\"

8 }

9

10 if [ $# -eq 0 ];then

11 usage

12 exit 1

13 fi

14

15 local_search()

16 {

17 if [[ "$(eval "$check_git")" == "" ]];then

18

19 git grep -n "$1"

20

21 elif [[ ! -d .git ]];then

22

23 find "$PWD" -type f,d -name "$1" -ls

24

25 else

26 :

27 fi

28 }

29

30 local_search "$@"

&#x200B;

Alright, now running this inside a git repo produced the right output , like this ,

~/LaTeX_Workouts [master|]

08:15 $ local_search "tex"

Bhaskar_Chowdhury.log:1:This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020 Gentoo Linux) (preloaded format=pdflatex 2020.8.23) 23 AUG 2020 10:59

&#x200B;

Now, running this on a normal directory i.e. not a git repo, produces this:

tp_x250_08:17:40_Tue Apr 11: :~/Music>local_search "Mark"

fatal: not a git repository (or any of the parent directories): .git

fatal: not a git repository (or any of the parent directories): .git

&#x200B;

In short, it is not working as "expected" due to the logical flaw in the loop construct, I am not able to fix it.

So, shed some light. ...and please be precise.

https://redd.it/12i6gv7
@r_bash
When using bash variable operators such as${x[@]##y}, can you "anchor" what matches y to the start/end of the variable / array field?

TL;DR: Im looking for functionality in pure-bash matching like what ^ and $ provide in regex matching.



Say I have an array

a=('hi' '-hi' 'hihi' 'hiya' 'hi how are you' 'hi' 'hi'$'\n''there')

Now say I want to clear (replace with `''`) the two fields that contain exactly the string 'hi' and nothing else. Can this be done in an `a=("${a<someOperator>}")` sort of way?

Now I mean I know a bunch of ways to do this. For example, one can do this in a single-line command that uses external commands, pipes, and bash substitution. e.g.,

mapfile -t a < <(printf '%s\n' "${a[@]//$'\n'/$'\034'}" | grep -vE '^hi$' | tr $'\034' $'\n')

I also know a "pure-bash" way that one can do this that uses a temporary non-array variable. e.g.,

a0="$(printf '%s\n' '' "${a[@]//$'\n'/$'\034'}")"
mapfile -t a <<< "${a0//$'\n'hi$'\n'/}"
a=("${a[@]//$'\034'/$'\n'}")

And of course the good old fashioned loop

for kk in "${!a[@]}"; do [[ "${a[$kk]}" == 'hi' ]] && unset a[$kk]; done

but I dont know how to do this in a single command without pipes / subshells / external commands / copying data into a temporary variable / loops (which `a=("${a<someOperator>}")` would seemingly provide). Is this possible?

Thanks in advance.



P.S. Note that the first two methods above methods temporarily replace newlines that are in the array field data with the ASCII field separator (FS) control code (octal 34). In the unlikely event that the array field data already has any of these FS control characters naturally, a different temporary newline replacement (that isnt already present in the data) would need to be used.

https://redd.it/12i7sxy
@r_bash
How to create log of all file modifications with timestamps?

Hi,

Bash newbie here. I'm trying to create a noscript that will create a log of all file modification operations done across my mac for a set time period (year ish). So it'll look something like

[01/02/23\] - fileA.txt OPENED

[01/02/23\] - fileA.txt MODIFIED

[01/06/23\] - fileB.txt DELETED

[01/15/23\] - fileA.txt DELETED

etc...

Not looking to be handed the answers. Just some guidance on where to look. An idea I had was to use the bash noscript to access the mac logs and filter by file operations, and then output those. But I can't find logs that have that file modification info.

https://redd.it/12i5v16
@r_bash
How to add progress bar and log to this noscript?

https://raw.githubusercontent.com/mfn77/Post-Install/main/Post-Install.sh

I wrote a bash noscript for my post install configuration. I am definetely not an expert and this code probably is garbage but it works for me, at least it looks like it 's working. But I am wondering that can I add progress bar between user interactions and write a log file instead for normal terminal output? If I can how?

https://redd.it/12iizna
@r_bash
rclone copy (or sync) with fzf

I'm not sure if I've ever posted any of my code like this; it's usually only in some comment responding to someone that I do that

but this little bash noscript, barely 35 lines if you leave out the comments and the "help" text, seems interesting enough that someone might care to try it

the use case is when you want to run rclone copy (or sync) but you'd like to (a) select what to copy using fzf, and (b) just as importantly, you want to copy multiple directories (or multiple files)

this ability to select is especially useful if you're copying from a remote to a local directory

code is at https://github.com/xkcd386at/noscripts/blob/master/fclone if you want to take a look

https://redd.it/12ijnqh
@r_bash
Adding portions of strings in tab delimited file to another column

Hi,

I have a tab separated file (it's an .ALE file exported from Avid Media Composer). I am trying to add portions of one column's data into other columns on the same row. The second column in this file is a list of filenames. Each filename has specific information about the file which I'd like to add in the same row but under another column.

For example, I have the following columns: Name, Scene, Camera, Take

Name Scene Camera Take
302012_0017_2-33f_1b_b026.mov
302012_0016_2-33f_1a_a026.mov
302012_0019_2-33f_2a_a026.mov
302012_0024_2-33f_3c_c021.mov
302012_0020_2-33f_2b_b026.mov
302012_0018_2-33f_1c_c021.mov
302012_0022_2-33f_3a_a026.mov
302012_0023_2-33f_3b_b026.mov
302012_0021_2-33f_2c_c021.mov

In the first filename, 33f would be the Scene, 1b would be the Camera, and b026 would be the Take. On the command line, I can use the following to get each section one-by-one, changing the field of the last cut command for each section, but I'm also not sure how to iterate through the filename to get each part.

echo {302012_0017_2-33f_1b_b026.mov%.*} | cut -d - -f2 | cut -d _ -f1

Any help or pointers would be appreciated.

Thank you!

https://redd.it/12jcjug
@r_bash
I can't fathom what's wrong with this noscript. Feel like I'm missing something small but can't see what. Can anyone spot something?

Hello. I have the following noscript that scans recursively within directories for .mkv files and shows me those without subnoscripts. Everything about this seems to me that it *should* work, but when I run it I get sh: line 5: : No such file or directory errors, regardless of which directory I use as $dir. There is also never any ouput file generated. Despite the warnings, I can hear the disk scanning the directories for some time, so it's doing *something*, just not what I wanted :D

I first thought it could be a permissions issue, but even when running as root I get the same error. The noscript is of course executable and mkvmerge is indeed in my PATH. I'm a bit stumped! Thanks

&#x200B;

#!/bin/bash

# Set the directory to search for .mkv files

dir="/path/to/directory"

# Set the output file

output_file="output.txt"

# Find all .mkv files recursively in the directory and check if they have subnoscripts

find "$dir" -type f -name "*.mkv" -exec sh -c '

for file do

# Check if the file has subnoscripts

if ! mkvmerge -i "$file" | grep -q "subnoscripts"; then

# Output the file name to the output file

echo "$file" >> "$output_file"

fi

done

' sh {} +

echo "Done. Output saved to $output_file."

https://redd.it/12jexov
@r_bash