r_bash – Telegram
Whats the best resource to learn bash?

I wanna know what's the best resource to learn bash noscripting.
And for cyber security which one is better bash or zsh?

https://redd.it/1cf19sy
@r_bash
Using a launcher/.desktop file to run a bash noscript in the same folder as the launcher?

Hi,

I made a bash noscript called "AppendDate.sh" which simply appends the modification date to the filenames of any drag and dropped files. Since I can't drag and drop files directly onto .sh files, to run this noscript, I am using a launcher to indirectly run it.

The launcher works if I use a an absolute path for the noscript combined with $1 for the dropped file(s). But I would like to use a relative path in the launcher instead, so that the solution is more "portable".

On other internet pages, I have read that an Exec command like the following should work:

sh -e -c "exec \\\\"\\\\$(dirname \\\\"\\\\$0\\\\")/AppendDate.sh\\\\"" %k

But this isn't working for me, no matter where I try to add $1 (or \\\\$1).

Any ideas?




https://redd.it/1cf5hpw
@r_bash
what is a "shell language" in the context of other programming languages?

question, what is a "shell language" in the context of other programming languages?

i keep hearing the term "shell language" but when i google it i just get "shell noscript" but people keep using this term "shell language" as if it's some how different in the context of other programming languages

any ideas?

thank you

https://redd.it/1cf79a7
@r_bash
what is an "argument" in bash?

Hello, so i did a search of r/bash and i asked "what is an argument" and i got this result

https://www.reddit.com/r/bash/search/?q=arguement&type=link&cId=690c4a5d-257a-4bc3-984a-1cb53331a300&iId=9528a6b6-c3f6-4cbb-9afe-2e739935c053

and i got a lot of posts about modifying arguments, but what i noticed is i couldn't find any explanation of what an argument is, so i wanted to take this moment to ask.

what is an argument in bash? what does an argument mean?

thank you

https://redd.it/1cfbc6r
@r_bash
I use bash: is "ls -d /" the best way to see only the dirs/ into a dir?

Hi, I use bash terminal, and I found by trying that the command ls -d
/ is the way mode to see only the dirs into another dir, excluding the files.
Do you know another command for filter only the dir/ ?
Thank you and regards!

https://redd.it/1cfl0jq
@r_bash
Who implements the features of bash ?

Bash works on any kind of processor and any operating system. when i execute 'ls' it works both on windows and linux even though both use completely different file systems ? so who implements the features of bash ?


Is bash just a specification and each os / motherboard manufactures implements it according to the specification ?

https://redd.it/1cfrwut
@r_bash
Avoid 100% cpu when I read a FIFO file

Hi!
I need to read FIFO file, because it arrives a log of snmp traps in the FIFO file that I need to read and process them sequentially.
So I've created a while (true) loop to begin to read lines of FIFO file and process the output. Problem is machine increase cpu up 100% with the use of the noscript.
I don't know if I put a sleep 3s for example in noscript. Should it read all lines of fifo file or could be that it doesn't read all lines?

Thanks and sorry for my English!

https://redd.it/1cfy6gf
@r_bash
A bash noscript that's purpose is to source the latest version of a GitHub repository

# Get the Latest Version of Any Git Repository

Easily retrieve the latest version of any Git repository with a simple bash noscript!

## Usage

1. Set the url variable in your bash noscript to the URL of the repository you want to clone:

   url="https://github.com/rust-lang/rust.git"


2. Create a variable to store the repository version and echo its value:

   repo_version=$(curl -fsS "https://raw.githubusercontent.com/slyfox1186/noscript-repo/main/Bash/Misc/source-git-repo-version.sh" | bash -s "$url")
echo "$repo_version"


Or use this shorter command:

   repo_version=$(curl -fsSL "https://gitver.optimizethis.net" | bash -s "$url")
echo "$repo_version"


## Other Execution Methods

./source-git-repo-version.sh "https://github.com/rust-lang/rust.git"


## The main noscript

You can see the noscript here

## Test It Out

Check out this bash noscript to see the noscript in action with various examples!

https://redd.it/1cgizdv
@r_bash
A bash noscript that's purpose is to source the latest version of a GitHub repository

# Get the Latest Version of Any Git Repository

Easily retrieve the latest version of any Git repository with a simple bash noscript!

