r_bash – Telegram
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
Intune Mac Scripting

I work for an MSP provider and am trying to deploy a noscript which will install our RMM agent remotely onto Mac devices.
I've tested the full noscript which works directly on the Mac.

However, it fails when running in intune.

Is anyone able to advise?

Script should download the PKG from URL then run the file.

#! Sudo /bin/bash -c curl -o PKG file name "web URL" ; sudo installer -pkg PKG name -target /

https://redd.it/15bu6jk
@r_bash
My nested for loops don't work

Hi, hope you're all well, I have a tough question here but I don't know what to do

I'm a bioinformatician and I want to convert one notation of RNA secondary structure to a .bed format, so, basically let's say I have this string:

........(((..((.((((((((((((....)))))))).....((((.((.......)).))))))))...)).))).

I want to track all the matching "()" and convert those positions into a new string "pos1-pos2", for example we have a matching () in the positions . In this example we have a match in the 8 and 68 positions, so I want to convert this to "8-68" and do it to all the positions.

To do that I wrote this code:

&#x200B;

EDIT: I tried to ident this right but it won't go, sorry

init_pair=()

final_pair=()

checklist=()

&#x200B;

for ((i=0; i<${#fold_result}; i++)); do

check=false

for j in "${checklist[@]}"; do

if [[ "$j" == "$i" ]]; then

check=true

fi

done

if [[ "$check" == false && "${fold_result:i:1}" == "(" ]]; then

checklist+=( "$i" )

init_bp=$(expr "$i" + "$initial_position")

init_pair+=( "$init_bp" )

fi

done

&#x200B;

for ((i=0; i<${#fold_result}; i++)); do

check=false

for j in "${checklist[@]}"; do

if [[ "$j" == "$i" ]]; then

check=true

fi

done

if [[ "$check" == false && "${fold_result:i:1}" == ")" && "${fold_result:i+1:1}" != ")" ]]; then

checklist+=( "$i" )

final_bp=$(expr "$i" + "$initial_position")

final_pair+=( "$final_bp" )

for ((k=i-1; k<"${#fold_result}"; k--)); do

if [[ "{fold_result:k:1}" == ")" && "${fold_result:k-1:1}" == ")" ]]; then

checklist+=( "$k" )

final_bp=$(expr "$i" + "$initial_position")

final_pair+=( "$final_bp" )

elif [[ "{fold_result:k:1}" == ")" && "${fold_result:k-1:1}" != ")" ]]; then

checklist+=( "$k" )

final_bp=$(expr "$i" + "$initial_position")

final_pair+=( "$final_bp" )

break

fi

done

fi

done

My logic is to define two arrays, one of the init pairs ("(") and other for the final (")"), use a checklist array to track it all. First I take all the "(" characters in a for loop, if the index is already in the checklist the loop continues. The next step is to take the final pairs and match, so I took all the ending positions ")", without any other ")" in the next index and started a nested for loop to go through the indexes before and save it in the array, later I would write those pairing positions.

The problem is this loop doesn't return nothing in the final_bp array, I think it's an infinite loop and I don't know how to proceed. Do you have any idea? I suspect that I'm doing the syntax wrong.

Thanks

&#x200B;

https://redd.it/15bzn37
@r_bash
-n not found, If statement not working

Hello world! I´m trying to make the following code work but it fails at the If statement:


#!/bin/bash
for ip in $(seq 1 15);
do
a=$(ping -c 1 192.168.0.$ip | pcregrep -M '([0-9]{1,3}.{1}){4}.*ping.*\n.*1 received' | grep -o -E '([0-9]{1,3}.{1}){4}');
if -n $a; then
echo $a;
else
:
fi;
done

I´m new to bash, regex and networking. Just doing little projects to learn.

When executing the noscript the output says:

noscript.sh: 6: -n: not found

Any help would be appreciated!

&#x200B;

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