leonsas
leonsas

Reputation: 4898

For loop in terminal

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.

Upvotes: 0

Views: 352

Answers (1)

Karthik T
Karthik T

Reputation: 31952

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

Upvotes: 3

Related Questions