r_bash – Telegram
]]; then
_usage
exit 1
fi

#### Print the parsed options
echo "copy-from=$copyfrom, creation=$creation, var-num=$varnum, step=$step"

​

https://redd.it/155magg
@r_bash
Adding a leading zero to a variable in a loop.

I'm writing a noscript that renames every video file in a directory with a shorter, clearer name. The intention is to name every one "S01E01.showname.ext", "S01E02.showname.ext" etc.

Incrementing a variable is easy, the problem is it's dropping the leading zero. So while S01E01 is fine, the next file becomes S01E2. Awful!

I tried to append a zero to any number less than 10:

function loop {

for video in *.$filetype
do if (($episodenumber < 10))
then $episodenumber=0${episodenumber}
fi
echo -e "debug filename: S${seasonnumber}E${episodenumber}_${showname}.${filetype}"
episodenumber=$((episodenumber+1))
done
}

But it doesn't work, giving this error from files 1 to 9:

./bulk_renamer.sh: line 24: 1=0{1}: command not found

I don't understand what it's doing here, because if I do this at bash shell

testver=1
testver=0${testver}
echo $testver

that returns "01".

What's going wrong?

https://redd.it/155o9xt
@r_bash
Help with optional arguments

I am trying to write a noscript that has several options, some with arguments some without, but I am completely stuck on the implementation of an optional argument.

An example run shows "./program.sh -w30 -c Message" working, where:-w30 sets the width to 30-c sets the special character to the default of "*" but optionally could be set with something like -c'X'and "Message" sets the pages text to Message

Everything I have tried sets -c to the value Message, and I am at my wits end trying to make this work.

Here is my function for handling options:

printhelp() {foo}

# Parse command line options
function check
options() {
while getopts ":hw:p:nc:" options

do

case "$option" in

h|--help) # Display help
printhelp
;;
\?) # Check for invalid option
echo "Error: Invalid option -
$OPTARG"
print
help
;;
w) # Set width
width="$OPTARG"
;;
p) # Set padding
padding="$OPTARG"
;;
n) # Display without border
onlytext=true
;;
c) # Use special character

;;
: ) # Missing arg for -w/-p
if [ "
$OPTARG" != "c" ]; then
print
help
fi
;;
esac

done
}

&#x200B;

https://redd.it/1568niu
@r_bash
Curious thing happening... workaround found, but what is happening here?

I am adding an IP address to azure NSG sourceAddressPrefixes. az wants a string like [1.2.3.4,1.2.3.5\] with no spaces in it. Here's my output:

Space in between test, echoing string is literal

pj@eth:~$ test="[`1.2.3.4`, `1.2.3.5`]"

pj@eth:~$ echo $test

[`1.2.3.4`, `1.2.3.5`]

&#x200B;

No spaces in test, something is being interpretted when test is echo'd.

&#x200B;

pj@eth:~$ test="[1`.2.3.4`,`1.2.3.5`]"

pj@eth:~$ echo ${test}

1

Battled with this for a while yesterday.

I need this string to be literal and to not have any spaces in it. It's for azure cli nsg which fails when there are spaces.

When I remove the space in between the comma and the 2nd IP address value, the value of test becomes 1. I ended up removing the brackets which seems to stop whatever interpretation is happening, and then adding them back later , wrapping them around the string. But I had never seen this before so I am curious what it is doing. I imagine there's a better way to handle it.

https://redd.it/156ka4q
@r_bash
Trying to use an argument as a path

i want to make a function to paste into a file
so ideally id like it to be

function ptf {
xclip -se c -o > $1
}

but obviously that doesn't work
gives me an error about redirecting to a string

https://redd.it/156oixz
@r_bash
newbie - accessing sub folder files from noscript

from a noscript i need to call "file.txt" in subfolder "sub"

i can't figure the syntax out:

