r_bash | Unsorted

Telegram-канал r_bash - r_bash

46

Credits: @r_channels & @reddit2telegram

Subscribe to a channel

r_bash

Command Palette...

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

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

Читать полностью…

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 script that contains this string in several lines of the script:

$(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' script.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

Читать полностью…

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

Читать полностью…

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

Читать полностью…

r_bash

How do I get the number of processes spawned by a script?

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

&#x200B;

I have a script 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 script:

$( 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:

&#x200B;

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 scripts, 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 script 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

Читать полностью…

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 script which, thus far, has performed effectively.

Note: I was unable to get the status=progress feature to function correctly. However, the script 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"

Читать полностью…

r_bash

A bash script 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 script!

## Usage

1. Set the url variable in your bash script 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/script-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 script

You can see the script here

## Test It Out

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

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

Читать полностью…

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

Читать полностью…

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&amp;type=link&amp;cId=690c4a5d-257a-4bc3-984a-1cb53331a300&amp;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

Читать полностью…

r_bash

Using a launcher/.desktop file to run a bash script in the same folder as the launcher?

Hi,

I made a bash script 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 script, I am using a launcher to indirectly run it.

The launcher works if I use a an absolute path for the script 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

Читать полностью…

r_bash

Benchmark "read -N" vs "head -c"
https://redd.it/1cest8z
@r_bash

Читать полностью…

r_bash

I want to read more about jq(preferred books)

Can you guide me some? I am till now not finding a book that contains at least 10 pages on jq.

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

Читать полностью…

r_bash

bash riddle

$ seq 100000 | { head -n 4; head -n 4; } 
1
2
3
4
499
3500
3501
3502


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

Читать полностью…

r_bash

Doubt



Hi guys i am learning linux command line from the book "The Linux Command Line
Second Internet Edition
William E. Shotts, Jr".
I completed part1 in this book.
Part 1 – Learning The Shell starts our exploration of the basics of the
command line including such things as the structure of commands, file system
navigation, command line editing, and finding help and documentation for com-
mands.
I need to know what is bash programming and bash programming language?.
What's the difference between bash and other programming language.

As mentioned in part 1, the things I learned are actually bash programming or not?
Whether i learning bash programming without knowing it?


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

Читать полностью…

r_bash

Question about basic telent session script.

Hello guys.

I want to gather the output from the cisco device by using 'show ip interface' every 30 seconds.

and I haven't finished yet but I faced unexpected behavior.

This is my script.

#!/bin/bash

# device
ip="192.168.192.138"
port="23"
pass="cisco"
duration=$((60 * 60)) # 1 hour
interval=30 # Check every 30 seconds

# Main
echo "Starting to gather 'show ip ali'"
start_time=$(date +%s)
end_time=$((start_time + duration))

while [ $(date +%s) -lt $end_time ]; do
echo "Gathering 'show version'..."
(
sleep 1
echo "$pass"
sleep 5
echo "show ip ali"
sleep 5
echo "exit"
) | telnet $ip $port
echo "Waiting for 30 seconds"
sleep $interval
done

Is there a way to maintain the telnet session without disconnecting and gather 'show ip ali' every 30 seconds?

With my script, every time telnet sessions disconnected after executing 'show ip ali' and then re-connect telnet session again.

Thank you!

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

Читать полностью…

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

Читать полностью…

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 script 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 script 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 script 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

Читать полностью…

r_bash

Bash Fans: Tools for building a custom GPS? (Aware of GRASS, looking for guidance)

Hi!
As title 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

Читать полностью…

r_bash

What is the utility of adding 1>&2 if there isn’t file to redirect to?



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

Читать полностью…

r_bash

;;
-L | -l) lsblk -o NAME,MODEL,SERIAL,VENDOR,TRAN
;;
-F | -f)
check_dependencies
format_drive "$2"
;;
*)
help
;;
esac

&#x200B;

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

Читать полностью…

r_bash

A bash script 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 script!

## Usage

1. Set the url variable in your bash script 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/script-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 script to see the script in action with various examples!


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

Читать полностью…

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 script.
I don't know if I put a sleep 3s for example in script. 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

Читать полностью…

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

Читать полностью…

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 script" 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

Читать полностью…

r_bash

Whats the best resource to learn bash?

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

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

Читать полностью…

r_bash

what is the difference between absolute and relative path in the bash shell?

Hello, i'm trying to understand what the difference between a relative path and an absolute path is in the bash shell

i did a reddit search of r/bash and found this

https://www.reddit.com/r/bash/comments/4aam9w/can\_someone\_tell\_me\_the\_difference\_between/

but i'm not really understanding what they are talking about in the context of the bash shell

can anyone give me any examples of the difference between an absolute path and a relative path that i can actually use in my shell so i myself can get a handle on the concept?

thank you

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

Читать полностью…

r_bash

Unable to understand the usage of jq while indexing

I am pretty much new to bash and learning to script well, I am learning how to use jq tool to parse a json file and access the elements of character by character.

https://pastebin.com/SfLFbJPE

In this effort, my code works fine I have the item to be "DOG"

and my for loop to have

for entry in $(echo "$json_data" | jq '.[\] | select(.[\] | contains("D"))'); do

where the key comes out to be 2 but when i access dynamically with ${item:$j:1} its not going to the for loop itself. Could someone help me understand this thing?

for entry in $(echo "$json_data" | jq '.[\] | select(.[\] | contains("${item:$j:1}"))'); do


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

Читать полностью…

r_bash

Quick select prompter utility

I wrote a utility script for choosing commands by pressing key key sequences. As it is interactive, the two gifs below are probably the best way to showcase the functionality. However I also wrote a short blog post about it, which also contain the actual script:

https://miropalmu.github.io/homepage/bash\_quick\_select\_prompter.html

Toy example usage \(key presses after enter are a, a, b\).

In the following, the script is wrapped to a infinite loop. It also showcases that the commands can be given descriptions by prefixing them with `<description> #`.

while true; do
~/ps/scripts/prompter.bash \
b "branches # git branch" \
t "status # git status" \
s "summary # git s" \
i "indexed/staged diff # git sd" \
u "ulog # git ul" \
l "log 5# git log -n 5" \
d "diff # git d" \
h "stash list # git hl"
echo
done

Git utility

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

Читать полностью…

r_bash

tee is doing nothing. Details included in pastebin link.

https://pastebin.com/h9FkmCBs

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

Читать полностью…

r_bash

Build my first project in bash. Test it out please

&#x200B;

https://preview.redd.it/gjvu2vyiefwc1.png?width=2204&amp;format=png&amp;auto=webp&amp;s=b85764d55d247c8f8533939a9ba55aa992e5ac57

github: https://github.com/suvanbanerjee/Keyboard-LED-Remapper

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

Читать полностью…
Subscribe to a channel