## Usage

1. Set the url variable in your bash noscript to the URL of the repository you want to clone:

   url="https://github.com/rust-lang/rust.git"


2. Create a variable to store the repository version and echo its value:

   repo_version=$(curl -fsS "https://raw.githubusercontent.com/slyfox1186/noscript-repo/main/Bash/Misc/source-git-repo-version.sh" | bash -s "$url")
echo "$repo_version"


Or use this shorter command:

   repo_version=$(curl -fsSL "https://gitver.optimizethis.net" | bash -s "$url")
echo "$repo_version"


## Test It Out

Check out this bash noscript to see the noscript in action with various examples!


https://redd.it/1cgiz2e
@r_bash
DriveTool.sh: A Script for Rapid and Secure File Copying to USB Flash Drives

Hello everyone,

In Linux, files are permanently written only after the partition is unmounted. This might explain why many graphical tools deliver unsatisfactory performance when writing files to USB flash drives. To address this issue, I have developed a compact noscript which, thus far, has performed effectively.

Note: I was unable to get the status=progress feature to function correctly. However, the noscript successfully copies the transferred files to the USB flash drive. After conducting multiple tests, no errors were encountered.


#!/bin/bash

declare -r MOUNT_POINT="/media/flashdrive"

# Function to check for required commands
check_dependencies() {
local dependencies=(lsblk mkdir mount umount dd cp du grep diff)
for cmd in "${dependencies[@]}"; do
if ! command -v "$cmd" &> /dev/null; then
echo "Error: Required command '$cmd' is not installed." >&2
exit 1
fi
done
}

# Function to check if device is mounted and unmount it
safe_unmount() {
local device="$1"
if mount | grep -qw "$device"; then
echo "$device is currently mounted, attempting to unmount..."
sudo umount "$device" && echo "$device unmounted successfully." || { echo "Failed to unmount $device."; return 1; }
fi
}

# Function to mount drive
ensure_mounted() {
local device="$1"
if ! mount | grep -q "$MOUNT_POINT"; then
echo "Mounting $device..."
sudo mkdir -p "$MOUNT_POINT"
sudo mount "$device" "$MOUNT_POINT" || { echo "Failed to mount $device."; exit 1; }
else
echo "Device is already mounted on $MOUNT_POINT."
fi
}

# Function to copy files or directories safely
copy_files() {
local source="$1"
local destination="$2"
local dest_path="$destination/$(basename "$source")"

if [[ -d "$source" ]]; then
echo "Copying directory $source to $destination using 'cp -r'..."
sudo cp -r "$source" "$dest_path" && echo "$source has been copied."
else
echo "Copying file $source to $destination using 'dd'..."
sudo dd if="$source" of="$dest_path" bs=4M status=progress && echo "$source has been copied."
fi

echo "Wait finishing changes..."
sudo mount -o remount,sync "$MOUNT_POINT"

# Verify copy integrity
du -b "$source" "$dest_path"
if sudo diff -qr "$source" "$dest_path"; then
echo "Verification successful: No differences found."
else
echo "Verification failed: Differences found!"
return 1
fi
}

# Function to format the drive
format_drive() {
local device="$1"
echo "Checking if device $device is mounted..."
safe_unmount "$device" || return 1
echo "Formatting $device..."
sudo mkfs.exfat "$device" && echo "Drive formatted successfully." || echo "Formatting failed."
}

# Function to display usage information
help() {
echo "Usage: $0 OPTION [ARGUMENTS]"
echo
echo "Options:"
echo " -c, -C DEVICE SOURCE_PATH Mount DEVICE and copy SOURCE_PATH to it."
echo " -l, -L List information about block devices."
echo " -f, -F DEVICE Format DEVICE."
echo
echo "Examples:"
echo " $0 -C /dev/sdx /path/to/data # Copy /path/to/data to /dev/sdx after mounting it."
echo " $0 -L # List all block devices."
echo " $0 -F /dev/sdx # Format /dev/sdx."
}

# Process command-line arguments
case "$1" in
-C | -c)
check_dependencies
ensure_mounted "$3"
copy_files "$2" "$MOUNT_POINT"
echo "Unmounting $MOUNT_POINT"
sudo umount "$MOUNT_POINT"
;;
-L | -l) lsblk -o NAME,MODEL,SERIAL,VENDOR,TRAN
;;
-F | -f)
check_dependencies
format_drive "$2"
;;
*)
help
;;
esac

