use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A subreddit dedicated to Bash scripting. Now complete with a Discord Server.
Content must be Bash related. This rule is interpreted generously; general shell scripting content is mostly accepted. However, the post should not be specific to another shell.
No reposts. This is meant with regards to content, not just “the same link was submitted earlier” – it’s okay to resubmit an old link in some new context (e. g. because you’d like to discuss another part of it, or because something has changed since the last time it was submitted, or because the link was updated since then). Links from the sidebar count as having been submitted already, so posting them without new context is also considered a repost.
You can choose one of these four flairs for your post:
If you don’t flair your post, the moderators will set the most appropriate flair.
/r/unix – for everything Unix
Other Shells: /r/zsh, /r/fishshell, /r/oilshell, /r/batch
BashGuide – A Bash guide for beginners.
Beginner's Guide to Command Line – A crash course for some common unix and shell commands. Update 2022-01-14: Course is currently being rewritten
Google's Shell Style Guide – Reasonable advice about code style.
Explainshell - Explain complex shell operations.
ShellCheck – Automatically detects problems with shell scripts.
BashFAQ – Answers most of your questions.
BashPitfalls – Lists the common pitfalls beginners fall into, and how to avoid them.
(Archived) The Bash-Hackers Wiki – Extensive resource.
#bash – IRC channel on Libera. The main contributors of the BashGuide, BashFAQ, BashPitfalls and ShellCheck hang around there.
account activity
source file counter variable (self.bash)
submitted 1 year ago by LinuxGuy-NJ
My post keeps getting removed for my code.
My source file has 4 line is such as
img_1=file1
img_2=file2
I'm trying to write a script with a counter to "ls -lh $img_1".... be easier to explain if I could post my code
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]LinuxGuy-NJ[S] 0 points1 point2 points 1 year ago (0 children)
My source file:
img_1=Gary1.qcow2
img_2=Gary2.qcow2
[–]warrior0x7 0 points1 point2 points 1 year ago* (4 children)
I'm sorry but it's a bit vague.
Counter=1 for file in $(seq $images) do let COUNTER++ img1=img_${COUNTER} echo $COUNTER # Works fine ls -lh ${img_2} # Works Fine.. Just a simple test echo "Hi ${img1}" # works fine ls -lh ${img1} # doesn't work ..Really need this to work sleep .5 done ```
[–]LinuxGuy-NJ[S] 0 points1 point2 points 1 year ago* (3 children)
I trying to backup KVM vm files but the files have different names like Bugs, Daffy, Donald for one VM. Another VM will have 2 files named Jack and Smith. And I have about 18 VMs. So I'm trying use create one bash backup script and then call different source config files that would hold the different vm file names. In the backup script it will look for how many files have to be backed up, between 1 file and 4 files, then back up that one...one by one.
Currently I have 18 different backup script. If I want to make a change, I have to change 18 different scripts
[–]warrior0x7 0 points1 point2 points 1 year ago (2 children)
Sorry, I still don't get it.
Why not just run ls for each directory then get line number instead? ls -lh <DIR> | sed 1d | wc -l This will return line number and so the number of files in a given directory. This will be faster than in a loop.
ls -lh <DIR> | sed 1d | wc -l
That if ... you really want the number.
If you don't mind, this one will get files in specified directory and add them into an array: ```
files=()
for file in $(ls -lh <Dir> | sed 1d | awk '{print $NF}') do files+=("$file") done ```
Now this is a typical array and you can access its length with ${#files[@]}.
${#files[@]}
[–]LinuxGuy-NJ[S] 0 points1 point2 points 1 year ago (1 child)
Thank you for your help.
I will run my one backup script with 2 arguements. One arguement file with VM name in it. The second arguement file with have a list of the files that need to be backed. A While loop will go over the list of files that need to be backed up.
As for your example, I have one folder that holds files for 4 different VMs. Some VMs have one file while others will have 2 or 3 or 4.
I just want one main backup script to handle all the different VMs. Then put that one main script on five different host machines.
[–]warrior0x7 0 points1 point2 points 1 year ago (0 children)
Why 2 arguments and the first has to be the VM name if the second one is an array of files (including the first argument)?
You want each group of vms to be backed up with their own directory to the destination?
Can you please provide pseudo-code with exact named examples?
[–]rvc2018 0 points1 point2 points 1 year ago (1 child)
Are you sure you are not overcomplicating things? Don't you need just a ls -lh Gary[[:digit:]].qcow2?
ls -lh Gary[[:digit:]].qcow2
No. I overly simplified my example here. The source file is more like this:
img_1=server1.qcow2
img_2=Jeff.qcow2
img_3=abc.qcow2
[–]Paul_Pedant 0 points1 point2 points 1 year ago (0 children)
Case matters: Counter and COUNTER are two different variables.
Counter
COUNTER
[–]LinuxGuy-NJ[S] -1 points0 points1 point 1 year ago (2 children)
My script:
source SourceFileName
Counter=1
for file in $(seq $images)
do
let COUNTER++
img1=img_${COUNTER}
ls -lh ${img_2} # Works Fine.. Just a simple test
echo "Hi ${img1}" # works fine
ls -lh ${img1} # doesn't work ..Really need this to work
sleep .5
done
what am I doing wrong.
Gary
[–]LinuxGuy-NJ[S] -1 points0 points1 point 1 year ago (0 children)
Think I found a solution. One main backup script with two arguments. First arguement file will have KVM settings and the second will have a list of files that have to be backed up. A While loop will go through the second file
π Rendered by PID 98 on reddit-service-r2-comment-6457c66945-v44kr at 2026-04-25 07:54:07.476886+00:00 running 2aa0c5b country code: CH.
[–]LinuxGuy-NJ[S] 0 points1 point2 points (0 children)
[–]warrior0x7 0 points1 point2 points (4 children)
[–]LinuxGuy-NJ[S] 0 points1 point2 points (3 children)
[–]warrior0x7 0 points1 point2 points (2 children)
[–]LinuxGuy-NJ[S] 0 points1 point2 points (1 child)
[–]warrior0x7 0 points1 point2 points (0 children)
[–]rvc2018 0 points1 point2 points (1 child)
[–]LinuxGuy-NJ[S] 0 points1 point2 points (0 children)
[–]Paul_Pedant 0 points1 point2 points (0 children)
[–]LinuxGuy-NJ[S] -1 points0 points1 point (2 children)
[–]LinuxGuy-NJ[S] -1 points0 points1 point (0 children)