1

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.

1

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.

  • 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 '14 at 19:34
  • @Kaly You have modified variables.sh as I told you? – Hauke Laging Jun 1 '14 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 '14 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 '14 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 '14 at 20:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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