r_bash – Telegram
How to use "cut" correctly

Hi there,


I have a file with urls in it, followed by a little denoscription.

`www.lol.com`///denoscription

I want to use dmenu to open it in a brower, at the moment, I'm using this line :

cat ~/world/Documents/bookmarks | ~/.config/sway/noscripts/mydmenu -p "Bookmarks:" | xargs -0 -I {} firefox "{}"

But sometimes it fails because xargs takes the full line with the spaces and the denoscription. How would you do to still print the whole line in dmenu, but only takes the first part (url) into args ?

https://redd.it/1j9gewb
@r_bash
Collections of very useful Bash Functions

I use Bash a lot working with applications, systems, containers or networks, mgmt & integration.

I've found and frequently use a few really useful Bash Github repositories with collections of Bash "Functions" that you can use in your own Bash noscripts.  

I've learned  a lot from them and have to say my Bash noscripts now have capabilities I'd probably never been smart enough to create myself. In your own noscript(s) you just "source" the file you create or download from the following URLs: 

I am sharing this info in case someone else finds them useful.

# Collections of Functions for Bash

GUI'sEasyBashGUI: **https://github.com/BashGui/easybashgui/blob/master/docs/install.md**

>Simplified way to code bash made GUI frontend dialogs!

Script-Dialog: **https://github.com/lunarcloud/noscript-dialog?tab=readme-ov-file**

>Create bash noscripts that utilize the best dialog system that is available. Intended for Linux,
but has been tested on macOS and Windows, and should work on other unix-like OSs.

If it's launched from a GUI (like a .desktop shortcut or the dolphin file manager)
\- it will prefer kdialog in Qt-based desktops and zenity in other environments.

If neither of those are available
\- then relaunch-if-not-visible will relaunch the app in a terminal so that a terminal UI can be used.

If it's launched in a terminal
\- It will use whiptail or dialog

If neither of those are available, then it will fallback to basic terminal input/output with tools like read and echo

Collections of General Bash FunctionsBashMatic: **https://github.com/kigster/bashmatic**

>Bashmatic is a BASH framework, meaning its a collection of BASH functions (almost 900 of them) that, we hope, make BASH programming easier, more enjoyable, and more importantly, fun \- due to the library’s focus on providing the developer with a constant feedback about what is happening, while a noscript that uses Bashmatic’s helpers is running.

Bash-Concurrent: **https://github.com/themattrix/bash-concurrent**

>A Bash function to run tasks in parallel and display pretty output as they complete.

https://redd.it/1j9lkzi
@r_bash
A noscript to install llama-cpp-python with CUDA enabled

I made an auto-install noscript for myself that I thought some people might find useful or interesting. I have seen posts online where some have claimed to be unable to figure out a way to install llama-cpp-python so for those people maybe this can help and for everyone else this is just a plain fast way to do this.

1. Optionally installs Conda (which I personally recommend doing)
2. Installs the latest version of llama-cpp-python with CUDA enabled from the official llama-cpp-python GitHub repo.

GitHub - llama-installer.sh

FYI, if you choose to install Conda it links to this noscript: GitHub - install_conda.sh

Cheers guys and have a great day.

-J

https://redd.it/1j9nxxn
@r_bash
how do you combine this 2 parts: touch + strftime ("%F")?

Hi, I'd like to do touch with date today like noum of the file...

how do you do that?

example: touch ..... make this file 2025-03-12

Thank you and regards!

https://redd.it/1j9tnlh
@r_bash
xarg or sgrep or xmllint or...

All I am trying to do is get

noscript="*"

file="*"

~~~~~

noscript="*"

file="*"

~~~~~

etc


**noscript xml path is:**

/MediaContainer/Video/@noscript

(i think that is how you write noscript being the attribute?).


**file xpath is** :


/MediaContainer/Part/@file

and just write it to a file.
The "file" is always after the noscript so I am not worried about something changing in the structure.

