r_bash – Telegram
Monitoring Changes to Bash Variable

I asked a question here

that I did not ask the question properly. I want to ask another question that is related to the previous question:

Is there a way that we can be aware of this change whenever the value of a bash variable changes?

Better to say:

Suppose in the .bashrc file, I want to track every time the value of the $! variable changes, its last value is stored in an array. The array variable is also defined in the .bashrc file and is available globally across all my shells.

backgroundpids+=($!)

And then I can check the array variable with the for loop whether the background processes are still running or not?

for pid in "${background
pids@}"; do
if kill -0 "$pid" >/dev/null 2>&1; then
echo "Background process with PID $pid is still running."
else
echo "Background process with PID $pid has completed."
fi
done

So I don't want to run and handle one or more background processes in the bash noscript file.

This is for just a challenge to learn more about bash, and it might sound stupid and pointless... but is it possible to use "trap" for example?

Is this useless or is there a method for it?

Sorry if the question is a bit strange.

https://redd.it/1co0zu3
@r_bash
Fast hour conversion

I was trying to do hour conversion. I thought that, since I am forgetting the syntax of date for the next I use it, I can write it down on a cheatsheet or create a function. I did this:

hour () {
if "$1" == -h ; then
echo "usage: hour timezone hourtoconvert"
else
date --date="$2 $1" +"%H:%M"
fi;
}

Any suggestions? I dont know how to convert date backwards btw. Thank you for reading.

https://redd.it/1co5d0e
@r_bash
Bug when pasting, and editing

How the bug looks.

