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 Answer 1

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.

5
  • 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
    Commented Jun 1, 2014 at 19:34
  • @Kaly You have modified variables.sh as I told you? Commented Jun 1, 2014 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
    Commented Jun 1, 2014 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. Commented Jun 1, 2014 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
    Commented Jun 1, 2014 at 20:17

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.