Simply bash script

Login or Register for Dates, Times and to Reply

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Simply bash script
# 1  
Simply bash script

I have a bash script that has 13 variables and runs commands on them.
I've copied the commands 13 times but I'd like to simplify it to simply add 1 to the previous and run 13 times.

The variables are
... up to 13. (more may be added later)

# executable stuff
mkdir "$dir01/".LogFiles
cp -R "$dir01"/*.aep\ Logs "$dir01"/.LogFiles/
rm -Rf "$dir01"/*.aep\ Logs "$dir01"/*.log 
 echo 01

mkdir "$dir02/".LogFiles 
cp -R "$dir02"/*.aep\ Logs "$dir02"/.LogFiles/
rm -Rf "$dir02"/*.aep\ Logs "$dir02"/*.log 
 echo 02

# repeated 13 times for each dir.

I'd like to simplify it to something like this:
n=1 # Amount to add each time
num=00 # Base number to start with

while [ x = 0 ]
    workon=$dir(($num += $n))
    mkdir "$workon/".LogFiles 
    cp -R "$workon"/*.aep\ Logs "$workon"/.LogFiles/
    rm -Rf "$workon"/*.aep\ Logs "$workon"/*.log 
    echo "$workon"

I don't know how to get it to stop when it's done 13 or whatever number of variables I have.

BTW I'm using cp and rm because mv won't clobber a directory.
These are render log files that are annoying and mostly useless so I don't mind overwriting files.

Any help with this code is very much appreciated.
# 2  
You are already using bash's shell arithmetics, why don't you use it for your loop:
$ while (( x-- )); do echo $x; done

BTW, don't cp and rm without error checking - if th cp fails, your log file is gone; use mv instead.
# 3  
I'm stuck

I defined paths for dir1 through dir13 as variables but I can't get the loop to add the number to the "dir" to create "dir1" and then use it as the defined variable.
This is my test code:
# dir1 through dir13 defined previously

run=13 # How many directories are we working on?
(( run += n )) # Add one to the number of directories

while (( $num < $run ))
      echo $dir$num
    (( num += n )) # When num = run exit loop

Instead of echoing the defined path for variable "dir1, dir2 ..." it echos "dir1" and "dir2," literally.
If I echo "$dir1" by adding it manually, I get the defined path as desired.
Why won't $dir$num echo as the path?
# 4  
Variables don't work that way. Dynamic variable names are discouraged in general -- that's the sort of thing you should be using an array or even just a space-separated string for.

You can dereference a variable name with ${!...} however. (This is a BASH-only feature.)


echo "${!VARNAME}"

You must assign the complete variable name to a string first, you can't put substitutions directly inside the ${}.
# 5  
That works!

Thank you. That works well.

How can I set a variable with a space in it?
"dir 1" and "dir\ 1" don't work.
I'm interested if having spaces in the variable definition would also work.
# 6  
Originally Posted by scribling
Thank you. That works well.

How can I set a variable with a space in it?
You can't. That's not a valid variable name.
# 7  
They designed it like that because it's a very bad idea to want spaces in the middle of a variable name. Use the underscore.
Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #288
Difficulty: Easy
In Linux, Logical Volume Manager (LVM) is a device mapper target that provides sound card I/O management for the Linux kernel.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Reverse prior batch job (without simply rewriting the script)

So, let's say I have a usual batch rename file like this: and I'd like to have a secon bat file, that can /reverse" the first. BUT, I don't want to simply "rewrite" it like move hello 254352426 but rather, have the new script take the source and destination and reverse it somehow... (6 Replies)
Discussion started by: pasc
6 Replies

2. UNIX for Beginners Questions & Answers

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed

In Bash shell - the ps -ef shows only the /bin/bash but the script name is not displayed ? Is there any way to get the script names for the process command ? --- Post updated at 08:39 AM --- in KSH (Korn Shell), my command output shows the script names but when run in the Bash Shell... (3 Replies)
Discussion started by: i4ismail
3 Replies

3. Shell Programming and Scripting

Simply Bash Script to Confirm that there are 2 Files in Same Folder

Im looking for 2 Files -> *.WAV and *.wav with find . -name 'unavail.wav' -o -name 'unavail.WAV' I need to list the Folders that contains these 2 Files? Thx in advance (13 Replies)
Discussion started by: valdez
13 Replies

4. Shell Programming and Scripting

How to block first bash script until second bash script script launches web server/site?

I'm new to utilities like socat and netcat and I'm not clear if they will do what I need. I have a "" script and a "" script that are started by emacs/elisp at the same time in two different processes. I'm using Cygwin bash on Windows 10. My... (3 Replies)
Discussion started by: siegfried
3 Replies

5. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

6. UNIX for Dummies Questions & Answers

Im new to bash scriping and i found this expression on a bash script what does this mean.

# check host value regex='^(||1|2|25)(\.(||1|2|25)){3}$' if ')" != "" ]; then if ]; then echo host $host not found exit 4 fi elif ]; then echo $host is an invalid host address exit 5 fi espeacailly the top regex part? ---------- Post updated at 06:58 PM ---------- Previous update was... (1 Reply)
Discussion started by: kevin298
1 Replies

7. Shell Programming and Scripting

how to make your bash script run on a machine with csh and bash

hi, i have a script that runs on bash and would like to run it on a machine that has csh and bash. the default setting on that machine is csh. i dont want to change my code to run it with a csh shell. is there any way i can run the script (written in bash) on this machine? in other words is there... (3 Replies)
Discussion started by: npatwardhan
3 Replies

8. Shell Programming and Scripting

passing variable from bash to perl from bash script

Hi All, I need to pass a variable to perl script from bash script, where in perl i am using if condition. Here is the cmd what i am using in perl FROM_DATE="06/05/2008" TO_DATE="07/05/2008" "perl -ne ' print if ( $_ >="$FROM_DATE" && $_ <= "$TO_DATE" ) ' filename" filename has... (10 Replies)
Discussion started by: arsidh
10 Replies

9. Shell Programming and Scripting

Setting basename and dirname variable to simply script.

Hello all, Can somebody explain to me how set up a basename and dirname variable to simplify this script. I currently have a 'infile' with the contents of FTTPDataPVC_ & BaaisDSLFeed. I need to add a basename and or dirname variable so that any additions can be made through the infile and not... (1 Reply)
Discussion started by: liketheshell
1 Replies

10. Shell Programming and Scripting

Simply shell script question

Struggling to add numbers in shell script.. ie. a=3 b=4 c=$a + $b echo $c I thought this should give me 7 - but cannot see what wrong.. should be simple answer eh? (8 Replies)
Discussion started by: frustrated1
8 Replies

Featured Tech Videos