r_bash – Telegram
Defining a function

Guys can someone please tell me what is the difference between
function main{
}
And
main(){
}

https://redd.it/1c5hmwz
@r_bash
Case statement

Does anyone know how to read blank values in case statement. For example

Echo "please enter a number"

Read number

Case $number in

1 ) echo "number is 1" ;;

2 ) echo "number is 2" ;;

*) echo "please enter a number" ;;

esac

What if the user does not input anything.
How do I read that

https://redd.it/1c616ni
@r_bash
Help with noscripts run as sudo

i have two noscripts as below, and i usually call the parent one from the terminal.

parent.sh

#!\bin\bash

# $SCRIPTDIR defined here
sudo env PATH="
$PATH" "$SCRIPTDIR"/child.sh
wait

# other functions to call here

child.sh

#!\bin\bash

zypper dup -y

flatpak update -y &

# vencord is in "$HOME"/.local/bin, which is specified in my user's $PATH env var
yes | vencord -install -branch auto &

what i would like to do are:

1 - when i run the parent noscript with -x option, the child one should enable that option as well.

2 - currently when i run the parent noscript, only zypper would print out to stdin. flatpak and vencord should print out to the terminal too.

what modifications should i make to the noscripts above? many thanks.

https://redd.it/1c6abe6
@r_bash
How to parse a SQL file with a BASH noscript ?

I need a noscript that will parse a SQL query that contains name and URL that he will then cURL and execute.

https://redd.it/1c6dz5b
@r_bash
What does ;;& in switch case ?

Hello everyone !

I'm pretty new to Bash, I began last week. I'm working on the switch case statement, and on the GNU doc (https://www.gnu.org/software/bash/bash/manual/bash.pdf) on the page 13 of the document (with their count), they talk about the ;;& but I don't understand well what does it do.

Can someone explain it to me please ?

Thank you in advance for your answers.

https://redd.it/1c6he0o
@r_bash
Trying to create shortcut key, combined with fzf.

My intention is to create a shortcut key for bash, when pressed it gives you a list of recently visited directories to chose from, and upon choosing the selected directory the selected directory will be appended to the current cursor point.

I have this achieve almost half way using z command and fzf

z | awk "{print \$2}" | fzf

I'm happy with what this is giving me, but want to initiate this fuzzy search of recently visited directory anytime when I'm typing a command.

Say I have typed cd on my prompt, then initiate the above feature to chose from the list, then chosen option to be appended after the existing cd command.

Can someone shed some lights?

https://redd.it/1c74yjw
@r_bash
A Bash noscript that adds custom colors to languages in Nano

This bash noscript works on Debian-based OSs and adds color noscripts to your user's $HOME.

Just execute the noscript, open Nano with a shell noscript or whatever language was included, and see the results yourself.

./add-colors-to-nano.sh

You can find the GitHub noscript here.

Cheers guys.

https://redd.it/1c77xb0
@r_bash
I got weriod ENV setting.

RHEL 7.9

When I create new user, the new user always has a env setting DM_HOME.

I checked the /etc/skel/.bashrc, and other files, DM_HOME is not set there.

Where can I find the setting of the env DM_HOME?

Thanks a lot.

BTW, all the existing users have this env when login.

https://redd.it/1c7kdr7
@r_bash
--Base-Path Argument

Good day r/bash,


I have an issue that could be fixed by creating a --base-path argument noscript. Trouble is, I only have a vague idea of what it is, and no clue on how to make one. Seeking explanations, advice, guidance & and any form of documentation on the matter. Thanks !

https://redd.it/1c7nrly
@r_bash
Save command to run as function, array, or string?

I have a while loop and there's one part where it should run either commandA or commandB depending on condition that could already be determined beforehand, therefore I should save the appropriate command into a "variable" that's set before the while loop, e.g. if I were to store the command as an array:

if ...; then
cmd=(do this thing)
else
cmd=(do this other thing)
done
while ...; do
...
# expand variable to run it
"${cmd@}"
...
done

Usually, a function is defined for a command to be re-used but I don't really see defining a function conditionally, i.e. cmd() { a... } else cmd() { b... }. And also, if cmd is simply a string, then eval $cmd also works.

How do these methods compare? Is there a case to use one over the other? Is one more expensive internally? For readability, eval might be the most straightforward or least verbose but apparently eval should not be used lightly. Is expanding an array itself that is implicitly(?) executed also considered hacky or at least non-intuitive?

https://redd.it/1c8e8ww
@r_bash
case statement

what if i use exit instead of break in this case statement when do and cat are selected what are the difference if this was my noscript ?

echo "Please select an animal"
select animal in dog cat; do
case $animal in
dog)
echo "dog selected break"
break ;;
cat)
echo "cat selected"
break ;;
*)
echo "I don't recognize this animal" ;;
esac
done

https://redd.it/1c8n4lt
@r_bash
Can I run my noscript with options ?

I have my noscript, that I want to use it with options. Should I create functions indead of those cases ?

#!/bin/env bash

# Customize Console
PROMPT_COMMAND='echo -en "\033]0;$(FBD|cut -d "/" -f 4-100)\a"'
now=$(date)
echo "Undertale Jokes, that's all. Mainly. You know, as ya want."

while true
do

#Input
VHS=$(gum input --placeholder " Enter Command")

# Treat Input
case $VHS in

exit | q | esc | bye)
exit
;;
clear | cls)
clear
;;
puns | under)
gum pager < ./src/under.txt
;;
nintendo)
gum pager < ./src/nintendo.txt
;;
help)
gum pager < ./doc.md
;;
edit | text | txt | file | editor | nano | vim | vi)
$EDITOR "$(gum file "$FBD")"
;;
issue)
gum log --level info "Report issue at github.com/FBD/issues"
;;
time | date)
gum log --level info "$now"
;;
duck | goose)
gum pager < ./src/duck.txt
;;
annoy | dog)
gum pager < ./src/dog.txt
;;

