r_bash – Telegram
Started learning bash

Hi everyone today I started learning bash with no prior knowledge.I’m excited but also moving at a realistic pace so I don’t feel overwhelmed. I’m currently watching the Microsoft developer series (Bash for beginners)on YT.If you have any tips and advices for me I’ll be happy to accept them.

https://redd.it/15h1sd0
@r_bash
Github noobie (somewhat bash related)

I want to make my bash scrips available so I created a github profile and uploaded some noscripts to a public repo.

Here comes the issue:
I can't just git clone a noscript from inside the terminal without being asked for authentication. How do I taggle this? I'd like to be able to just git clone without any further ado. I don't mind to have the noscripts available for everybody.

I know, pretty noob question...

https://redd.it/15h8l4x
@r_bash
Array not populating in bash noscript ?

Here is a link to my bash noscript that I am working on , as long with a text file and awk file that I use with the noscript.

https://pastebin.com/xTGyk5su

For what ever reason my quotawheels array is not being populated and I cant figure out why ? If anyone can help , I would really appreciate it.

https://redd.it/15hhgek
@r_bash
need to extract stats from a webpage

ive used wget to download a url. so i have a html file. i need to extract all these stats fromthe text in it and tabulate them. i don't think the sed command can do that?

https://redd.it/15hl0v1
@r_bash
Bash History full of garbage

Hi All,

Linux Mint 21.2 Victoria -- Cinnamon

Below I copied a small chunk of the random stuff in my bash history, anyone know what is creating all this?

Thanks, Joe

===============
091 cd "`printf '%b' '\\0057mnt\\0057usb128\\0057backup\\0057current\\0057\\0056config\\0057nvim'`"

1092 cd "`printf '%b' '\\0057mnt\\0057usb128\\0057backup\\0057current\\0057\\0056config\\0057nvim\\0057lua'`"

1093 cd "`printf '%b' '\\0057mnt\\0057usb128\\0057backup\\0057current\\0057\\0056config\\0057nvim\\0057lua\\0057core'`"

1094 cd "`printf '%b' '\\0057mnt\\0057usb128\\0057backup\\0057current\\0057\\0056config\\0057nvim\\0057lua\\0057core\\0057plugin\\0137config'`"

1110 mc_print_command_buffer () { printf "%s\\\\n" "$READLINE_LINE" >\&12; }

1114 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056vim\\0057spell'`"

1115 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056vim'`"

1117 mc_print_command_buffer () { printf "%s\\\\n" "$READLINE_LINE" >\&12; }

1121 cd "`printf '%b' '\\0057usr\\0057share\\0057vim\\0057vim82'`"

1130 cd "`printf '%b' '\\0057usr\\0057share\\0057vim\\0057vim82\\0057spell'`"

1132 cd "`printf '%b' '\\0057usr\\0057share\\0057vim\\0057vim82'`"

1133 cd "`printf '%b' '\\0057usr\\0057share\\0057vim'`"

1134 cd "`printf '%b' '\\0057usr\\0057share'`"

1135 cd "`printf '%b' '\\0057usr'`"

1136 cd "`printf '%b' '\\0057'`"

1137 cd "`printf '%b' '\\0057usr\\0057share\\0057vim\\0057vim82\\0057spell'`"

1138 mc_print_command_buffer () { printf "%s\\\\n" "$READLINE_LINE" >\&12; }

1142 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056vim'`"

1789 cd "`printf '%b' '\\0057home\\0057joe\\0057VirtualBox\\0137VMs\\0057Debian12'`"

1790 cd "`printf '%b' '\\0057home\\0057joe\\0057VirtualBox\\0137VMs\\0057Debian12\\0057Logs'`"

1833 mc_print_command_buffer () { printf "%s\\\\n" "$READLINE_LINE" >\&12; }

1837 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share\\0057applications'`"

1838 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share\\0057doc\\0057kitty\\0057html'`"

1839 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share\\0057doc\\0057kitty'`"

1840 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share\\0057doc'`"

1841 cd "`printf '%b' '\\0057home\\0057joe\\0057\\0056local\\0057kitty\\0056app\\0057share'`"

​

​

https://redd.it/15hnoe4
@r_bash
Need to return the largest set

Hey all, I'm writing a noscript to query an endpoint that returns a list of groups and members. That's fine but is there a way that I can parse that list to just return the group with the largest membership?

https://redd.it/15hz5zf
@r_bash
Read function and many options

hey guys I am working lately on automated noscript that create file with variety extension and I came across with the needs to use read function with many option
# which one of these will work
A) read -are Pyfiles
B) read -a -r -e Pyfiles
C) or none of these and if so why?

and I am sure it C but why ?

