0

I have a program GG that needs two integers as arguments for parameters x and y as input, in this way:

./GG -x 0 -y 100 

I need to run GG over sequential start/end pairs of integers, like the pairs in each row here:

x y 0 100 100 200 200 300 ... ... 10000 10100 

The closest I get would be something like this:

for i in {0..10000}; do for j in {100..10100}; do ./GG -x ${i} -y ${j}; done; done 

but this will loop each j value over each i value, and this is not what I need.

Any suggestion is very welcome !

1

1 Answer 1

3

There's no need to loop over two values. Loop over one, but add your offset to it to get the other.

for ((i=0; i<=10000; i+=100)); do ./GG -x "$i" -y "$(( i + 100 ))" done 

See this running at https://ideone.com/r3qBZU

See the C-style for loop, and arithmetic expression syntax.

Sign up to request clarification or add additional context in comments.

5 Comments

Thanks very much @Charles Duffy !. I know it is a different question, but do you know by chance how to run GG over each pair of values using parallelization instead of waiting until each loop finishes?
for ((i=0; i<=10000; i+=100)); do printf '%s\0' "$i"; done | xargs -0 -P 8 -n 8 sh -c 'for arg; do ./GG -x "$arg" -y "$((arg + 100))"; done' _ is one approach. Which, by the way, is already in the knowledgebase if you searched for it.
(the -P 8 is number of processes to run at once, the -n 8 is number of items to handle by each process instance before it's restarted with a different group of numbers; tune to fit your hardware, tolerance for overhead, etc).
Hi @Charles Duffy, my very last question, apologies. The real GG program takes several arguments: GG -h -l -k -int. The parameter -int uses two integers, like those I mentioned (e.g. GG -h -l -k -int 0 100; GG -h -l -k -int 100 200; etc.). I am not sure if this adjusted script would be the correct one: for ((i=0; i<=10000; i+=100)); do printf '%s\0' "$i"; done | xargs -0 -P 8 -n 8 sh -c 'for arg; do ./GG -h 5 -l 10 -k 1000 -int "$arg" "$((arg + 100))"; done' _
@Lucas, at a glance, it looks about right to me. That said, change it from sh -c to sh -x -c, and it'll print a log of the commands it's actually running so you can compare.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.