r_bash – Telegram
Issue with a simple Bash Script for adding an iRule to a list of Virtual Servers.

Hello Community, 

I am quite new to Bash noscripting. Though I know the platform I intend the noscript to run on is not relevant to this sub, I would appreciate if you could at least elevate my concern that the problem I am encountering is with my Bash noscript itself and not the environment I'm running it on.

I am having an issue with a bash noscript for an F5 BIG-IP Load Balancer which is intended to:

1. Take as input a .txt file named "vs_list.txt" containing a list of Virtual Server Names, each on a separate row.

2. Prompt the user for the name of the iRule that needs to be added to the Virtual Servers.

3. Iterate over the VS Names from the input list and retreives the partition in which each VS resides (building on F5 Support Solution K59493724: How to get details of the virtual servers in all partitions via TMSH? (https://my.f5.com/manage/s/article/K59493724))

4. Checks for and retrevies the iRules which are currently configured for the given VS# Adds the new iRule to the given VS and coserves pre-existing iRules if any. 


When running the noscript I am only hitting the outermost 'else' statement for being unable to find the partition and VS name.



#!/bin/bash

# Prompt the user for the iRule name and read it into the 'new' variable
echo "Please enter the iRule name:"
read new

noneRules='rules none'

while IFS= read -r vsname; do
# Retrieve the partition and virtual server name
full
vsinfo=$(tmsh -c "cd /; list ltm virtual recursive" | grep "$vsname" | grep -m1 "^ltm virtual")
echo "Full VS Info Debug: $fullvsinfo"

# Extract the partition and virtual server name from the retrieved information
if [ $full_vs_info =~ ltm\ virtual\ (.+)/(.+) ]; then
partition="${BASHREMATCH[1]}"
vs
name="${BASHREMATCH[2]}"

# Format the tmsh command to include the partition
rule=$(tmsh list ltm virtual /$partition/$vs
name rules | egrep -v "\{|\}" | xargs)

if [ "$rule" == "$noneRules" ]; then
tmsh modify ltm virtual /$partition/$vsname rules { $new }
echo "iRule $new was added to $vs
name in partition $partition"
else#
tmsh modify ltm virtual /$partition/$vsname rules { $rule $new }
echo "iRules $rule were conserved and added $new to $vs
name in partition $partition"
fi
else
echo "Could not find partition and virtual server name for $vsname"
fi
done < /shared/tmp/test
list.txt

tmsh save sys config

As far as I was able to troubleshoot, the problem I am encountering appears to be with line 11 of my noscript where I attempt to assign the string "ltm virtual SomePartition/VS_Example.com {" to the "full_vs_info" variable using:

full_vs_info=$(tmsh -c "cd /; list ltm virtual recursive" | grep "$vs_name" | grep -m1 "\^ltm virtual")

When I run the tmsh command [tmsh -c "cd /; list ltm virtual recursive" | grep "VS_Example.com" | grep -m1 "\^ltm virtual"\] on its own, from the F5's Bash shell, I am getting the output I expect:

"ltm virtual SomePartition/VS_Example.com {"

However, when I run the noscript with the debug echo , it only outputs "Full VS Info Debug:", and ends the noscript with "Could not find partition and virtual server name for $vs_name" and a sys config save.

If at all relevant, I am attempting to run this on a BIG-IP, version (https://15.1.10.2), build 0.44.2. 

All feedback and criticism is highly appreciated! Thanks in advance!

https://redd.it/1al6h61
@r_bash
Responsive image gallery in three lines of bash

Three lines of bash to generate a responsive HTML image gallery — motivated by having to spend way too much time on getting /r/immich and /r/photoprism to do what I wanted them to do... both are awesome projects btw.

>❗️NOTE: on MacOS this requires the GNU flavour of find — use brew install findutils and replace in the noscript find with gfind.

echo '<html><head><style>img {max-width: 321px; height: auto; display: table-cell;} body>div {display: grid; grid-template-columns: repeat(auto-fill, minmax(321px, 1fr)); gap: 5px;}</style></head><body><div>' > gallery.html
find -type f ! -name '.*' -a -iregex '.*\.\(jpg\|jpeg\|png\|noscript\|bmp\|webp\|gif\)' -printf '<a href="%P" target="blank" noscript="%P size: %s bytes; created: %c"><img src="%P" loading="lazy"></a>\n' >> gallery.html
echo '</div></body></html>' >> gallery.html

(the only reason I did not extract gallery.html to a variable was to keep it to 3 lines)

https://redd.it/1amkg1n
@r_bash
What do you call the '-' when used to specify options (like set -x)?



View Poll

https://redd.it/1amlbbo
@r_bash
Invoke bash noscript in remote server

I'm running a noscript from my jump host to copy a noscript to client servers. From that copied noscript will run on the client server and check for a specific agent is installed or not. If not it will install through that noscript. But before installation happen I want to raise a standard change servicenow.

Standard change raising noscript also in the jumphost. That only can be run from the jumphost.

So I want to know how can we invoke standard change raising noscript with parsing some information to that from the client machine,

https://redd.it/1amqbf8
@r_bash
Dynamic FFmpeg build noscript

This builds the latest stable release version series 6 of FFmpeg with additional codecs included.

This will dynamically find the latest source code for each codec and update them accordingly.

I am slowly working on a GNU version of this noscript that will allow the user to specify a GNU all build so check back over time.

As always I hope some of you guys find this useful.

Cheers

&#x200B;

Lit of codecs available

GitHub Script

https://redd.it/1ao2csh
@r_bash
Shell noscript that can add bookmarked directories and cd into them directly (+video)

Hi all. I was getting tired of writing long cd commands to get to specific directories in the terminal. I found there were some programs/tricks for this (like CDPATH) but none of them worked for me. Hence, I made a small shell noscript that can add/delete/navigate bookmarked directories. Thank you!

You can watch a 2 minute demo here: Loom video

Or view the source code here: https://github.com/TimoKats/CDBookmark/

https://redd.it/1ao5lob
@r_bash
GREP - Practical Guide 🚀

Last month, I discovered one of the best terminal commands. And wrote a blog about it here - https://www.priya.today/blogs/grep
Can you share some useful tips and tricks?
I would greatly appreciate your feedback.

https://redd.it/1ao6dfz
@r_bash
Anyone handle piping ?

What command line can I use to read an output from piping: "cat this.txt | xargs echo" show me a string with spaces like this "123 156 856 899". But into this.txt have next line.

I need to grab one to one that elements before spaces in the next piping command line using xargs or another commmand line.

Thanks.

https://redd.it/1aob81w
@r_bash
Using SORT but ignore "the", "a" and "an"?

As the noscript says...

I have list of noscripts. I want to sort the list, but I don't want all the noscripts starting with "the" bunch together.

I read through the man page hoping to find a way to get SORT to ignore a word, but I ran dry.

Anyone have any ideas?

https://redd.it/1aod18i
@r_bash
Please give me pointers on how to make my watermark noscript suck less

#!/bin/bash

# First, a file to work on is picked

echo "Which file to work on?"

read -e "filetoworkon"

# Then, the format to convert to is chosen:

# 1) 13x18cm (2126px wide)

# 2) 18x13cm (942px wide)

read -n1 -p "Pick 1) for 18x13 or 2) for 13x18 1,2" doit
case $doit in
1) convert "$filetoworkon" -resize 2126 "$filetoworkon"-resized.png ;;
2) convert "$filetoworkon" -resize 492 "$filetoworkon"-resized.png ;;
) echo dont know ;;
esac