https://redd.it/15i8omu
@r_bash
What's wrong with my noscript?

I am not ashamed to say that I am a bit of a noscript monkey when it comes to writing noscripts for bash; so I am probably doing something simple wrong.

I want to have a noscript that does a simple daily backup and then remove backups that are older than seven days old. I can see that the backup element works, but the removal of existing backups does not.

#!/bin/bash -e

# backup folder
destination_folder="/home/[folder]/backup/drupal/$(date +%F)"
archive_file="backup-$(date +%H%M).tar.gz"
rotate=7
mkdir -p "$destination_folder"

# backup

/bin/tar --exclude='/home/[folder]/backup' -Oczf "$destination_folder/$archive_file" /var/www/html/[site folder]/

# attempt delete older than 7 days
find /home/[folder]/backup/drupal/ -name '*.tar.gz' -mtime +6 -exec rm -rf {} \;

Would appreciate a fresh pair of eyes - I even tried to move the find and remove to a separate file (and tried rewriting it) to try and fix it, without success.

https://redd.it/15jib6f
@r_bash
Script to Automatically Start a glassfish domain once it's down?

Steps to reproduce this issue:

1) Glassfish domain called domain1 goes down.

2) Now, start that domain again.

Algorithm like this should exist:

if(domain1 is down)

{
path/to/asadmin start-domain domain1
}

How do I check if domain1 is down or not?
Assume there are multiple domains, around 8 of them And different servers have varying amount of domains.

My pseudocode:

domainarray= get list of all domains in an array

for(i=0;i<domain
array.length;i++)

{
if(path/to/asadmin stop-domain domainarray[i]==0) //assuming exit code 0 returns if the domain is already stopped
{
path/to/asadmin start-domain domain
arrayi;
}
}

Problems with my code:

I might be attempting to stop an unstopped domain which is very bad.

Is there an equivalent of systemctl is-active servicename for glassfish domains?

Sources: https://glassfish.org/docs/5.1.0/administration-guide/domains.html#giurs

https://www.freedesktop.org/software/systemd/man/systemd.service.html

https://redd.it/15jl0cu
@r_bash
Script to send an alert mail once the disk space is above 90% sends disk full even when the disk isn't full, how to resolve this issue?

#!/bin/bash

df -m > myfile
server_ip_address=$(ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1)

if awk '$2 > 10000 && $5 > 90' myfile ; then
echo "Disk Full in $server_ip_address"
else
echo "Nothing wrong with the server"
fi


When I execute the noscript, always get disk full as output?
Output of df -m looks like this:

Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 949305 512827 436479 55% /
devtmpfs 3811 0 3811 0% /dev
tmpfs 3823 0 3823 0% /dev/abc
tmpfs 3823 18 3806 1% /run
tmpfs 3823 0 3823 0% /sys/fs/cgroup
/dev/sda2 1014 175 840 18% /boot
/dev/sda1 1022 12 1011 2% /boot/efi
tmpfs 765 0 765 0% /run/user/2000



The outut of df -m differs from server to server. I am thus comparing if Size>10GB and Use%>90%. As some disk with size<10GB may not be important to be under 90% disk usage.


**Update 1:**

GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

OS is centos 7

Problem with the current code:

I need to compare the second row's columns, but my current code is comparing the first rows columns, that's why I suppose it's not working. How to resolve the issue?

https://redd.it/15jkztg
@r_bash
Script to send an alert mail once the disk space is above 90% sends disk full even when the disk isn't full, how to resolve this issue?

#!/bin/bash

df -m > myfile
server_ip_address=$(ip addr show $(ip route | awk '/default/ { print $5 }') | grep "inet" | head -n 1 | awk '/inet/ {print $2}' | cut -d'/' -f1)

if awk '$2 > 10000 && $5 > 90' myfile ; then
echo "Disk Full in $server_ip_address"
else
echo "Nothing wrong with the server"
fi


When I execute the noscript, always get disk full as output?
Output of df -m looks like this:

Filesystem 1M-blocks Used Available Use% Mounted on
/dev/mapper/centos-root 949305 512827 436479 55% /
devtmpfs 3811 0 3811 0% /dev
tmpfs 3823 0 3823 0% /dev/abc
tmpfs 3823 18 3806 1% /run
tmpfs 3823 0 3823 0% /sys/fs/cgroup
/dev/sda2 1014 175 840 18% /boot
/dev/sda1 1022 12 1011 2% /boot/efi
tmpfs 765 0 765 0% /run/user/2000



The outut of df -m differs from server to server. I am thus comparing if Size>10GB and Use%>90%. As some disk with size<10GB may not be important to be under 90% disk usage.


**Update 1:**

GNU bash, version 4.2.46(2)-release (x86_64-redhat-linux-gnu)

