r_bash – Telegram
Built-ins, distribution, and bootstrapping

## Background:
Bash seems nearly as ubiquitous as it gets (to me, at least), and I see so many examples of people doing neat things with it (and not just in their personal dotfiles; some examples here https://github.com/awesome-lists/awesome-bash)

## Questions:
1. Why doesn't there seem to be much effort or talk about developing more built-ins? (Blog on built-ins I found intriguing yesterday: https://blog.dario-hamidi.de/a/build-a-bash-builtin)
2. I've seen a lot of custom bootstrap/setup noscripts, and neat repos, but is there not any kind of more centralized way of sharing/searching/downloading bash noscripts/libs/utils? Like pip for python? Maybe I'm missing something, but there seems to be a lot of duplicated effort out there for reasons that don't always seem clear to me given how long bash has been out there, and how interested so many seem to be in using it.
3. I find myself unsure how best to approach sharing bash support in an environment, like extra libs, project setup utilities, etc. If you care to take the time, I'm curious what people think of bootstrap/setup noscripts, using curl/wget, or something like the makefile in this repo: https://github.com/jmcantrell/bashful. I'm open to anything people want to say/share, I'm just trying to understand.

___
### Personal Context:
(and very possibly irrelevant)
I've used Linux for years doing controls work for particle accelerators, but haven't had a real reason to really dive into bash until these last few months; after realizing that it seemed like a good fit for helping me address certain site specific issues at a new lab I just started at in the last year.

I've been learning by trying to write my own bash libraries to support bash noscripting and drafting/testing setup noscripts. All while thoroughly investigating all questions that pop up in my head along the way, or which shellcheck makes me curious about, digging through all of the examples I can find, comparing coding styles and common patterns, trying to incorporate things I see and.. just generally trying to get as much as I can out of the opportunity presented by my genuine interest in something I was weak at and which represents a good value-add at work.

From everything I've seen so far, r/bash seems like a great community that's already proven helpful to me. Whether you respond to this or not, thanks for this.

Cheers!

https://redd.it/1ecvoeg
@r_bash
Script to get lat/lon

I'm trying to figure out how to get the location (latitude/longitude) from the find my device web site. I'm using Linux on a Chromebook which does not have GPS. On the CB I can log into Find My Device to find my phone, which is next to the CB, and therefore get the lat/lon of my CB.

I think I can use curl (???) to get the find my device web page and somehow find the lat/lon by grepping download.

Then I'll feed to coordinates to navigation software - opencpn.

My noscript knowledge is pretty rusty, so any advice appreciated.

Is this a realistic project?

https://redd.it/1ed1s46
@r_bash
How to recursively copy, maintaining dir structure, with intermediate processing

I have a bunch of albums on an external hard drive that I would like to copy to an SD card that has limited storage capacity while maintaining the directory structure.

Some of the albums are in MP3 format, and some are FLAC. To this end, my thought is to simply copy the existing MP3s as-is and process the FLAC audio, creating smaller MP3s as needed, but I don't want to keep the newly-generated MP3s at all, whether on my laptop or on the external hard drive. I just want to create them to be sent to the SD card.

Is this feasible with Bash? If so, how? Otherwise, should I be looking at doing this in a different language, like Python? Or should I just store the MP3s?

https://redd.it/1edj8qp
@r_bash
Coloring issue with 3rd party application

Hey guys,

I'm trying to create a log filter to one of my bash apps, but I've came across an annoying issue, which I cannot fix with my knowledge sadly, so I ask for your kindness and help. <3

So basically, my code's important section for this aspect looks like this:

# Replace Startup Variables
MODIFIEDSTARTUP=$(eval echo $(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g'))
log
message "Starting server: ${MODIFIEDSTARTUP}" "running"

# Run the Server
eval "${MODIFIED
STARTUP}" 2>&1 | while IFS= read -r line; do
if [ "$line" =~ "blockable_text_here" ]; then
logblockedmessage "$line"
else
echo -e "$line"
fi
done

This works perfectly as I see the blocked messages (it's just for debug), but sadly the echo changes the 3rd party application's message colors to white. I tried to use printf, echo and awk, but sadly all output looks like this now for example:

https://preview.redd.it/h0jpszfdf4fd1.png?width=1590&format=png&auto=webp&s=5bdb0bdffe61602bd457fdfd04b1fc7dfd8d3c18

the expected original output looks like this:

https://preview.redd.it/ddz88wvlf4fd1.png?width=1196&format=png&auto=webp&s=86327e228e9730f72ab28e87338c999cdeb132e3

I would really appreciate that if you could guide me to fix this annoying issue. Of course the code work as intended, but the colors required for this service sadly.

Appreciate your time for reading this, even if you cannot help :(

EDIT: The working colouring is achieved with this by default:

# Replace Startup Variables
MODIFIEDSTARTUP=`eval echo $(echo ${STARTUP} | sed -e 's/{{/${/g' -e 's/}}/}/g')`
log
message ":/home/container$ ${MODIFIEDSTARTUP}" "running"

# Run the Server
eval ${MODIFIED
STARTUP}

https://redd.it/1edpts0
@r_bash
Du vs df how they work and why df is so much faster

If I do du -sh / it’s very slow but if I do df -h / it’s able to return immediately. Can anyone provide technical explanation of how these different commands differ at the lower level allowing df to be so much faster.

I’m guessing du must be reading all the files recursively or something but how does df manage?

https://redd.it/1edtxsc
@r_bash
How do you keep bash notes and oneliners to create a personal wiki?

I started writing down my bash notes 3 years ago on text files. Then i realized i need a structured approach. 6 months ago i switched to Markdown and Joplin and started linking related pages.

As i progress on shell, i needed a knowledge wiki including man pages, command examples, notes, questions and see also section. Closest for me for now is Logseq.

How do you keep your bash notes?

Thanks!

https://redd.it/1eeamna
@r_bash
Good book for a beginner to learn for bash

So I’ve got the very basics from a Udemy course on Bash, but I’d like a pretty comprehensive book that can assist me on my learning journey.

Any recommendations? Tks

https://redd.it/1eeiohr
@r_bash
Update noscript

I am trying to learn bash, and I wanted to make a noscript that would automatically update my system, preferably on startup. It looks like this. So far, I managed to make it run on startup, it makes a new file with correct name and that's basically it. It does not update anything or put any kind of output to file. Can you tell me what did I do wrong, or where can I find some info about it?

#!/bin/bash

# Script for automaticly updating arch linux and dumping all logs to log file.

sleep 10

RED='\0330;31m'
NC='\033[0m'
CURRENT_TIME=$(date +%d-%m-%Y-%H:%M-%S)
STRING_UPDATE="_update"
FILE_NAME="${CURRENT_TIME}${STRING_UPDATE}"
NAME=$(grep -E '^(VERSION|NAME)=' /etc/os-release)

if [ "$NAME" = "Garuda Linux" ; then
garuda-update --noconfirm >>"/home/konbor/noscriptlogs/update/$FILENAME.txt"
else
sudo pacman -Syu --noconfirm >>"/home/konbor/noscriptlogs/update/$FILENAME.txt"
fi

# /dev/null 2>&1 to skip output

UPDATE=$?

if $UPDATE -eq 1 ; then
echo "${RED}Udate failed log saved in ~/noscriptlogs/update/ as $FILENAME.txt${NC}"
bat ~/noscriptlogs/update/"$FILENAME.txt"
else
echo "Update complete"
bat ~/noscriptlogs/update/"$FILENAME.txt"
fi

https://redd.it/1eet0ms
@r_bash
Help!!!! I’m in school learning bash my professor won’t help!!!

Hello fellow bashers,

I have a few assignments left before my final an I’m doin horrible my professor are non-existent and on vacation 😡 during class. I have no guidance no one to help me and this is my last class before I graduate.

Can anyone tell me how: I can view a txt file from a folder

I tried catnames.txt
Echo $”$”

And it says doesn’t show any record of file and it clearly is in my c drive and my documents and download folder and I can see the names if I click on them.

Edit: how to view a .txt document in bash

Example: dog names.txt
Catnames.txt

https://redd.it/1efarl8
@r_bash
Help!! Where do I even start!!!
https://redd.it/1efhpua
@r_bash
I feel so stupid, just found out something about pushd, and not something obscure.

I just learned that pushd swap the top two directory in the stack and that pushd +n/-n rotate the stack. Frankly I felt that the directory stacks command were less useful than cd - .

I thought you could only change to a directory in the stack with popd but it removed a directory from the stack which would kind of make the whole thing a lot less useful. I frankly though dirs, pushd, popd were only useful in edge cases I wasn't knowledgeable enough to imagine.

Now I'm going to alias dirs to dirs -v, I wonder why it's not its default behaviour but I guess I might find out.

https://redd.it/1efxxgu
@r_bash
How to compare keys of two json documents?

As the noscript indicates I'd like to get a diff of the keys (and only the keys, not values) of two json documents. Anyone here who have an idea about how to do so?

https://redd.it/1eg5ac6
@r_bash
Triple nest quotes, or open gnome-terminal window and execute command later?

I'm trying to make a Bash noscript that can open Minecraft servers. So far I have this working, which makes a screen for playit.gg and another for the server I'm running in a new gnome-terminal window:

if ! screen -list | grep -q "serversminecraftplayit" ;
then

screen -d -m -S "serversminecraftplayit"

fi

SERVER=$(basename "$1")
SCREEN="serversminecraft"$SERVER

if ! screen -list | grep -q $SCREEN ;
then

screen -d -m -S $SCREEN

fi

gnome-terminal -- /bin/bash -c "gnome-terminal --tab --noscript=playit.gg -- /bin/bash -c 'screen -r serversminecraftplayit'; gnome-terminal --tab --noscript=$SERVER -- /bin/bash -c 'screen -r $SCREEN'";;

But for this to work as a control panel, it needs to open a tab for each server that's currently running. One way to do that would be to add another gnome-terminal call to that last part for each running server, but to do that, I'd need a third layer of quotes so I can assign the whole last command to a variable and add calls for each server. Something like (pretending ^ is a triple-nested quote):

COMMAND="gnome-terminal -- /bin/bash -c ^gnome-terminal --tab --noscript=playit.gg -- /bin/bash -c 'screen -r serversminecraftplayit';^"
COMMAND=$COMMAND" gnome-terminal --tab --noscript=$SERVER -- /bin/bash -c 'screen -r $SCREEN'"
#this would be a loop if I got it working to check for all running server screens
$COMMAND;;

The other, and probably more sensible, way to do this would be to figure out how to use either gnome-terminal or screen to open a new window, then open more screens in tabs of that same window and attach screens to them. Does anyone know how I might do either of these?

https://redd.it/1egamw2
@r_bash
How can i create a bash noscript to check that there is packet activity on either host IP A or host IP B?

I have this bash noscript but it is not working as intended since it gets stuck on the case that only one of the hosts have packet activity and wondering if there is a better way to solve the original problem? I do not really like having to manually check the /tmp/output files generated but it is fine for now. I just need a way to support `OR` for either host instead of waiting for both to have 10 packets worth of traffic.

#!/bin/bash

capturednstraffic() {
    tcpdump -i any port 53 and host 208.40.283.283 -nn -c 10 > /tmp/output1.txt
    tcpdump -i any port 53 and host 208.40.293.293 -nn -c 10 > /tmp/output2.txt
}
capturednstraffic & ping -c 10 www.instagram.com
wait



https://redd.it/1eh259z
@r_bash
User Creation Script - Is there a better way?

I've been an admin for many years but never really learned to noscript. Been working on this lately and I've written a couple of noscripts for creating/deleting users & files for when I want to do a lab.

The User creation and deletion noscripts work but throw some duplicate errors related to groups. I'm wondering if there is a better way to do this.

Error on Creation Script:

https://preview.redd.it/gxucc73qfyfd1.png?width=510&format=png&auto=webp&s=a29d0783dc735633a5c9d0897ac1a4a701f5b56d

Here is the noscript I'm using:

#!/bin/bash
### Declare Input File
InputFile="/home/user/noscript/newUsers.csv"
declare -a fname
declare -a lname
declare -a user
declare -a dept
declare -a pass

### Read Input File
while IFS=, read -r FirstName LastName UserName Department Password;
do
        fname+=("$FirstName")
        lname+=("$LastName")
        user+=("$UserName")
        dept+=("$Department")
        pass+=("$Password")

done<$InputFile

### Loop throught input file and create user groups and users
for index in "${!user@}";
do
        sudo groupadd "${dept$index}";
        sudo useradd -g "${dept$index}" \
                     -d "/home/${user$index}" \
                     -s "/bin/bash" \
                     -p "$(echo "${pass$index}" | openssl passwd -1 -stdin)" "${user$index}"
             done
### Finish Script

I'm guessing I probably need to sort the incoming CSV first and possibly run this as two separate loops, but I'm real green to noscripting and not sure where to start with something like that.

I get similar errors on the delete process because users are still in groups during the loop until the final user is removed from a group.

https://redd.it/1eh4dye
@r_bash
How to run noscripts in the background in Ubuntu?

Hello everyone,

I know that you can run your noscripts with “&” in the background, but this option does not work so well for me, are there perhaps other commands with which I can achieve a similar result?
Thanks for your help guys

https://redd.it/1ehb8dn
@r_bash
Can I push a config file and a noscript to run with ssh?

I have a noscript to run on a remote box and there is a separate config file with variables in it that the noscript needs. What would be a smart way to handle this? Can I push both somehow?

https://redd.it/1ehtyty
@r_bash
QEMU-QuickBoot.sh | Zenity GUI launcher for quick deployment of QEMU Virtual Machines

QEMU-QuickBoot is a Bash noscript i made with the help of chatGPT, It's designed to simplify the deployment of Virtual Machines (VMs) using QEMU, with a user-friendly GUI interface provided by Zenity. It allows users to quickly create and boot VMs directly from their desktop, using connected physical devices or bootable image files as the source media. User-Friendly Interface, Utilizes Zenity to present a straightforward interface for selecting VM boot sources and configurations. Multiple Boot Options: Supports booting VMs from connected devices, various file formats (.vhd, .img, .iso), and ISO images with virtual drives or physical devices. Dynamic RAM Configuration: Allows users to specify the amount of RAM (in MB) allocated to the VM. BIOS and UEFI Support: Provides options for booting in BIOS or UEFI mode depending on the user's preference. Includes error handling to ensure smooth operation and user feedback throughout the VM setup process.

noscript here at GITHUB: https://github.com/GlitchLinux/QEMU-QuickBoot/tree/main

I appreciate any feedback or advice on how to improve this noscript!

Thank You!

https://redd.it/1ehv23e
@r_bash
Any "auto echo command" generator app or website?

Hello. I have been wondering if there is any "auto echo command" generating website or app. For example, I'd be able to put the color, format, symbols etc. easily using GUI sliders or dropdown menu, and it will generate the bash "echo" command to display it. If I select the text, and select red color, the whole text will become red; if I select bold, it will become bold. If I select both, it'll become both.

It will make it easier to generate the echo commands for various bash noscripts.

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