r_bash – Telegram
Question about basic telent session noscript.

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 noscript.

#!/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 noscript, 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
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
Quick select prompter utility

I wrote a utility noscript 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 noscript:

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 noscript is wrapped to a infinite loop. It also showcases that the commands can be given denoscriptions by prefixing them with `<denoscription> #`.

while true; do
~/ps/noscripts/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
Unable to understand the usage of jq while indexing

I am pretty much new to bash and learning to noscript 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
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
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
Benchmark "read -N" vs "head -c"
https://redd.it/1cest8z
@r_bash
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

&#x200B;

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

&#x200B;

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:

&#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 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