What I did in the 2nd terminal was copying the text from the first one, pasting it, and pressing the left arrow key to edit it. The highlighted area is purely visual and I cannot change it.
I've also had issues were my prompt wouldn't loop when reaching the end of my terminal, and then it's all offset and it's impossible to edit it. This happens of every terminal when using bash. I've tried zsh, and it doesn't have this issue. If there is a fix, or this is already reported please let me know. My bash version is version 5.2.21 on void linux (how ever I've had the same issue on arch)

https://redd.it/1cohwzz
@r_bash
Github to Codeberg Bulk Migration Script

github 2 codeberg

Hello there!

I just made a noscript that allows the user to "bulk migrate" repositories from github to codeberg directly, if anyone is interested, more here: https://www.rahuljuliato.com/posts/github\_to\_codeberg

https://redd.it/1cop0a7
@r_bash
is there a way for see w x h in list of pics? ls -lh ¿w x h?

Hi, is it a way for see a list with w and h of pics?

w and h is the size of pics... width x height

I use imagemagick for it and If you know how IM command identify -format [%wx%h] * | more show in list tell me how because I only get a horizontal list all pic's data in the same file and I use pipe more for page the list because bash if not showme only first to right-margin

thank you and rigards

https://redd.it/1cp35oh
@r_bash
Scripting projects and learning to deploy applications

A product support engineer who's an aspiring sysadmin(linux) wants to have elaborative knowledge on "bash shell noscripting" as well as in "deploying applications over the linux virtual machine(without using k8s, docker etc, just pure shell noscript or ansible)".

I don't want to be a software developer but want to be a systems administrator. I am working as a product support engineer and want to enhance my skillset on noscripting and deployment. They say "learn by doing", so I want to follow it. But the issue is that I am unable to find what to do. There's very minimal need of noscripting in my day to day workflow. Even if there's need of noscripting, I have to use chatgpt to write it because the issues that I require aren't from beginner to advanced level sorted out.



Similarly goes for deployment. I can use chatgpt to write deployment ansible but I won't learn anything that way. Thus, I want a guided project based way to learn these two things. I am from Nepal and such jobs are in high demand as we're using old technologies in our country. Traditional tech is well paid. I haven't seen anyone using AWS in Nepal.

https://redd.it/1cpajfz
@r_bash
What is a Subshell and Child-processs and how are they used when writing a noscript?



https://redd.it/1cpjdxl
@r_bash
Is it possible to convert bash noscripts into Python noscripts?

Just wondering If it's possible

https://redd.it/1cpljyt
@r_bash
Why is last command not running?

I have a noscript that creates a temp file with filenames, it then feeds that list to clamscan for scanning only files that have been modified. I'd like to open the log file with the application "kate" at the end of the noscript and then exit the existing terminal. It isn't working. The noscript runs the scan, but then just exits without opening the logfile. What am I doing wrong?

#!/usr/bin/bash
# CLAMSCAN RECENTLY CHANGED FILES
# DIRECTORIES TO SCAN
scandir="/home/"
# TEMPORARY FILE
list
file=$(mktemp -t clamscan.XXXXXX) || exit 1
# LOCATION OF LOG FILE
logfile="/home/clamweekly.log"
# MAKE LIST OF NEW FILES
if [ -f "$log
file" ]
then
# use newer files then logfile
find "$scandir" -type f -cnewer "$logfile" -fprint "$listfile"
else
# scan modified in last 7 days
find "$scan
dir" -type f -ctime -7 -fprint "$listfile"
fi

if [ -s "$list
file" ]
then
# Scan files
clamscan -i -f "$listfile" > "$logfile"
else
# remove the empty file, contains no info
rm -f "$listfile"
fi
# OPEN THE LOG FILE TO REVIEW AND CLOSE THE TERMINAL
kate $log
file & disown
exit

https://redd.it/1cq428r
@r_bash
Data onion help

I am making a kind of data onion of sorts for someone where the end goal is to find a text file.

I do not know a lot about bash or coding in general but the person who im making it for does. He's basically a pro. I would like to get some help with encrypting or hiding the file using bash, and just generally making it difficult/ annoying.

Any help is appreciated.

https://redd.it/1cqb7lf
@r_bash
Run command as another user exactly as if the other user opened a prompt and typed the command

Im the root and want to run a command as the notroot user, how to make the command run like this -

su - notroot
echo $PATH
whoami
echo $-


Output

/usr/local/bin:<paths from .bashrc>
notroot
himBHs


Tried

/bin/bash -c 'sudo --login -u notroot echo $-'
/bin/bash -c 'sudo --login -u notroot echo $PATH'


Output

hBc
Missing .bashrc paths


Is there a way so all the things I define in the .bashrc (mainly additions to PATH) will show when exec command as another user



https://redd.it/1cqvdo7
@r_bash
Script for Watch Folder and then Copy sub-folders

New to noscripting, so I apologize for the most-likely-obvious question.

I'm looking to create a watch folder (testsource) and copy the sub-folders and their contents to a different location (testdest), then delete the original.

#!/bin/bash
source_d="/test/testsource/"
destination_d1="/test/testdest/"
inotifywait -m -q -e close_write "$source_d" |
while read -r path action file; do
cp -- "$path$file" "$destination_d1"
# rm -- "$path$file"
done

When I create files in /test/testsource, they are detected and copied to /test/testdest. But if I copy a folder with a testfile in it (/test/testsource/testfolder/testfile1) it does not. I did notice that if I then place a file into /test/testsource (test/testsource/testfile2), it will copy both the file as well as the other subfolder.

I presume its the "$path$file" format that is wrong, but I don't know what should be used. I tried "$path" but it didn't copy anything. I tried with " cp -r $path" but also didn't get it to work.

https://redd.it/1cqyi1f
@r_bash
Get file contents into a variable - the file is referenced by a variable

I want to get the contents of a file into a variable, but the file is referenced by a variable.

The code below hangs the session, and I have to break out.

resultsfile=~/results.txt

messagebody="$(cat $resultsfile)"

It is the same if I remove the quote marks.

If I simply messagebody=$(cat ~/results.txt) it works as I expect.

I have also tried using quotes on the $resultsfile (fails with cat: '': No such file or directory, and placing $resultsfile inside escaped quotes (fails with cat: '""': No such file or directory

I feel I'm missing something basic but can't quite get the syntax correct.

https://redd.it/1crc9d3
@r_bash
Can Make be used as a replacement for bash?

Hi,
I am a complete novice at make butbhave used bash fairly regularly. Recently my manager suggested to use make instead of bash. Not just in some use cases but in general, like "lets do everything with make as it is easier to debug than bash".
Now I don't understand make not I claim to be an expert in bash, but just by googling a bit i understood that make is mainly used as a built tool and might not be as general purpose as bash. Is that it or can make actually be used as a replacement of bash? I don't find the argument "easier to debug" convincing enough but see it as a more of a skill issue e.g. same goes for make for me, I don't know make so it's not easier to debug for me.

https://redd.it/1crhvpt
@r_bash
what is a "stack" in the bash shell?

hello, i keep hearing people talk about this thing called a "stack" in the bash shell

what is that? that is a "stack"?

thank you

https://redd.it/1crkta6
@r_bash
Help me improving my tmux start up noscript

So after I boot up my WSL2 Ubuntu I have a small noscript to setup my tmux session and start VsCode. It does (mostly) what it's suppost to do and I'm working on the next iteration:

#!/bin/bash

SESSIONNAME="main"
tmux has-session -t $SESSIONNAME >? /dev/null

if $? != 0
then
tmux new-session -s $SESSIONNAME -n admin -d
tmux new-window -n project -t $SESSIONNAME:
tmux send-keys -t $SESSIONNAME 'cd ./pyprojects/' Enter 'code' Enter

fi
tmux attach -t $SESSIONNAME
cd ~/pyprojects

So far so good. The frist flaw of this program is that it will create another 'project' window if called again. I'm unsure how to prevent this.

Secondly, the next step would be to source the last utilized Python venv. I haven't though of how to keep track of this. So for now I would go with just a default venv, stored in an env variable.

At the moment I'm mainly wondering if send-keys is actually the best / proper way of interacting with my tmux session. I'm affraid I'll be limited in what I can do from this bash noscript, as "things are happening in another console". So I'd be interested in your adivice here.

Also I'm interested in your ideas on how to track what venv had been used last. I thought using the fact, that VsCode keeps track of the last project, however I havn't been able to find/use this information.

https://redd.it/1croxg6
@r_bash
need help with xargs or mv

so im trying to move all files and folders within /sdcard1/Download/ to /sdcard/daya excluding a folder name dualnine in /sdcard1/Download. Here is the command i used

find /sdcard1/Download/ -mindepth 1 -maxdepth 1 ! -name dualnine | xargs mv -f /sdcard/daya/

but i get an error saying mv: dir at '/sdcard/daya/'

Can anyone pls explain I don't understand what is wrong

https://redd.it/1crq4fw
@r_bash
Bash and Unix course help

Hello!

I have been working for the past year or so as a DevOps engineer, the position relies on many tools and technologies and basic-intermediate Unix and python. I have been encountering more and more difficulties lately at work due to my limited knowledge of Unix, I know and understand the basics but I'm having some difficulties with Intermediate level stuff. So far, I have been heavily relying on ChatGPT to save me in these scenarios but this deducts from my learning.

I want a course on the Intermediate level that will help me with generic Unix and bash noscripting, stuff like getting a directory and splitting it based on "/" then printing one element, stuff like escaping characters and when they are used (bonus points if Dockerfiles are mentioned in specific), how quotation marks work and why " is different than ' or """ . I have already read on these things but I was wondering if a specific course would cover these better than lazily reading a bit of documentation and putting 0 practice in it.

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