r_bash – Telegram
Better way to get variables out of file

I have two files:

#config
nextcloud-1="/var/www/nextcloud-1"
nextcloud-2="/var/www/nextcloud-2"

and the actual noscript:

#!/bin/bash
dir="$1"
dir=$(cat /opt/dirs.txt | grep "$dir" | sed -e 's/.*"\(.*\)".*/\1/')
cd $dir
sudo -u www-data php -f occ app:update --all

This noscript updates all Nextcloud apps on the selected instance: `./updateapps.sh nextcloud-1`. Is there a more efficient way of doing this, with error handling in case of a non-existent directory/variable for directory?

https://redd.it/14tzjik
@r_bash
Help me

So i got this problem as summer homework:Write a bash noscript that prompts the user to enter an upper limit. The program will calculate the sum of all prime numbers between 2 and the specified limit. Next, the program will have to generate a text file called "primes.txt" that will contain the prime numbers found, one per line. This is what i wrote:

#!/bin/bash
# Function that checks if a number is prime
function is_prime() {
if (( $1 < 2 )); then
return 1
fi
for (( i=2; i*i<=$1; i++ )); do
if (( $1 % $i == 0 )); then
return 1
fi
done
return 0
}
# Function that calculates the sum of prime numbers up to a given limit
function calculate_prime_sum() {
local limit=$1
local sum=0
for (( num=2; num<=limit; num++ )); do
if is_prime $num; then
      (( sum += num ))
fi
done
echo $sum
}
# Read a number from input
read -p "Enter an upper limit: " limit
# Calculate the sum of prime numbers up to the limit
prime_sum=$(calculate_prime_sum $limit)
echo "The sum of prime numbers up to $limit is $prime_sum"
# Generate a text file with prime numbers up to the limit
echo "Prime numbers up to $limit:" > primes.txt
for (( num=2; num<=limit; num++ )); do
if is_prime $num; then
echo $num >> primes.txt
fi
done
echo "primes.txt file generated successfully."
I dont know why but it gives an error :( can someone help me?

https://redd.it/14u6cfy
@r_bash
Using printf in a function to replace echo

Since I've learned about some drawbacks of the echo command, e.g. the lack of support for -- to indicate the end of options, I thought it would be reasonable to just stick with printf.

So I thought, why not write a function that allows me to use printf without having to retype the format strings.

Something like this:

output() {
printf '%s\n' "$*"
}

output "$foo is $bar"


But I'm not sure if this is syntactically correct. Normally, I would write printf '%s is %s\n' foo bar, instead of printf '%s\n' "$foo is $bar". Both seem to work the same; also, output "foo is bar" gives me the expected result. Shellcheck doesn't complain either. But is it safe to use, or are there any edge cases where my function wouldn't work as expected?

Thanks in advance.

https://redd.it/14tl682
@r_bash
What is a good tool to enable dynamic auto complete as you type in Bash 5.2

I have seen examples as you type, it will display a popup window above the line with the history of the shell.

https://redd.it/14toulb
@r_bash
Part of noscript doesn't execute when launched through cron

Here is my noscript: (tome is my username)

#!/bin/zsh

month_day=$(date -d "$D" '+%d')
week_day=$(date +%u)

if [ $month_day = 07 ] || [ $month_day = 14 ] || [ $month_day = 21 ] || [ $month_day = 28 ]
then
echo "$(date +'%H:%M:%S %d/%m/%y : weekly backup '$month_day' starts')" >> /home/tome/.log/backup.log
weekly_backup
echo "$(date +'%H:%M:%S %d/%m/%y : weekly backup '$month_day' ok')" >> /home/tome/.log/backup.log
else
echo "$(date +'%H:%M:%S %d/%m/%y : daily backup '$week_day' starts')" >> /home/tome/.log/backup.log
daily_backup
echo "$(date +'%H:%M:%S %d/%m/%y : daily backup '$week_day' ok')" >> /home/tome/.log/backup.log
fi

updatedb

and the relevant part of the output of `sudo crontab -e`:

0 1 * * * /home/tome/.noscripts/backup

(btw `/home/tome/.noscripts/` is where I store all my noscripts, including `daily_backup`, but I double-checked it's part of `$PATH` both for tome *and* root - also, the permissions are correct and the noscripts are executable).

However, here's what I get :

* from `~/.log/backup6.log` (6 because it's Saturday and that's how I classify my backups): ***simply nothing.***
* from `~/.log/backup.log`:

&#8203;

01:00:01 08/07/23 : daily backup 6 starts
01:00:01 08/07/23 : daily backup 6 ok

Notice how the noscript completed in less than one second.

I don't want to put my whole `daily_backup` code here, I don't think that would be useful. Let's just say that the noscript *always* takes *at least* 10 minutes to run, and *a lot* is supposed to end up in `~/.log/backup6.log`, since evey task completed succesfully by the noscript leaves a message in the log.

My only conclusion is that: `backup` launches normally, but then skips `daily_backup` altogether.

And all this only happen when I run `backup` from root cron, if I do a `sudo backup` in a terminal suddenly it seems to work.

Please help if you can (that's really a background job I need cron to do for me)

-----------

EDIT: I think the issue was that `cron` used its own, minimalistic, $PATH. So I specified my $PATH in the `cron` file's beginning, and it works now.

Thanks to everyone for your help.

https://redd.it/14tp1l4
@r_bash
sudo su in a bash/zsh noscript (but only part of that noscript)

I'm trying to run some noscripts on openSUSE Tumbleweed (or rather its WSL but it doesn't matter)

Here's what I want to do (mind that this code does not work, I'm trying to get as close as possible to this with a working noscript):

#!/bin/zsh

# some command as normal user:

cp /home/tome/.zshrc /home/tome/.zshrcroot
sed -i 's/agnoster/avit/g' /home/tome/.zshrc
root

# and here comes the part I don't know how to do

read -p "root password:" password

sudo su -p $password <<HERE

# here goes some stuff I want to run as the root user (not just prepending a sudo to the commands, the actual root user!)

cp /home/tome/.zshrcroot /root/.zshrc
cp -r /home/tome/.vim /root/.
cp /home/tome/.zsh
history /home/tome/.vimrc /root/.
cd /root
sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
git clone https://github.com/zsh-users/zsh-autosuggestions /root/.oh-my-zsh/custom/plugins/zsh-autosuggestions
source /root/.zshrc
source /root/.vimrc

HERE

# and finally some non-sudo user commands again, could be anything

pwd && echo "I'm not sudo"

I found many ways to "run as root" commands in a bash/zsh noscript, but none of them gave me a satifying result... I know I have done this kind of thing to automate SQL commands, so I don't see why I couldn't do it with sudo su...?

All help will be greatly appreciated!

https://redd.it/14thn0c
@r_bash
What do you think are the good features a bash prompt should have?

I recently went into customizing my PS1 variable and I noticed that Im not really sure of knowing what I want. Do I want to see the full path of my current directory? Do I want to see my user name and hostname? The only thing I know for sure is the git branch. Does anyone has an opinion of what is and what is not a good PS1 variable? Should I use linebreaks for splitting the prompt line from the input? Thank you in advance.

EDIT: no one is talking about differentiating different prompt inputs by using a linebreak. Imho I think this is really helpful.

https://redd.it/14sg2nw
@r_bash
ppd - a pushd/popd alternative written in bash

I use pushd/popd often, however I felt there was something lacking from the functionality offered by those commands, so I decided to build ppd.

ppd allows you to perform the following actions on a directory stack:

- list directories in the stack
- push directories to the stack
- pop directories from the stack
- navigate to directories in the stack
- add directories to the stack
- remove directories from the stack
- clear directories from the stack

You can find a demo of how it works in the README:

https://github.com/paololazzari/ppd

https://redd.it/14sj1cy
@r_bash
how do i list .txt files in defferent sub directories from the working directory



https://redd.it/14vpdcb
@r_bash
how to accumulate the strings through a loop in text file or a variable?

Hi everyone I am trying to read a JSON and putting the results in a text file or a variable(it would be optimal). I already have this error.json file. But I tried putting nested loop but doesnt work.

&#x200B;

outputFile="errors.json"

jsonData=$(jq '.' "$outputFile")

IDs=$(echo "$jsonData" | jq -r '.result.details.componentFailures | length')

for (( i=0; i<IDs; ++i )); do

#getting changeset name
branch=$(jq --arg i "$i" -r '.result.details.componentFailures[$i|tonumber].fullName' <<<"$jsonData")
# echo "$branch"

#getting their changeset record IDs
IDofCSs=$(jq --arg i "$i" -r '.result.details.componentFailures[$i|tonumber].problem' <<<"$jsonData")
# echo "$IDofCSs"

testing="${branch}=====${IDofCSs}"$'\n'

echo "$testing"

done

my output

Vehicle_Media_Update_Image_Sequence=====The field "EPM_Image_Sequence__c" for the object "EPM_Vehicle_Media__c" doesn't exist.

Account_Score_Inactivity_Decrease=====Update_Accounts_IER (Action) - We can't find an action with the name and action type that you specified.

WebForm_RecordTrigger_WebFormFlow=====Update_WFH_Score (Update Records) - The field "EPM_Score__c" for the object "EPM_Web_Form_History__c" doesn't exist.

The output is fine but I want it whole in one variable. Because every time its overriding the "testing" variable.

https://redd.it/14se6f0
@r_bash
multiple variables in one loop

I'm processing a bunch of videos, automated with a loop.

If I do:

for video in = .mp4;
[stuff getting done]
done

The loop successfully goes through every file in the directory, in order. video1.mp4, video2.mp4 etc.

But if I want to do an operation that goes through
two sets of files in each step, for example, video1.mp4 + audio1.m4a, how do I do this?

I am trying to create two variables, $video and $audio. Eventually they'll go into an FFMPEG command together, to add matching audio tracks to video files.

Here's what I've tried:

for video in ./videotrack/
.mp4; audio=./finalaudio/.m4a
echo $video
echo $audio

done

Unfortunately It gives me:

line 12: syntax error near unexpected token `audio=./finalaudio/
.m4a'

So what is the correct way to approach this?

https://redd.it/14vs2k7
@r_bash
Brace Expansion with variable

Hi guys!

I need help with this.

The last echo is not working.

Why is this happening?

I have tried several things and nothing works.

I'm lost. Help!

&#x200B;

echo txt{1,5}
txt
1 txt5

echo txt
{1..5}
txt1 txt2 txt3 txt4 txt5

END=5

echo txt
{1,$END}
txt1 txt5

echo txt{1..$END}
txt
{1..5}

https://redd.it/14ro2ud
@r_bash
How to escape special character, quotes (single and double) and spaces?

I am a beginner and I got to know that we have to escape some characters in bash.

this is the string stored in "testing" variable.

1.) Vehicle_Media_Update_Image_Sequence====Flow====The field "EPM_Image_Sequence__c" for the object "EPM_Vehicle_Media__c" doesn't exist.
2.) Account_Score_Inactivity_Decrease====Flow====Update_Accounts_IER (Action) - We can't find an action with the name and action type that you specified.
3.) WebForm_RecordTrigger_WebFormFlow====Flow====Update_WFH_Score (Update Records) - The field "EPM_Score__c" for the object "EPM_Web_Form_History__c" doesn't exist.

I tried to use tr but it didn't work.

https://redd.it/14vusjs
@r_bash
Can bash be used to crop images by inserting pixel dimensions?

Just like there is Bash commands/noscripts to edit folders and text files, I'm wondering if it can also be used to crop a bunch of images at once, using the pixels (or something else) as a parameter to crop all of them equally.

https://redd.it/14vwob1
@r_bash
I'd like some feedback and suggestions for how to improve my shell noscript

Hey everyone, I'm relatively new to programming and I've started to seriously get into writing small utilities for myself that automate a lot of time consuming tasks. This noscript aims to tackle a niche use case of mine - running games via WINE without leaning on apps like Lutris, Bottles, etc. This is more of a personal preference - I just like to simply type a single command into a terminal and be in-game within a few seconds.

The noscript accepts an .exe file as an argument and prompts you to enter a name for the prefix you'd like to use, then it sets everything up for you and runs the .exe file that was passed (ideally an installer of some sort). It then prompts you to install recommended libraries like \`dxvk\` and/or \`vkd3d\` if desired, and then searches for any unique .exe files located within the prefix and presents the user with a numbered list of the .exe files it found. Then, you once select the .exe you want to use it will automatically create an executable wrapper noscript for that game/exe file and place it in my $PATH.

#!/bin/bash

# Set the WINE prefix path -- This is the directory where
# the noscript will help to manage your WINE prefixes
PFX_PATH="$HOME/Games/Wine"

# Function for checking which directories are WINE prefixes
# and displaying them to the user.
set_prefix() {
echo -e "\033[1mAvailable WINE prefixes: \033[0m"
for dir in "$PFX_PATH"/*; do
if [ -d "$dir/drive_c" ]; then
echo "$(basename "$dir")"
else
continue
fi
done
read -p $'\e[1;33mEnter a name for the WINE prefix: \e[0m' PFX_NAME
export WINEPREFIX="$PFX_PATH/$PFX_NAME"
}

find_exe_files() {
echo -e "\033[1mSelect which .exe file to use: \033[0m"
PFS=$IFS
IFS=$'\n'
select option in $(
find "$WINEPREFIX" -type f\
-a -not \(\
-path "*/drive_c/Program Files*/Common Files/*" -o\
-path "*/drive_c/Program Files*/Internet Explorer/*" -o\
-path "*/drive_c/Program Files*/Windows Media Player/*" -o\
-path "*/drive_c/Program Files*/Windows NT/*" -o\
-path "*/drive_c/ProgramData/*" -o\
-path "*/drive_c/users/*" -o\
-path "*/drive_c/windows/*"\
-prune \)\
-a -name "*.exe"\
-print
); do
if [[ -n "$option" ]]; then

exe_dir="$(dirname "$option")"
exe_file="$(basename "$option")"

break
else
echo "Invalid option."
fi
done
IFS=$PFS
}

make_run_noscript() {
read -p $'\033[1mEnter a name for the run command to use: \033[0m' cmd

run_noscript_path="$HOME/.bin/$cmd"

cat <<EOF > "$run_noscript_path"
#!/bin/bash

pfx="$WINEPREFIX"
exe_dir="$exe_dir"
cache_dir="\$pfx/shadercache/$(basename "$WINEPREFIX")"

mkdir -p "\$cache_dir"
cd "\$exe_dir"

# Default environment variables
export WINEPREFIX="\$pfx"
export __GL_SHADER_DISK_CACHE=1
export __GL_SHADER_DISK_CACHE_PATH="\$cache_dir"
export WINEFSYNC=1
export WINEESYNC=1
export DXVK_HUD="compiler"
export GAMEMODERUNEXEC="wine $exe_file"

# Additional environment variables

gamemoderun "\$@"
EOF

chmod +x "$run_noscript_path"

echo "Run noscript created for $(basename "$WINEPREFIX")."
echo "Now, simply type '$cmd' in a terminal to run the game :)"
}

run_with_wine() {
cd "$exe_dir"
wine "$exe_file"
}

# Function for checking if 'dxvk-bin' is installed, and
# setting it up on the desired prefix
install_dxvk() {
read -p $'\e[1;33mInstall DXVK (Y/n)? \e[0m' INSTALL_DXVK
# read -p "Install DXVK (Y/n)? " INSTALL_DXVK
if [[ $INSTALL_DXVK =~ ^[Yy]$ ]]; then
if ! command -v setup_dxvk &> /dev/null; then
echo "The package 'dxvk-bin' is missing on your system. Aborting."
exit 1
else
echo "Installing DXVK on Wine prefix: $WINEPREFIX"
setup_dxvk install
fi
fi
}

# Function for checking if 'vkd3d-proton-bin' is installed, and
# setting it up on the desired prefix
install_vkd3d() {
read -p $'\e[1;33mInstall VKD3D Proton (Y/n)? \e[0m' INSTALL_VKD3D
# read -p "Install VKD3D Proton (Y/n)? " INSTALL_VKD3D
if [[ $INSTALL_VKD3D =~ ^[Yy]$ ]]; then
if ! command -v setup_vkd3d_proton &> /dev/null; then
echo "The package 'vkd3d-proton-bin' is missing on your system. Aborting."
exit 1
else
echo "Installing VKD3D Proton on Wine prefix: $WINEPREFIX"
setup_vkd3d_proton install
fi
fi
}

# Check if WINE is installed
if ! command -v wine &> /dev/null; then
echo "Wine is not installed"
exit 1
fi

# Display options if no argument is passed
if [ -z "$1" ]; then
cat <<EOF

Options:
-setup /path/to/*.exe Install the specified .exe file using WINE
-empty Create a new empty prefix

-run Run an exe within an existing WINE prefix
-remove Remove an existing WINE prefix
-modify Modify an existing WINE prefix
-dxvk Setup DXVK on an existing WINE prefix
-vkd3d Setup VKD3D on an existing WINE prefix

EOF
exit 0
fi

# Manage the options
case "$1" in
"-setup")
if [[ "$2" == "-empty" ]]; then
set_prefix
wineboot -u
echo "New WINE prefix $WINEPREFIX created."
elif [[ "$2" != *.exe ]]; then
if [[ -z "$2" ]]; then
echo "Missing .exe file"
exit 1
fi
echo "Not a valid .exe file"
exit 1
else
# Extract the directory and filename of the .exe
DIR="$(dirname "$2")"
FILE="$(basename "$2")"

set_prefix

if [ -d "$WINEPREFIX" ]; then
echo "Using existing WINE prefix $WINEPREFIX"
else
echo "Creating a new WINE prefix $WINEPREFIX"
wineboot -u
fi
wine "$DIR/$FILE"
install_dxvk
install_vkd3d
find_exe_files
make_run_noscript

fi
;;

"-run")
set_prefix
if [ -d "$WINEPREFIX" ]; then
echo "Using WINE prefix $WINEPREFIX"
find_exe_files
run_with_wine
else
echo "Not yet implemented."
exit 1
fi
;;

"-remove")
set_prefix
if [ -d "$WINEPREFIX" ]; then
read -p "Are you sure? This will delete the prefix and all data inside (Y/n)? " CONFIRM
if [[ $CONFIRM =~ [Yy]$ ]]; then
echo "Removing WINE prefix $WINEPREFIX"
rm -rf "$WINEPREFIX"
else
echo "Aborting."
exit 1
fi
else
echo "WINE prefix $WINEPREFIX does not exist."
exit 1
fi
;;

"-modify")
set_prefix
if [ -d "$WINEPREFIX" ]; then
echo "Modifying WINE prefix $WINEPREFIX..."
winecfg
else
echo "WINE prefix $WINEPREFIX does not exist."
exit 1
fi
;;

"-dxvk")
set_prefix
if [ -d "$WINEPREFIX" ]; then
install_dxvk
else
echo "WINE prefix $WINEPREFIX does not exist."
fi
;;

"-vkd3d")
set_prefix
if [ -d "$WINEPREFIX" ]; then
install_vkd3d
else
echo "WINE prefix $WINEPREFIX does not exist."
fi
;;

*)
echo "Not a valid option."
exit 1
;;
esac

All in all, I'm happy with the result but I'm always looking to improve and learn. I also leaned heavily on ChatGPT for this so I'd like some real human feedback on how to improve or reimplement existing features. To anyone who takes the time to read through this, I'd greatly appreciate the feedback!

https://redd.it/14rfgjg
@r_bash
Is there a linux/bash command that will recursively fix invalid filenames in my 3.5TB harddrive to make them more universally compatible with most software & filesystems?

So, here's the deal.

I've been making folder names that include characters like the colon : and parenthesis ( and dashes - etc. for years without thinking too much of it until now.

This was all on linux, and I didn't think much of it because the computer allowed me to name files however I wanted.

For example, I'll have a folder called
 BOOKS
or have
(Unabridged)
in the filename etc.

...and sometimes some weird glitch would have added quotes ' or " into the filename, too - this wasn't me, but I reckon it happened when it was downloaded or something.

But when I try to rsync, many will fail due to weird characters.

It's not just rsync. it's a lot of scenarios.

It's been a huge headache.

Even when I restored some of my folders from my cloud backup on Crashplan, the software itself renamed my folders by removing colons and adding slashes / to likely make them more universally compatible etc.

My question is, is there a simple and safe command that will recursively remove or fix these filename issues to make them universally compatible?

I wish I hadn't used colons, but I certainly don't want to break any files or screw up the renaming so royally that I have tens of thousands of misnamed files/folders either.

Thanks for any help.

https://redd.it/14qix2c
@r_bash
Unable to call executable from .gitconfig even though its in PATH, but can call it with full path

I'm struggling to understand why this is occurring, but it seems that .gitconfig doesn't have permission to call some executables by name, even if they are in PATH.

For context, I am setting up diff-so-fancy, and have cloned the git repo, changed the permissions of the executable and added the directory to PATH in my dotfiles. I then added the following line in the [core\] section of my gitconfig as instructed:

pager = diff-so-fancy | less --tabs=4 -RFX

however, that results in the following error

>diff-so-fancy | less --tabs=4 -RFX: 1: diff-so-fancy: Permission denied

&#x200B;

So I decided to make sure PATH contained the directory at the time of calling git diff and added the following line

pager = echo $PATH && diff-so-fancy | less --tabs=4 -RFX

which did have the appropriate path in it, prior to printing the permission-denied error

&#x200B;

Just having the executable directory in PATH and calling it by name in gitconfig does work on my Mac laptop for work.

&#x200B;

It also works when I just call diff-so-fancy by its name in the terminal, so it is clear to me that it is in the path and callable.

&#x200B;

I then decided to check what would happen if I had the full path to the executable within the gitconfig alias, so I added

pager = ~/path/to/diff-so-fancy | less --tabs=4 -RFX

which did have the expected output.

&#x200B;

It doesn't, however, work if I make a symlink to the executable and put it in /usr/bin/

&#x200B;

Its also worth noting that .gitconfig can call other executables, such as less just fine, without needing to specify the full path. The only time it doesn't work is when gitconfig is trying to call the specific executables.

&#x200B;

Also, yes, I know, I could just leave the full path in my gitconfig and have the expected output, but I just wanted to figure out why it wouldn't work, when it does on my Mac laptop.

&#x200B;

System Information:

Shell: Bash 5.1.16

Git 2.34.1

OS: Windows 10 22H2 19045.3086

WSL version: 1.2.5.0

WSL Kernel version: 5.15.90.1

https://redd.it/14qpk0y
@r_bash