​

https://redd.it/1cgpe1y
@r_bash
How do I get the number of processes spawned by a noscript?

TL;DR: What command will return a list or count of all commands spawned from the current noscript? Ideally it would include the actual commands running, eg: aws ec2 describe-instances ...

​

I have a noscript that pulls data from multiple AWS accounts across multiple regions. I've implemented limited multi-threading but I'm not sure it's working exactly as intended. The part in question is intended to get a count of the number of processes spawned by the noscript:

$( jobs -r -p | wc -l )

jobs shows info on "processes spawned by the current shell" so I suspect it may not work in cases where a new shell is spawned, as in when using pipes. I'm also not sure if -r causes it to miss processes (aws-cli) waiting on a response from AWS.

Each AWS command takes a while to run, so I let it run 2 less than the number of cores in parallel. Here's an example of it and the rest of the code/logic:

​

list-ec2(){
local LPROFILE="$1"
local L
REGION="$2"
[ $( jobs -r -p | wc -l ) -ge ${PARALLEL} ] && wait -n
aws ec2 describe-instances --profile ${LPROFILE} --region ${LREGION} > ${LOUTFILE} &
}

ACCOUNTS=( account1 account2 account3 account4 )
REGIONS=( us-east-1 us-east-2 us-west-1 us-west-2 )
PARALLEL=$(( $( nproc )-2 )) # number of cores - 2

for PROFILE in ${PROFILES@} ; do
for REGION in ${REGIONS@} ; do
list-ec2 "${PROFILE}" "${REGION}"
done
done

I have a handful of similar noscripts, some with multiple layers of functions and complexity. I've caught some of them spawning more than ${PARALLEL} number of commands so I know something's wrong.

I've also tried pgrep -P $$ but I'm not sure that's right either.

Ideally I'd like a command that returns a list of all processes running within the current noscript including their command (eg: aws ec2 describe-instances ...) so I can filter out file-checks, jq commands, etc. OR - a better way of implementing controlled multi-threading in bash.

https://redd.it/1cgvc7v
@r_bash
Why does the order for redirection matter in bash?

For some reason, this works:
bash -i 1>& /dev/tcp/127.0.0.1/8080 0>&1 2>&1
However, this doesn't:
bash -i 0>& /dev/tcp/127.0.0.1/8080 1>&0 2>&0
I just inverted the order. Why doesn't it work?
I had this doubt years ago but it doesn't seem to leave my mind so here it is :).

https://redd.it/1ch2sg8
@r_bash
Bash Fans: Tools for building a custom GPS? (Aware of GRASS, looking for guidance)

Hi!
As noscript implies — I’m wanting to build a custom GPS app using only low-level code (bash and C, specifically). My requirements are to:
- load a GPS map, doesn’t necessarily need more than terrain
- mark custom locations (ie “Mom’s House”, “Secret Smoke Spot”, etc)
- ability to set a waypoint and (ideally) get directions to said waypoint

Is this possible? I have seen grass, but want to know if there are any tools that are a bit more in tune with what I want. This project will be on a Raspberry Pi (and not the only code running), so it can’t take a whole lot of memory ideally.

Thanks in advance!

https://redd.it/1chabtu
@r_bash
The REAL way to pipe stderr to a command.

( (seq 11 19; seq 21 29 >&2;) 2>&1 1>&11 11>&- | cat &> cat.txt 11>&- ) 11>&1

I just wanna document on the internet what's the real way to redirect stderr to a command, while still redirecting stdout to stdout, without the use of <(process) >(substitution).

I wanna document it because i just see people suggesting https://unix.stackexchange.com/questions/404286/communicate-backwards-in-a-pipe ways to get the job done but nobody ever mentions how to *just* pipe stderr to a command without side effects.

&#x200B;

https://redd.it/1ch9rfn
@r_bash
Handling special characters in paths when hard linking

