Reputation: 79
I have a number of bash scripts. for example
script1_1.sh
script1_2.sh
script1_3.sh
etc....
I was wondering, is there a way for me to run these all in another script. where by I don't have to manually add each. So through the use of a for loop? where it increments a number like
for (i=0; i < 40; i++)
./script1_[i].sh
I would also like to check each script worked, so if not, it would exit the for loop.
if ./script[i] (command unsuccessful)
then break?
Can anyone help me write this code? i'm very new to scripting languages.
Upvotes: 0
Views: 122
Reputation: 34914
You are really close to answering your own question. The C-style for loop will work fine for your scenario, but if your scripts are randomly named, you can use an array. You can use ||
, which will execute the right side if a non-zero exit status is returned. You could also use an if statement like you have proved in your question, with a "!" between if in the command (if not).
scripts=( script1_1.sh script1_2.sh script1_3.sh )
for script in "${scripts[@]}"; do
./"$script" || { echo "$script failed"; break; }
done
Using the for loop like in your questions:
for ((i=1;i<4;i++)); do
./script1_${i}.sh || { echo "$script failed"; break; }
done
Upvotes: 3
Reputation:
Since you have this question tagged with perl
, you can also do this in a Perl one-liner:
perl -e 'foreach(1..40){system("sh script_" . $_ . ".sh")==0 or die $!;print "script_" . $_ . ".sh done\n";}'
Upvotes: 0