Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have several shell scripts that I summarised within a global script. I am using some variables that are the same across all scripts, and I wrote a separate script for this, which I am sourcing in each sub-script. This is what my variables script looks like:

variables.sh
NUMBER="6"
TYPE="${NUMBER}_xyz"
STATE="S05"
DIR="path/file_${NUMBER}_blabla"
FILE="file.txt"

My global script, global.sh, looks like this:

source variables.sh
bash script1.sh
bash script2.sh
...

Now, I need to change the NUMBER variable in my variables script, run the global script, change the number, run the global script again etc. for each number from 6-15. At the moment I am doing this manually every time the global script has finished, but it's taking up a lot of time.

Has someone got an easy to implement tip for a newbie?

Thanks in advance!

share|improve this question

1 Answer 1

up vote 1 down vote accepted

First you have to adapt variables.sh so that NUMBER is not set unconditionally any more:

NUMBER=${NUMBER:-6}

That allows you to pass the value from the calling process:

for((NUMBER=6;NUMBER<16;NUMBER++)); do
  source variables.sh
  bash script1.sh
  bash script2.sh
done

General remark: I am surprised that this works without export statements or set -a.

share|improve this answer
    
My script is indeed executed 10 times now, but the problem is, it inputs the number 6 for the variable every time, not numbers from 6-15. –  Kaly Jun 1 at 19:34
    
@Kaly You have modified variables.sh as I told you? –  Hauke Laging Jun 1 at 19:36
    
Yep, I placed NUMBER=${NUMBER:-6} into my script, and also replaced the other "NUMBER"s inside variables.sh with ${NUMBER:-6}. Maybe that was wrong? –  Kaly Jun 1 at 19:38
    
@Kaly You should replace only the NUMBER= line. It shouldn't cause problems in the other positions, though. For debugging put a line echo $NUMBER at the beginning of variables.sh. –  Hauke Laging Jun 1 at 19:43
    
Sorry about that! It still didn't work, but I placed an "export" before "NUMBER=" and now it works perfectly. Thanks so much! –  Kaly Jun 1 at 20:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.