Disclaimer: Completely new and mostly clueless about anything Linux related
I am using a python noscript to process some files and create some hard links of a large number of files. This might not be the most efficient but it works for my use case. My noscript compiles directory and file names into the hard link command ln {source} {dest} along with the respective mkdirs where needed. And what I do is execute it in the shell. I am running OMV 7.05, linux 6.1.0-20 kernel. I run and generate all link commands on my Win10 laptop and ssh into my omv machine to execute the commands.
Most of the link commands execute with no problem but when a filename contains a special character like quotes or exclamation marks, it does not work. Here is a sample command:

ln "/srv/dev-disk-by-uuid-5440592e-75e4-455f-a4b6-2f2019e562fa/Data/TorrentDownloads/TRAnime/Mr Magoo 2018/Mr Magoo S01 720p HMAX WEB-DL DD2.0 x265-OldT/Mr MagooS01E76Free the Rabbit!.nfo" "/srv/dev-disk-by-uuid-5440592e-75e4-455f-a4b6-2f2019e562fa/Data/Media/Anime/Mr Mgoo/Mr Magoo S01 720p HMAX WEB-DL DD2.0 x265-OldT/Mr MagooS01E76Free the Rabbit!.nfo"

it says `- bash: !.info not found`

I have tried escaping the special character like `Mr Magoo
S01E76Free the Rabbit\!.nfo` ***and*** `Mr MagooS01E76Free the Rabbit\\!.nfo (idk why but i just tried)` and it says

> ln: failed to access '/srv/dev-disk-by-uuid-5440592e-75e4-455f-a4b6-2f2019e562fa/Data/TorrentDownloads/TR\
Anime/Mr Magoo 2018/Mr Magoo S01 720p HMAX WEB-DL DD2.0 x265-OldT/Mr Magoo_S01E76_Free the Rabbit\\!.nfo': No such file or directory

Ive also tried encasing just the filename or the word the Rabbit! in single quotes like ln "/srv/d....Mr Magoo_S01E76_Free the'Rabbit!'.nfo" ... with the same result.

Same goes for single or double quotes, commas and iirc dashes too and this occurs irrespective of file type. The only way I got it to work is manually go in and removing the special character from the filename which is near impossible to do for hundreds of files.
Is there anyway I can make this work? I can adjust my noscript on my own but I just need a way to make the link command to work with the special chars.



https://redd.it/1chflwy
@r_bash
Question about sed

Hello, I have the following question and I can not solve it, I would like to know if the following can be done using sed and how it would be, I would need someone to explain me exactly how the address patterns and capture groups within sed to put a regular expression that matches a string of text within a capture group and then use it in the substitution to add text after or before that capture group.

In this case, I have a noscript that contains this string in several lines of the noscript:

$(dig -x ${ip} +short)

this command substitution is inside an echo -e “”

the issue is that I would like to add everywhere where $(dig -x ${ip} +short) appears the following:

simply after +short and before the closing parenthesis, this:

2>/dev/null || {ip}

so would there be any way to use sed to add that string after +short?

i have tried to do something like this but it gives error when i run it:

sed '/dig -x .* +short/s/...\\1 2>/dev/null || ${ip}/g' noscript.sh

I have done it this way because as I have read, the capture groups are defined using (), but by default sed identifies as capture groups the substrings of the regular expression, so (.*) would be the first capture group, so I use ...\\1 as placeholder .* to tell it that after that the following string has to go: 2>>/dev/null || ip
My understanding is probably wrong

The truth is that I am quite lost with the operation for these cases of the tool and I would like you to help me if possible, thanks in advance.

https://redd.it/1chk67d
@r_bash
Find command, sort by date modified

This question was asked on stackoverflow but I still can't quite figure out how to write the command. I want to find files with a specific name, and sort by date modified or just return the most recently modified. All the files I am looking for have the same name, but are in different directories.

find -name 'filename' returns all the options, I just want the most recently modified one

https://redd.it/1chn36z
@r_bash
Command Palette...

I need a command palette for CLI in basj... please help. Not marker.

https://redd.it/1chwd71
@r_bash
Iterate through items--delimit by null character and/or IFS=?

When iterating through items (like files) that might contain spaces or other funky characters, this can be handled by delimiting them with a null character (e.g. find -print0) or emptying IFS variable ( while IFS= read -r), right? How do the two methods compare or do you need both? I don't think I've ever needed to modify IFS even temporarily in my noscripts---print0 or equivalent seems more straightforward asuming IFS is specific to shell languages.

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