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
​
No spaces in test, something is being interpretted when test is echo'd.
​
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
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`]​
No spaces in test, something is being interpretted when test is echo'd.
​
pj@eth:~$ test="[1`.2.3.4`,`1.2.3.5`]"pj@eth:~$ echo ${test}1Battled 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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
Any insights on starting Enterprise level bash noscripting? Good resources to level up my skills?
https://redd.it/157dtrz
@r_bash
https://redd.it/157dtrz
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
#
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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
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
Open Source Heroes
Top Shell Contributors
Top Shell Contributors creating and contributing to Open Source Software
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 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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
What in the name of Brian Fox is this?
this works,
this works
this also works
why the heck this dont work? what is the meaning of
https://redd.it/15a4lwh
@r_bash
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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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?
​
​
https://redd.it/15aabcn
@r_bash
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?
​
​
https://redd.it/15aabcn
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
​
https://redd.it/15aeyq2
@r_bash
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
​
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
​
#!/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
​
https://redd.it/15ai9aq
@r_bash
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
​
#!/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
​
https://redd.it/15ai9aq
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
is there a read flag to enforce integer input?
ie
read -i "[input 1-9\]" int
https://redd.it/15anz23
@r_bash
ie
read -i "[input 1-9\]" int
https://redd.it/15anz23
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
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
https://redd.it/15az0b1
@r_bash
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 advancehttps://redd.it/15az0b1
@r_bash
Reddit
From the bash community on Reddit
Explore this post and more from the bash community
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
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
I'm wondering what would be the best way (if any) to escape the
https://redd.it/15bkgu2
@r_bash
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
Reddit
From the bash community on Reddit
Explore this post and more from the bash community