*)
echo -n "Command not found or not implemented yet."
;;
esac
done

https://redd.it/1c8n90e
@r_bash
Having trouble writing bash noscript to management multiple git repos

Hello everyone, I have multiple git repos, let's say /home/plugin/, /home/core/, /home/tempate/.

I'm trying to write a bash noscript that will run git add . && git commit -m $msg && git push origin main on each of them. However my attempt to use cd to get into the appropriate repo isn't working

#!/bin/bash

read -p 'Message: ' msg

declare -a location=(
"core/"
"plugin/"
"template/"
)
dir=$(pwd)
for var in "${location@}"
do
cd "$dir/$var"
git add .
git commit -m "$msg" .
git push origin main --quiet
done

Can anyone point me in the right direction?

https://redd.it/1c8v0ym
@r_bash
How to create loop?

create a folder structure for each user, in the first folder create as many folders as uid +1, in each folder with a lower level uid -1 etc.

https://redd.it/1c8wsh0
@r_bash
Which command using this file denoscriptor?

(echo "$BASHPID" ; cat /dev/fd/0)

I can write to first shell, using new shell.

echo "hello" >> /proc/PID(BASHPID)/fd/0

This command sequence works but i really dont know how and what happened.

What is /dev/fd/0 here? Is it subshell or cat command?

And why PID(BASHPID) worked in the next command. I am so confused. Please help.

https://redd.it/1c92pmw
@r_bash
Why is this stack not empty?

Interestingly, this noscript doesn't give me the output I would expect.

#!/usr/bin/env bash
stack=""
push() {
stack+="$1"
}
pop() {
if [ -z "$stack" ]; then
echo ""
else
local len=${#stack}
local last=${stack:len-1:1}
stack=${stack:0:len-1}
echo "$last"
fi
}
push ""
result=$(pop)
if [[ "$result" != "[" ]; then
echo "false"
else
echo "true"
fi
echo "$stack"

Output:

>true
[

https://redd.it/1c9hcnw
@r_bash
can you help me the issue with this noscript?

I have to create a new accounts_new.csv file based on accounts.csv file and modify like that.

was

2,1,Christina Gonzalez,Director,,
8,6,Bart charlow,Executive Director,,
9,7,Bart Charlow,Executive Director,,

# should become:

2,1,Christina ,
8,6,Bart Charlow,Executive ,
9,7,Bart Charlow,Executive ,Gonzalez,Director,cgonzalez@abc.comDirector,bcharlow6@abc.comDirector,bcharlow7@abc.com

here is the code:

#!/bin/bash

# Check if the correct number of arguments is provided
if "$#" -ne 1 ; then
    echo "Usage: $0 <pathtoaccounts.csv>"
    exit 1
fi

# Check if the input file exists
if ! -f "$1" ; then
    echo "File $1 not found!"
    exit 1
fi

# Function to process each line of the input file
processline() {
    IFS=',' read -r -a fields <<< "$1"
    id="${fields[0]}"
    name="${fields[2]}"
    position="${fields[3]}"
    location
id="${fields1}"

    # Format name: first letter uppercase, rest lowercase
    formattedname=$(echo "$name" | awk '{print toupper(substr($1,1,1)) tolower(substr($1,2)) " " toupper(substr($NF,1,1)) tolower(substr($NF,2))}')

    # Format email: lowercase first letter of name, full lowercase surname, followed by @
abc.com
    formatted
email=$(echo "$name" | awk '{print tolower(substr($1,1,1)) tolower($NF)}')
    formattedemail+="@abc.com"

    # Append location
id to email if available
    if -n "$location_id" ; then
        # Check if the email already contains a number
        if [ "$formatted_email" =~ [0-9$ ]]; then
            formattedemail="${formattedemail%,}@abc.com"
        fi
        formattedemail="${formattedemail%,}${locationid}@abc.com"
    fi

    # Output the formatted line
    echo "${id},${fields[1]},${formatted
name},${position},${formattedemail},"
}

# Process the header row and write it to accounts
new.csv
head -n 0 "$1" > accountsnew.csv

# Add a newline character to the end of the file if it's missing
sed -i -e '$a\' "$1"

# Process each line (excluding the header) of the input file and append to accounts
new.csv
while IFS= read -r line || -n "$line" ; do
    if -n "$line" ; then
        processline "$line"
    fi
done < "$1" >> accounts
new.csv

echo "Processing completed. Check accountsnew.csv for the updated accounts."

# Ensure the output file exists and is readable
if [ -f "accounts
new.csv" ]; then
    echo "File accountsnew.csv created successfully."
else
    echo "Error: Failed to create accounts
new.csv."
    exit 1
fi




the test case says that accounts_new.csv is not found in the directory

https://redd.it/1ca748j
@r_bash
My first bash noscript - Hide.me VPN Linux CLI Server Switcher

Hi guys n girl,


i wrote my first bash noscript because i had a neat usecase and wanted to try out bash for some time.

In my case i wanted to have a easier and more elegant way to switch my VPN Server. I use hide.me atm and they provide a CLI Client for that purpose, but its not the most userfriendly and comfortable implementation.

I am not a dev so dont throw rocks at me :-P


Github/hide.me-server-switch

https://redd.it/1cabqgd
@r_bash
Can I define something like '~' (tilda)

Is it possible to define '~~' (or some other symbol) to behave like '~' ?

For example define '~~' to be /host_fs/home/user and then it will be possible to do things like cd ~~/Downloads etc.


Motivation: inside a docker container i have /host_fs/ I want that '~~' be some path in it and users can type stuff like vim ~~/config.txt

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