r_bash – Telegram
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
command being run last even though its first in the noscript

Hi, I am currently trying to create some simple tmux layouts with bash noscripts, but i seem to have hit the wall. This is the noscript in question:

#!/bin/bash
SESSIONNAME="server"

tmux has-session -t $SESSIONNAME &> /dev/null
RESULT=$?
echo RESULT

if $RESULT != 0
then
echo "Creating..."
tmux new-session -s $SESSIONNAME -d
tmux split-window -h "htop"
else
echo "Session already exists. Attaching..."
tmux attach -t $SESSIONNAME
fi

When i run the noscript, it will fail almost everytime because the command tmux has-session -t $SESSIONNAME &> /dev/null is being run last.

Output with -x flag set:

sh -x server.sh
+ SESSIONNAME=server
+
+ RESULT=0
+ echo 0
0
+ 0 != 0
+ echo Session already exists. Attaching...
echo Session already exists. Attaching...
+ tmux attach -t server
+ tmux has-session -t server
can't find session server
can't find session server

For some reason, the command is being run last, which makes the rest of the noscript pretty much obsolete. Binding wait to the process also doesn't work. Does it have something to do with using /dev/null? I really can't wrap my head around it. I guess it's some basic Bash knowledge that I've missed. Thank you in advance

https://redd.it/15az0b1
@r_bash
How to get values from psql inside the database_name variable but send the errors to error_log file?
https://redd.it/15azejl
@r_bash
Best way to escape a . in an argument?

I'm building a CLI and one of the command needs to edit a JSON config file. We decided it would be good to do command edit settings.roles.<username>.role admin to change for example the role of <username to admin. It works smoothly for all cases except when usernames contains a . (dot).

I'm wondering what would be the best way (if any) to escape the . in a username. Note that it's not possible to prevent usernames from containing a . as our usernames contain domain names.

https://redd.it/15bkgu2
@r_bash
Few questions about this noscript if you don't mind helping a newbie...
https://redd.it/15bqomb
@r_bash
bash command to catch opened url by browser

Hello,

is there any bash command or tool which is able to catch the currently urls opened by browsers (Firefox or/and Chromium)?

TIA

https://redd.it/15bqeb6
@r_bash
Someone explain how this obscure code works please!

Hi everyone!

So I found this code that one of my friends sent me.

:; ______=$? __=${#______} ____=$[__+__] ________=$[__+____] _____=$[____+____]
__________=$[____+_____] _________=$[__+__________] ______________=(
/????/$$/????) ____________=${______________[$______]}
_____________=${____________##*/} _______________=(${____________//\// })
________________=${_______________: -$__:$__}$_____________
___________________=${________________:$______:$________}
___________=${_____________:$______:$__} _________________=${___________^}
. <($___________________<<<__________________=\({$_________________..\
${___________}}\))&&_______=(${__________________[@]:$______:$____$__________})
___=(${_______[@],,})&&${___[$_____]}${___[$____]}${___[$_________]}${___[
$__$_____]} -${___[$_____]} ${_______[ $_________]}${___[${_____}]}${___[$__$__
]}${___[$__$__]}${___[$__$_____]} ${_______[$____$____]}${___[$__$_____]}${___[
$__$_________]}${___[ $__$__]}${___[$________]}\\$______$[$_____#$____$____$__]

So basically it prints "Hello World!". I'm just curious to know how this code works even though it's impractical and hella unreadable. Tried finding explanations, but couldn't get anything. Is there any program that converts text to code made of only special characters like this?

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