OS is centos 7

Problem with the current code:

I need to compare the second row's columns, but my current code is comparing the first rows columns, that's why I suppose it's not working. How to resolve the issue?

https://redd.it/15jmqf4
@r_bash
Is there any dmenu alternative configured in Lua/YAML/JSON/TOML?

I need a such application launcher which supports such configuration file format there is an already existing LSP and linting for. For instance, I've found projektor launcher, but it seems it's dead, no recent commits exist.

It would be nice to have config in Lua format as I plan to use Awesome window manager which uses Lua for setup. But at the same time I love YAML for it's simplicity because not always I need rich customization capabilities.

https://redd.it/15jx9yn
@r_bash
I created a noscript to start or stop an Aria2 downloader daemon.

For those of you who aren't familiar, aria2 has over 30,000 stars on GitHub because of its usefulness to people who are looking for one of the best command line download softwares on the internet.

I have included info and instructions at the top of the noscript for the user to read.

I hope someone finds this useful!

Cheers!

Aria2 Repo

GitHub Script

https://redd.it/15k2q0q
@r_bash
awk Match everything between two patterns, but ignore the first occurrence of the end pattern

# Overview

I'm hacking old Chromeboxes to be digital signage for the school district I'm working at over the summer. The functional needs are working, but I discovered that the Chromeboxes can't drive 4K displays without a significant performance hit.

I'm modifying the runtime noscript to check for available resolutions below 4K (or QHD if the Chromebox is using two monitors, just to be safe), and pick the highest supported resolution that preserves the aspect ratio of the current resolution if possible. Yeah, it's a bit overengineered, but I'm not going to be there if something goes wrong, so I want to make this as functional as possible.

# Problem

To get available resolutions for each monitor (iterated in a for loop), I'm parsing xrandr -q, which outputs the list of available resolutions in a nice, indented list like this:

Screen 0: minimum 320 x 200, current 3280 x 1080, maximum 16384 x 16384
HDMI-1 connected 1920x1080+0+0 (normal left inverted right x axis y axis) 527mm x 296mm
1920x1080 60.00+ 50.00 59.94
1680x1050 59.88
1600x900 60.00
1280x1024 60.02
1440x900 59.90
1280x800 59.91
1280x720 60.00 50.00 59.94
1024x768 60.00
800x600 60.32
720x576 50.00
720x480 60.00 59.94
640x480 60.00 59.94
720x400 70.08
DP-1 disconnected (normal left inverted right x axis y axis)
HDMI-2 connected 1360x768+1920+0 (normal left inverted right x axis y axis) 410mm x 230mm
1360x768 60.02
+
1920x1080i 60.00 59.94
1280x720 60.00 59.94
1024x768 75.03 70.07 60.00
1440x480i 59.94
800x600 75.00 60.32
720x480 60.00 59.94
720x480i 60.00 59.94
640x480 75.00 60.00 59.94
720x400 70.08

The command I have written to parse this information is

DISPLAY=:0 xrandr | awk -v mon="$MONITOR" '$0 ~ mon, $0 !~ /^ /{print $1}'

I want awk to print everything between line with the monitor's name (eg, HDMI-1) and the end of the indentation block, excluding the headings themselves (some help on that would be cool as well). With MONITOR = "HDMI-1"

1920x1080
1680x1050
1600x900
1280x1024
1440x900
1280x800
1280x720
1024x768
800x600
720x576
720x480
640x480
720x400

However, this only returns

HDMI-1

I think I understand the issue. The line that matches the start pattern also matches the end pattern, so awk only prints that line and calls it a job well done. How do I tell awk to ignore the line with the start pattern and stop at the next line that matches the end pattern?

https://redd.it/15k3fjk
@r_bash
Should I learn python, ruby or perl for shell noscripting in Linux?


I just heard bash noscripting is inefficient thing to learn for shell noscripting in Linux in 2023. While being a stubborn fellow, I'd still learn bash. Which of python, perl or ruby should I learn for shell noscripting?
I'd go with anything that has tons of resources available specific to shell noscripting. I know a little bit of python already.

https://redd.it/15kat8x
@r_bash
if you had to recommend only 1 bash book which one would you recommend (except tldp)

My requirements:

I don't read books line by line. I learn by solvng exercises given in the book. The exercises given in the book should have a kind of hint in the earlier chapter about it. i.e in theory.


That's all my requirement. Please recommend me a book.

I want to learn bash shell noscripting as I decided that it's not optional to learn bash as a sysadmin.

bash noscripting is mostly about sed, awk commands mastery so recommend 1 book for them as well and word processing and regular expressions in general.