if [[ "$(<sub/file.txt)" ...

https://redd.it/15731x8
@r_bash
Any insights on starting Enterprise level bash noscripting? Good resources to level up my skills?



https://redd.it/157dtrz
@r_bash
echo result issue

my noscript has:

x='echo "1+2" | bc'

echo $x

but it's just displaying the string x not the result 3?

https://redd.it/158surm
@r_bash
What is wrong with this noscript?



#

Can someone tell me what is wrong with the below bash noscript? When I run the noscript, I only get one result: https://www.google.com | hakrawler. When I run the command (echo https://www.google.com | hakrawler) in the Kali terminal I get hundreds of results. Thank you.

\#!/bin/bash

\# Define the URL to crawl

url="https://www.google.com"

\# Get the domain name from the URL

domain=$(echo "$url" | sed -e 's#\^https://www.##' -e 's#/$##')

\# Generate the folder path on the desktop

output_folder="$HOME/Desktop/${domain}"

\# Create the folder if it doesn't exist

mkdir -p "$output_folder"

\# Generate the output file name with domain and date

output_file="${output_folder}/${domain}_$(date +"%Y%m%d").txt"

\# Define the command to run

command_to_run="echo $url | hakrawler"

\# Run the command and save the output to the generated file

$command_to_run > "$output_file"

\# Check for and remove duplicates in the output file

if [ -f "$output_file" \]; then

sorted_file="${output_file}.sorted"

sort "$output_file" | uniq > "$sorted_file"

mv "$sorted_file" "$output_file"

fi

echo "Hakrawler output saved to $output_file"

https://redd.it/158ubfq
@r_bash
Why do bash noscripts need to have permission changed before being executed?

I'm trying to learn bash for cloud computing but I'm a complete begginer to Linux and currently just learning off of tutorials which I can't seem to understand much of and without a PC or laptop.

My question basically is, since code files can be executed as is why can't the same be done for bash noscripts? Why do they need to be assigned path environment variables first?

https://redd.it/159eajn
@r_bash
Top Shell open source projects and contributors

Hello everyone,

I'd like to introduce you some interesting lists and rankings related to the Shell open source ecosystem:

\- Top Contributors (global or by country): https://opensource-heroes.com/contributors?language=shell
\- Trending projects: https://opensource-heroes.com/discover/shell (based on GitHub stars increase)
\- Awesome projects: https://opensource-heroes.com/awesome/shell (we plan to add soon a new feature to allow everyone to contribute to that list directly from the site)

You can also find "stars" history in the detail page of some repos (it will be available soon for all Shell repos, we're still processing some data!) and embed this chart in your project's README or docs. Or you are also able to send Reviews of your favorite plugins/projects.

Hope you find this content useful! Any feedback is really appreciated. Please note that be are still in beta 🙏 We want to build a platform that allows everybody to easily explore the open source world!

https://redd.it/159pjx9
@r_bash
while logic

what would be the logic & syntax, i want to do stuff with a user input until the user inputs a certain string ie:

while read -p "Enter your string: " x != "y"; do

do stuff with x then repeat this loop

done

if the input is "y" i want to exit

https://redd.it/159vtgo
@r_bash
What in the name of Brian Fox is this?

stat <(cat /proc/cpuinfo)

this works,
cat <(cat /proc/cpuinfo)

this works
cat < <(cat /proc/cpuinfo)

this also works
stat < <(cat /proc/cpuinfo)

why the heck this dont work? what is the meaning of < after cat in the third command?

https://redd.it/15a4lwh
@r_bash
Is echo trolling me?

in the snippet bellow I've assigned `IFS=' -'`. code:

#!/bin/env bash

set -o errexit
set -o nounset

main() {
IFS=' -' read -r -a words <<<"${*^^}"
for word in "${words[@]}"; do
echo "${word[@]}"
done
echo $IFS
}

main "$@"

I ran the snippet with this argument:

1--------2 3- 4---         5

I've expected the output to be:

1
2
3
4
5

but instead it is:

1







2
3
4


5

if I use `echo` with `-n` option or use `printf`, the output is `12345` as expected but with `-n` this is the output. I can wrap my head around wtf is going on. any idea?

&#x200B;

&#x200B;

https://redd.it/15aabcn
@r_bash
Using Functions from an external sh file.

Hi

Original Post: So I have now massed a massive collection of my own functions which I use a lot in many of my projects.

The problem I am having is that when I update the code in one function, I have to copy and paste it to all my noscripts that use it, on all the servers running it.

I would like to find away that I could "include" an external file or more, that allows me to call those functions as if they were part of the noscript I am running.

This would make my life easier as I could then simply just copy the latest version of that shell library.


Solution:


library.sh

hello() {
echo "Hello World"
}

mainprog.sh

#!/bin/bash

source "library.sh"

hello

Then to test:

$ ./mainprog

Output:

Hello World

&#x200B;

https://redd.it/15aeyq2
@r_bash
modifying file names

Hi, I'm trying to change the names of my torrent files, but I don't understand why line 14 when I do

`echo "${nameArr[$counter]}"`

it doesn't display the element corresponding to the array index it doesn't even print the part during the iteration :

example on the file : Ant-Man.and.the.Wasp.Quantumania.2023.1080p.MA.WEBRip.DDP5.1.Atmos.x264-CM.torrent

i get this result :

Ant-Man

WEBRip

&#x200B;


#!/bin/bash

dir=($(find . -maxdepth 1 -name "*.torrent"))
for file in "${dir[@]}" ; do
IFS="." read -a nameArr <<< "$file"
echo "${nameArr[@]}"
boolContinue=true
counter=0
newName=""
arrSize=${#nameArr[@]}
echo "$arrSize"
while [ $boolContinue ] && [ $counter -lt "$arrSize" ]; do
echo "${nameArr[$counter]}"
if [ $counter == 0 ] ; then
newName+=${nameArr[$counter]}
counter+=1
elif [[ ${nameArr[$counter]} =~ ^[0-9]{4}$ ]] ; then
newName+=${nameArr[$counter]}
counter+=1
boolContinue=false
else
newName+=${nameArr[$counter]}
counter+=1
fi
done
done

&#x200B;

https://redd.it/15ai9aq
@r_bash
Using JQ to return the index number where an element in an array has a specific value.

I managed to do this when I was teaching myself JSON and playing with JQ. Now I can't remember how I did this. So any guidance would be of value.


Take the following file: settings.json

{
"BotAPIKey": "SuperSecretKey",
"Channels":
{
"Channel_Name": "First Channel",
"Channel_Short": "ch01",
"Channel_ID": 4004841050681
},
{
"Channel_Name": "Second Channel",
"Channel_Short": "ch02",
"Channel_ID": 4004685917007
}

}

If I use the following:

jq '.Channels | contains("ch02")' settings.json

It returns:

false
true

What I actually need is the index number in the array. In this case it must return 1.

I did achieve this once, a few weeks back, when experimenting, and now I can't repeat the results. Like a fool, I didn't document everything I did.

https://redd.it/15axlk0
@r_bash