The closest I got (but for only 1 and I have no idea how to get the pair of them) is

find . -iname '*.xml' -print0 | \
xargs -0 -r grep -ro '<Video[ \t].*noscript="[^"]*"' | awk -F: '{print $3}' >>test.txt

Any help would be appreciated.

https://redd.it/1j9zugv
@r_bash
is it crazy change rm by mv (file or dir/) ~/.local/share/Trash/files/

Hi, is it possible to do an auto-change from rm to mv file/dir ~/.local/share/Trash/files/ ?

This would avoid being wrong to erase something that I shouldn't erase, so when I do RM Bash changes the RM command for the other command that I put up.

If this is not complicated or just experts. I am not. You already see what I am wrong ...

Thank you and Regards!

https://redd.it/1japd7w
@r_bash
A simple Bash function that allows the user to quickly search and match all functions loaded in the current environment

### Idea:
- The following command will display any functions in your environment that contain a direct match to the value of the first argument passed and nothing else.

###
To return any function that contains the exact text Function: $func issue the below command (the listfunc() must be loaded into your environment for this to work), and it will return the entire listfunc() for display (and any other functions that matched as well).

list_func 'Function: $func'


list_func() {
# Determine the directory where the bash functions are stored.
if [[ -d ~/.bash_functions.d ]]; then
bash_func_dir=~/.bash_functions.d
elif [[ -f ~/.bash_functions ]]; then
bash_func_dir=$(dirname ~/.bash_functions)
else
echo "Error: No bash functions directory or file found."
return 1
fi

echo "Listing all functions loaded from $bash_func_dir and its sourced noscripts:"
echo

# Enable nullglob so that if no files match, the glob expands to nothing.
shopt -s nullglob

# Iterate over all .sh files in the bash functions directory.
for noscript in "$bash_func_dir"/*.sh; do
# Get file details.
filename=$(basename "$noscript")
filepath=$(realpath "$noscript")
fileowner=$(stat -c '%U:%G' "$noscript") # Get owner:group

# Extract function names from the file.
while IFS= read -r func; do
# Retrieve the function definition from the current shell.
func_body=$(declare -f "$func" 2>/dev/null)

# If a search term was provided, filter functions by matching the function definition.
if [[ -n "$1" ]]; then
echo "$func_body" | grep -q "$1" || continue
fi

# Print the file header.
echo "File: $filename"
echo "Path: $filepath"
echo "Owner: $fileowner"
echo
# Print the full function definition.
echo "$func_body"
echo -e "\n\n"
done < <(grep -oP '^(?:function\s+)?\s*[\w-]+\s*\(\)' "$noscript" | sed -E 's/^(function[[:space:]]+)?\s*([a-zA-Z0-9_-]+)\s*\(\)/\2/')
done
}


Cheers guys!

https://redd.it/1jb2u8k
@r_bash
Pulling hair out: SSH and sshpass standalone

I have a bit of a problem I have been scrambling to solve and am ready to give up. Ill give it one last shot:

I have a linux system that is connected to a router. THE GOAL is to ssh into the router from the linux system and run a command AND get the output. - seems simple right?

The linux system is pretty outdated. NO INTERNET ACCESS. I have access to commands on this linux system ONLY through PHP functions - don't ask me why, its stupid and I hate it. EG I can run commands by using exec(), I can create new files using file_put_contents(), etc. However because of this I can not interact with the terminal directly. I can create a .bash noscript and run that or run single commands but thats pretty much it.

It is actually over 1000 total systems. All of them running almost the same specs. SOME OF THE TARGET SYSTEMS have GNU screen.

The router uses password authentication for ssh connections. Once logged in you are NOT presented with a full shell, instead you are given a numerical list of specific commands that you can type out and then press enter.

The behavior is as follows:



FROM AN UPDATED LINUX TEST MACHINE CONNECTED TO ROUTER WHERE THE ROUTER IP IS 192.168.1.1:

ssh `admin@192.168.1.1`

type "yes" and hit enter to allow the unknown key

type "password" hit enter

type the command "778635" hit enter

the router returns a code

type the second command "66452098" hit enter

the router returns a second code

type "exit" hit enter

A one liner of this process would look something like:

sshpass -p password ssh -tt -o 'StrictHostKeyChecking=no' `admin@192.168.1.1` "778635; 66452098; exit"

Except the router does not execute the commands because for some reason it never recieves what ssh sends it. The solution that works on the TEST MACHINE is:

echo -e '778635\n66452098\nexit' | sshpass -p password ssh -o 'StrictHostKeyChecking=no' -tt `admin@192.168.1.1`

This works every time on the UPDATED TEST SYSTEM without issue even after clearing known hosts file. With this command I am able to run it from php:

exec("echo -e '778635\n66452098\nexit' | sshpass -p password ssh -o 'StrictHostKeyChecking=no' -tt admin@192.168.1.1", $a);

return $a;

and I will get the output which can be parsed and handled.



FROM THE OUTDATED TARGET MACHINE CONNECTED TO THE SAME ROUTER:

target machine information:

bash --version shows 4.1.5

uname -r shows 2.6.29

ssh -V returns blank

sshpass -V shows 1.04

The command that works on the updated machine fails. AND RETURNS NOTHING. I will detail the reasons I have found below:

I can use screen to open a detached session and then "stuff" it with commands one by one. Effectively bypassing sshpass, this allows me to successfully accept the host key and log in to the router but at that point "stuff" does not pass any input to the router and I cannot execute commands.

The version of ssh on the target machine is so old it does not include an option for 'StrictHostKeyChecking=no' it returns something to the effect of "invalid option: StrictHostKeyChecking" sorry I don't have the exact thing. In fact "ssh -V" returns NOTHING and "man ssh" returns "no manual entry for ssh"!

After using screen however if I re-execute the first command now it will get farther - because the host is added to known hosts now - but the commands executed on the router will not return anything and neither will ssh itself even with verbose flag. I believe this behavior is caused by an old version of sshpass. I found other people online that had similar issues where the output of the ssh command does not get passed back to the client. I tried several solutions related to redirection but to no avail.

So there is two problems:

1. Old ssh version without a way to bypass host key checking.
2. Old sshpass version not passing the output back to the client.

sshpass not passing back the output of either ssh or the router CLI is the biggest issue - I
cant even debug what I don't know is happening. Luckily though the router does have a command to reboot (111080) and if I execute:

echo -e '111080' | sshpass -p password ssh -tt `admin@192.168.1.1`

I wont get anything back in the terminal BUT the router DOES reboot. So I know its working, I just cant get the output back.

So, I still have no way to get the output of the two commands I need executed. As noted above, the "screen" command is NOT available on all of the machines so even if I found a way to get it to pass the command to the router it would only help for a fraction of the machines.

At this point I am wondering if it is possible to get the needed and updated binaries of both ssh and sshpass and zip them up then convert to b64 and use file_put_contents() to make a file on the target machine. Although this is over my head and I would not know how to handle the libraries needed or if they would even run on the target machine's kernel.

A friend of mine told me I could use python to handle the ssh session but I could not find enough information on that. The python version on the target machine is 2.6.6

Any Ideas? I would give my left t6ticle to figure this out.

https://redd.it/1jbaskl
@r_bash
Seeking Feedback on My Bash Script for Migrating APT Keys

Hello everyone!

I recently created a Bash noscript designed to help migrate APT keys from the deprecated apt-key to the new best practices in Ubuntu. The noscript includes user confirmation before each step, ensuring that users have control over the process. I developed this noscript using DuckDuckGo's AI tool, which helped me refine my approach.

# What This Script Does:

It exports existing APT keys to the `/etc/apt/trusted.gpg.d/` directory.
It verifies that the keys have been successfully exported.
It removes the old keys from `apt-key`.
It updates the APT package lists.

# Why I Want This:

As Ubuntu continues to evolve, it's important to keep our systems secure and up to date. Migrating to the new key management practices is essential for maintaining the integrity of package installations and updates.

# Questions for the Community:

1. Is this noscript safe to use? I want to ensure that it won't cause any issues with my system or package management.
2. Will this noscript work as is? I would appreciate any feedback on its functionality or any improvements that could be made.

Here’s the noscript for your review:Hello everyone!
I recently created a Bash noscript designed to help migrate APT keys from the deprecated apt-key to the new best practices in Ubuntu. The noscript includes user confirmation before each step, ensuring that users have control over the process. I developed this noscript using DuckDuckGo's AI tool, which helped me refine my approach.
What This Script Does:
It exports existing APT keys to the /etc/apt/trusted.gpg.d/ directory.
It verifies that the keys have been successfully exported.
It removes the old keys from apt-key.
It updates the APT package lists.
Why I Want This:
As Ubuntu continues to evolve, it's important to keep our systems secure and up to date. Migrating to the new key management practices is essential for maintaining the integrity of package installations and updates.
Questions for the Community:
Is this noscript safe to use? I want to ensure that it won't cause any issues with my system or package management.
Will this noscript work as is? I would appreciate any feedback on its functionality or any improvements that could be made.
Here’s the noscript for your review:

# !/bin/bash

# Directory to store the exported keys

KEY_DIR="/etc/apt/trusted.gpg.d"

# Function to handle errors

handle_error() { echo "Error: $1" exit 1 }

# Function to prompt for user confirmation

confirm() { read -p "$1 (y/n): " -n 1 -r echo # Move to a new line if [[ ! $REPLY =\~ ^([Yy\]$) \]\]; then echo "Operation aborted." exit 0 fi }

# Check if the directory exists

if [ ! -d "$KEY_DIR" \]; then handle_error "Directory $KEY_DIR does not exist. Exiting." fi

# List all keys in apt-key

KEYS=$(apt-key list | grep -E 'pub ' | awk '{print $2}' | cut -d'/' -f2)

# Check if there are no keys to export

if [ -z "$KEYS" \]; then echo "No keys found to export. Exiting." exit 0 fi

# Export each key

for KEY in $KEYS; do echo "Exporting key: $KEY" confirm "Proceed with exporting key: $KEY?" if ! sudo apt-key export "$KEY" | gpg --dearmor | sudo tee "$KEY_DIR/$KEY.gpg" > /dev/null; then handle_error "Failed to export key: $KEY" fi echo "Key $KEY exported successfully." done

# Verify the keys have been exported

echo "Verifying exported keys..." confirm "Proceed with verification of exported keys?" for KEY in $KEYS; do if [ -f "$KEY_DIR/$KEY.gpg" \]; then echo "Key $KEY successfully exported." else echo "Key $KEY failed to export." fi done

# Remove old keys from apt-key

echo "Removing old keys from apt-key..." confirm "Proceed with removing old keys from apt-key?" for KEY in $KEYS; do echo "Removing key: $KEY" if ! sudo apt-key del "$KEY"; then echo "Warning: Failed to remove key: $KEY" fi done

# Update APT

echo "Updating APT..." confirm "Proceed with
updating APT?" if ! sudo apt update; then handle_error "Failed to update APT." fi

echo "Key migration completed successfully."

I appreciate any insights or suggestions you may have. Thank you for your help!

https://redd.it/1jaz34c
@r_bash
Using grep / sed in a bash noscript...

Hello, I've spent a lot more time than I'd like to admit trying to figure out how to write this noscript. I've looked through the official Bash docs and many online StackOverflow posts.

This noscript is supposed to take a directory as input, i.e. /lib/64, and recursively change files in a directory to the new path, i.e. /lib64.

The command is supposed to be invoked by doing ./replace.sh /lib/64 /lib64

#!/bin/bash

# filename: replace.sh

INDIR=$(sed -r 's/\//\\\//g' <<< "$1")
OUT
DIR=$(sed -r 's/\//\\\//g' <<< "$2")

echo "$1 -> $2"
echo $1
echo "${INDIR} -> ${OUTDIR}"

grep -rl -e "$1" | xargs sed -i 's/${INDIR}/${OUTDIR}/g'

# test for white space ahead, white space behind
grep -rl -e "$1" | xargs sed -i 's/\s${INDIR}\s/\s${OUTDIR}\s/g'

# test for beginning of line ahead, white space behind
grep -rl -e "$1" | xargs sed -i 's/^${INDIR}\s/^${OUTDIR}\s/g'

# test for white space ahead, end of line behind
grep -rl -e "$1" | xargs sed -i 's/\s${INDIR}$/\s${OUTDIR}$/g'

# test for beginning of line ahead, end of line behind
grep -rl -e "$1" | xargs sed -i 's/^${INDIR}$/^${OUTDIR}$/g'

IN_DIR and OUT_DIR are taking the two directory arguments, then using sed to insert a backslash before each slash. grep -rl -e "$1" | xargs sed -i 's/${IN_DIR}/${OUT_DIR}/g' is supposed to be recursively going through a directory tree from where the command is invoked, and replacing the original path (arg1) with the new path (arg2).

No matter what I've tried, this will not function correctly. The original file that I'm using to test the functionality remains unchanged, despite being able to do the grep ... | xargs sed ... manually with success.

What am I doing wrong?

Many thanks

https://redd.it/1javegq
@r_bash
find, but exclude file from results if another file exists?

I found a project that locally uses whisper to generate subnoscripts from media. Bulk translations are done by passing a text file to the command line that contains absolute file paths. I can generate this file easily enough with

find /mnt/media/ -iname .mkv -o -iname .m4v -o -iname .mp4 -o -iname .avi -o -iname .mov -o -name .mpg > media.txt

The goal would be to exclude media that already has an .srt file with the same filename. So show.mkv that also has show.srt would not show up.

I think this goes beyond find and needs to be piped else where but I am not quite sure where to go from here.

https://redd.it/1jc6ht5
@r_bash
Install NVM with bash

Anyone have a handy noscript that will install nvm + LTS nodejs with a bash noscript?

I use the following commands on an interactive shell fine, but for the life of me I can't get it to install with a bash noscript on Ubuntu 22.04.

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash && source ~/.bashrc && nvm install --lts


https://redd.it/1jc4ge2
@r_bash
How to make a noscript to populate an array in another noscript?

I'm too new to know what I even need to look up in the docs, here. Hopefully this makes sense.

I have this noscript:

#!/bin/bash

arr0="0"
arr1="1"
arr2="2"

rand=$$RANDOM % ${#arr[@}]

xdotool type ${arr$rand}

Which, when executed, types one of the characters 0, 1, or 2 at random. Instead of hard coding those values to be selected at random, I would like to make another noscript that prompts the user for the values in the arrays.

Ie. Execute new noscript. It asks for a list of items. I enter "r", "g", "q". Now the example noscript above will type one of the characters r, g, or q at random.

I'm trying to figure out how to set the arrays arbitrarily without editing the noscript manually every time I want to change the selection of possible random characters.

https://redd.it/1jc3m5l
@r_bash
My while read loop isn't looping

I have a folder structure like so:
/path/to/directory/foldernameAUTO001
/path/to/directory/foldername
002

I am trying to search through /path/to/directory to find instances where the directory "foldernameAUTO" has any other directories of the same name (potentially without AUTO) with a higher number after the underscore.

For example, if I have a folder called "testfolderAUTO001" I want to find "testfolder002" or "testfolderAUTO002". Hope all that makes sense.

Here is my loop:

#!/bin/bash

Folder=/path/to/directory/

while IFS='/' read -r blank path to directory foldername
seq; do
echo "Found AUTO of $foldernameseq"
foldername=$(echo "$foldername
seq" | cut -d -f1) && echo "foldername is $foldername"
seq=$(echo "$foldername
seq" | cut -d -f2) && echo "sequence is $seq"
printf -v int '%d/n' "$seq"
(( newseq=seq+1 )) && echo "New sequence is 00$newseq"
echo "Finding successors for $foldername"
find $Folder -name "$foldername"
00"$newseq"
noauto=$(echo "${foldername:0:-4}") && echo "NoAuto is $noauto"
find $Folder -name "noauto"00"newseq"
echo ""
done < <(find $Folder -name "*AUTO*")

And this is what I'm getting as output. It just lists the same directory over and over:

Found AUTO of foldernameAUTO
001
foldername is foldernameAUTO
sequence is 001
New sequence is 002
Finding successors for foldernameAUTO
NoAUTO is foldername

Found AUTO of foldernameAUTO001
foldername is foldernameAUTO
sequence is 001
New sequence is 002
Finding successors for foldernameAUTO
NoAUTO is foldername

Found AUTO of foldernameAUTO
001
foldername is foldernameAUTO
sequence is 001
New sequence is 002
Finding successors for foldernameAUTO
NoAUTO is foldername

https://redd.it/1jdel1z
@r_bash
DD strange behavior

Im sending 38bytes string from one device via uart to pc. Stty is configured as needed 9600 8n1. I want to catch incoming data via dd and i know its exactly 38bytes.

dd if=/dev/ttyusb0 bs=1 count=38

But after receiving 38bytes it still sits and waits till more data will come. As a workaround i used timeout 1 which makes dd work as expected but i dont like that solution. I switched to using cat eventually but still want to understand the reasons for dd to behave like that shouldnt it terminate with a status code right after 38bytes?

https://redd.it/1jgxyit
@r_bash
"return" doesn't return the exit code of the last command in a function

#!/bin/bash

bar() {
echo bar
return
}

foo() {
echo foo
bar
echo "Return code from bar(): $?"
exit
}
trap foo SIGINT

while :; do
sleep 1;
done

I have this example noscript. When I start it and press CTRL-C (SIGINT):

# Expected output:
^Cfoo
bar
Return code from bar(): 0

# Actual output:
^Cfoo
bar
Return code from bar(): 130

I understand, that 130 (128 + 2) is SIGINT. But why is the return statement in the bar function ignoring the successful echo?

https://redd.it/1jgvheu
@r_bash
send commands via stdin

Hi every one, I am working with gdb, and I want to send commands to it via stdin,

look at this commands:

`echo -e "i r\n" > /proc/\`ps aux | grep "gdb ./args2" | awk 'NR == 1 {print $2}'\`/fd/0`

and I tried this

`echo -e "i r\r\n" > /proc/\`ps aux | grep "gdb ./args2" | awk 'NR == 1 {print $2}'\`/fd/0`





I expected to send `i r` to gdb, and when I check gdb, I found the string I send "i r", but it did not execute, and I was need to press enter, how to do it without press enter.

note: I do not want to use any tools "like expect", I want to do it through echo and stdin only.





https://redd.it/1jhergb
@r_bash
Continue the noscript after an if ?

Hi there, I'm struggling :)

trying to make a small bash noscript, here it is :



#!/bin/bash

set -x #;)

read user

if [[ -n $user ]]; then

exec adduser $user

else

exit 1

fi

mkdir $HOME/$user && chown $user

mkdir -p $HOME/$user/{Commun,Work,stuff}


I am wondering why commands after the if statement won't execute ?



https://redd.it/1ji02u4
@r_bash