https://redd.it/15kia65
@r_bash
Is there any way to change the pipe buffer size of a single specific pipe using shell tools?

I have a bash function that more-or-less does the following:

#!/bin/bash

# setup anonymopus pipe and tmpdir
exec {fd_nOrder}<><(:)
baseDir=$(mktemp -d)

# fork a coproc to print output file names to anonymous pipe (same names that `split -d` uses)
# this basically does: printf '%s\n' {00..89} {9000..9899} {990000..998999} {99900000..99989999} ...
coproc pOrder (

local v0 v9

v9=''
v0=''

printf '%s\n' {00..09} {10..89} >&${fd_nOrder}

while true; do
v9="${v9}9"
v0="${v0}0"

source <(printf '%s\n' 'printf '"'"'%s\n'"'"' {'"${v9}"'00'"${v0}"'..'"${v9}"'89'"${v9}"'} >&'"${fd_nOrder}")
done
)

# set exit trap to cleanup
trap 'kill '"${pOrder_PID}"'; exec {fd_nOrder}>&-; rm -rf "${baseDir:?}"' EXIT

# run each element of array x through someFunction in a forked process
# save output to tmpfile whose base name it read from pipe {fd_nOrder}
for kk in ${!x[@]}; do
read -r -u ${fd_nOrder}
{
someFunction "${x[$kk]}" >"${baseDir}/x${REPLY}"
} &
done

# cat all the outputs saved to ${baseDir}
# outputs will be in the same order as inputs are in array x
cat ${baseDir}/x*

# WARNING: FORKING LOOP ITERATIONS IN THE WAY SHOWN ABOVE IS DANGEROUS!!!
# Depending on how many elements are in x and how complex someFunction is
# this could easily crash your system. My "real" code does not do this.


The idea behind the above code is that the file names that I will be using to safe each forked process's output will nicely sort in order, so that I can just `cat` them and they will be ordered the same way as the inputs (in array `x`) are ordered. These are the same filenames as `split` uses when using numeric suffixes (`split -d`). They start at 00 and go up to 89, then when you hit 90 you add `00` to the end (giving 9000), then go from 9000 - 9899, when you hit 9900 add `00` to the end again (giving 900000), and keep on repeating indefinitely.

The coproc `pOrder` generates these filenames using bash's builtin shell expansion of `{#...#}` and writes them to an anonymous pipe, which is read by the main process just before it forks that iteration's `someFunction` call. It will keep generating filenames until it fills up the `{fd_nOrder}` pipe's buffer (64 kb --> ~10000 filenames initially, less as the file names keep getting longer). This has the benefit of the filenames being buffered in the pipe (so there is no wait to read them) and of not adding any additional complexity/IPC to the code (to tell pOrder when it needs to generate more filenames), but 10000 filenames is *way* more than really need to be buffered, and wastes memory and cpu time on generating filenames that will never be used.

**Is there some way (using the tools available to a shell...i.e., not with C/C++) to modify the buffer size of just the `{fd_nOrder}` pipe down to, say, 1 kb (or even less)?**

I dont want to globally change the pipe buffer size, just the buffer size for that specific pipe. Using procfs and/or sysfs is fine, though Id prefer a way that doesnt require root privileges. Using tools that are "standard" is preferable too...id rather not add any additional unusual dependencies to the code.

Thanks in advance!

***

**ADDITIONAL NOTES**

Ill again note that I dont (and you shouldnt) fork things like shown above. But, what is above makes for a simple easy-to-understand example, whereas the "actual" way I do this is more efficient and is safer and is *considerably* more complicated, so...

If you are curious, the "real" code im using this for is [here](https://github.com/jkool702/forkrun/blob/main/mySplit_nLinesAuto_test.bash), which is a prototype for a new IPC method for my [forkrun
project](https://github.com/jkool702/forkrun/blob/main/forkrun.bash), which is (IMO) basically a better/faster version of `xargs` that parallelizes loops using (almost) pure bash.

Its also worth noting that I previously implemented this using a function that more or less did

getNextFileName() {

for nn in "${@}"; do
# OMITTED - SOMETHING TO DEAL WITH BASH TREATING 00-09 AS OCTALS
((nn++))
[[ ${nn} =~ ^9+0+$ ]] && nn="${nn}00"
echo ${nn}
done

}

fName='00'
for kk in ${!x[@]}; do
{
someFunction "${x[$kk]}" >"${baseDir}/x${fName}"
} &
fName="$(getNextFileName ${fName})"
done

This worked, but generating them in batches using bash's built-in expansion via `printf '%s\n' {#..#}` is *considerably* faster and more efficient.

https://redd.it/15kn6o7
@r_bash
What is the difference between :class: and [:class:]?

I can't find anything on the net or manual.

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