# Then the file to use as a watermark is chosen

echo "Which file to use as watermark?"

# A list of options is given

echo "Options:"

ls my-logo-
.pdf

read -e "watermarkfile"

# Finally, the watermark is added

convert "$filetoworkon"-resized.png "$watermarkfile" +distort affine "0,0 0,0 %w,%h %fx:t?v.w*(u.h/v.h*0.05):s.w,%fx:t?v.h*(u.h/v.h*0.05):s.h" -shave 1 -gravity southeast -geometry +50+50 -compose hard-light -composite "$filetoworkon"-resized-signed.png

echo ""File saved as "$filetoworkon"-resized-signed.png""


It does what it has to, but it is ugly and there is some redundancy. I've also been told that I should use functions instead of read -e, but I don't know how.

&#x200B;

I hope someone will help me clean up this thing, and explain how it could be improved.

https://redd.it/1aosyju
@r_bash
Move to new $ line

I need to move to a directory, move to a new line and execute a noscript.


user@ubuntu:\~$ mynoscript
user@ubuntu:\~/Documents$ server started on port 3000

Right now my noscript executes like this

user@ubuntu:\~$ server started on port 3000

&#x200B;



https://redd.it/1aox1kr
@r_bash
I can't understand the result. bad string to variable assignment.

if I run:

URL=https://dl.discordapp.net/apps/linux/0.0.43/discord-0.0.43.deb; echo "text \"$URL\"";

# as expected result:
text "https://dl.discordapp.net/apps/linux/0.0.43/discord-0.0.43.deb"

but,

URL=$(curl -Is -- 'https://discord.com/api/download/stable?platform=linux&format=deb' | grep -i 'location' | awk '{print $2}'); echo "text \"$URL\"";

