0

I have a script which I need to run with many input combinations. Currently I'm doing it with a perl script but I want to learn how to do it in a shell.

I need to run ./script.pl a b for all combinations of a=1..100 and b =1..100

for ($a = 1; $a <100; $a++) {
    for ($b = 1; $b <100; $b++) {
      system "./script.pl $a $b";
        }
}

I'm currently using bash, but zsh or tcsh work too.

leonsas
  • 4,718
  • 6
  • 43
  • 70

1 Answers1

3

You have 2 choices of syntax in bash for loops.

for VARIABLE in 1 2 3 4 5 .. N
do
    commands
done

and

for (( EXP1; EXP2; EXP3 ))
do
    commands
done

The first is similar to java loops for navigating lists etc, while the second is the old school for loop.

You can rewrite your loops as either of these.

for b in {1..100}
do
   ./script $a $b
done

or

for ((b = 1; b <100; b++))
do
   ./script $a $b
done
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
Karthik T
  • 31,456
  • 5
  • 68
  • 87