tags:

views:

1752

answers:

2

I want to execute these commands

./a.out 1
./a.out 2
./a.out 3
./a.out 4
.
.

.so on How to write this thing as a loop in make file?

+7  A: 

The following will do it, if (as I assume by your use of ./a.out) you're on a UNIXy platform.

for number in 1 2 3 4 ; do \
    ./a.out $$number ; \
done

Test as follows:

qwert:
    for number in 1 2 3 4 ; do \
        echo $$number ; \
    done

produces:

1
2
3
4

For bigger ranges, use:

qwert:
    number=1 ; while [[ $$number -le 10 ]] ; do \
        echo $$number ; \
        ((number = number + 1)) ; \
    done

This outputs 1 through 10 inclusive, just change the while terminating condition from 10 to 1000 for a much larger range as indicated in your comment.

Nested loops can be done thus:

qwert:
    num1=1 ; while [[ $$num1 -le 4 ]] ; do \
        num2=1 ; while [[ $$num2 -le 3 ]] ; do \
            echo $$num1 $$num2 ; \
            ((num2 = num2 + 1)) ; \
        done ; \
        ((num1 = num1 + 1)) ; \
    done

producing:

1 1
1 2
1 3
2 1
2 2
2 3
3 1
3 2
3 3
4 1
4 2
4 3
paxdiablo
But if I want to go up to 1000, then cant we specify the limit?
avd
Why yes, you can :-) See my update.
paxdiablo
One thing is What is the use of the label qwert? Other thing is nested loops. It shows syntax error in thisfor n1 in 0 1 2 ; do \ for n2 in 1 2 3 ; do \ ./a.out $$n1 $$n2 ; \ donedone
avd
qwert is just a target name that's unlikely to be a real file. Makefile rules need a target. As for the syntax error, you're missing some stuff - see update.
paxdiablo
Since you're assuming his shell recognizes ((...)), why not use the much simpler for ((i = 0; i < WHATEVER; ++i)); do ...; done ?
Idelic
+3  A: 

If you're using GNU make, you could try

NUMBERS = 1 2 3 4
doit:
        $(foreach var,$(NUMBERS),./a.out $(var);)

which will generate and execute

./a.out 1; ./a.out 2; ./a.out 3; ./a.out 4;
Idelic