How to write loop in a Makefile?

后端 未结 12 2246
天命终不由人
天命终不由人 2020-11-27 09:38

I want to execute the following commands:

./a.out 1
./a.out 2
./a.out 3
./a.out 4
.
.
. and so on

How to write this thing as a loop in a

12条回答
  •  失恋的感觉
    2020-11-27 10:05

    The following will do it if, as I assume by your use of ./a.out, you're on a UNIX-type platform.

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

    Test as follows:

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

    produces:

    1
    2
    3
    4
    

    For bigger ranges, use:

    target:
        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:

    target:
        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
    

提交回复
热议问题