or

URL=$(curl -Is -- 'https://discord.com/api/download/stable?platform=linux&format=deb' | grep -i 'location' | cut -d' ' -f2); echo "text \"$URL\"";

# strange result:
"ext "https://dl.discordapp.net/apps/linux/0.0.43/discord-0.0.43.deb

the first letter of 'text' is removed: 'ext'.
the double quotes are moved to the first token instead of covering up the URL.

I don't know how to explain it, I don't know how to research it, I have no idea what the problem is or how to solve it.

thx _o/

https://redd.it/1apetnl
@r_bash
Script acts differently when called with sh compared to bash

Weird noscript but bear with me.

&#x200B;

So I have a noscript that fails on machines when called without specifying bash. The noscript ultimately does the below as an example.

&#x200B;

mynoscript

#!/bin/bash
#
#
#
searchPath=/opt/folder/files/
player < ${searchPath}/music.mp3


When I call the noscript as /bin/mynoscript, i fails with "No such file or directory" and does not appear to expand the \
as a wildcard. If I call it as "bash /bin/mynoscript" it functions correctly and handles the wildcard.

&#x200B;

Any thoughts on why it fails unless I specify "bash" even though there is a shebang/execute bits?

https://redd.it/1aphmz7
@r_bash
How to get realtime output from bash

Have a noscript that runs a custom program within bash, the exe takes a while to run, about 5 minutes, but when you run it there are heartbeats printed to let the user know that it is still running, on my bash, those messages appear after the noscript has finished running, also, it seems the buffer is full and not every message gets printed, I tried redirecting to a file but still the output is not "real time"

something like

# !/bin/bash
testexecutable --flag=1 | tee "/tmp/testoutput.txt"

this is on macos, also tried to output to /dev/tty but it says its not configured.

https://redd.it/1apk4hq
@r_bash
Why does "cat | ls" behave like that?

Hello everyone,


I am doing a project that consists on replicating bash (some of its basic behavior) in C.
I am still on the early stages of the project, analyzing how bash behaves with different inputs.
The point is that I was playing with some basic commands and its combinations through pipe (|) operator, and when I tried "cat | ls" I was kind of expecting just the ouput of ls, but instead got:


bash-4.4$ cat | ls
file1 file2 file3


And it just holds there awaiting for input. Then after just enter it stop the execution with a result of:

bash-4.4$ cat | ls
file1 file2 file3

bash-4.4$

It feel like bash its prioritizing ls over cat, does anyone know what is going on underneath?


Thanks!




https://redd.it/1apxpef
@r_bash
some help with this example noscript

So lets say I have 50 different noscripts I need to run.
For the last section that checks error codes -ne 0, is there a way
I can some how put this in some loop that checks $exitcode1-50
without having to state each variable.

-----------------------
#!/bin/bash

testnoscript1="test1.sh"
testnoscript2="test2.sh"
testnoscript3="test3.sh"

# Run the first test noscript and store the exit code
$testnoscript1
exitcode1=$?

# Run the second test noscript and store the exit code
$testnoscript2
exitcode2=$?

# Run the third test noscript and store the exit code
$testnoscript3
exitcode3=$?

# Check the exit codes and exit with error 1 if any of them is non-zero
if $exit_code_1 -ne 0 || $exit_code_2 -ne 0 || $exit_code_3 -ne 0 ; then
exit 1
fi

https://redd.it/1aqzji0
@r_bash
Using JQ to return the index number for every occurrence of an element in an array

Take the following example. How can I use jq to return an array of the embeds index number for each occurance of the field element in the Json? I.e. [0 0 1 2 2 2]

{
"embeds":
{
"name": "Name1"
"field": "Field1"
"field": "Field2"
}
{
"name": "Name3"
"field": "Field3"
}
{
"name": "Name1"
"field": "Field1"
"field": "Field2"
"field": "Field3"
}

}

&#x200B;

https://redd.it/1ard3he
@r_bash
Interested in using zsh to run a program that requires bash...

I feel like this would be an easy lift for an experienced linux user but I haven't done more than a few google searches on this yet...

This app requires a settings.sh noscript sourced before you use it, so I cant just alias all the cmds with `bash -c $app`. I need the bash shell env setup by the settings.sh, then I need to be able to have zsh push all cmds starting with that apps name to the shell that has been setup.

I feel like there's got to be a way to have cmds run from zsh that open a bash shell and then specify in .zshrc (with aliases) which cmds get sent to that bash shell...

also, the only reason this particular linux box exists is to run this app, so I don't care if the solution somehow lightly interferes on other use cases...

thanks for reading.

&#x200B;

https://redd.it/1ard6yd
@r_bash