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 !

Lucas
  • 1,139
  • 3
  • 11
  • 23
  • Hmm. *Part* of this is a duplicate of [How to produce a range with step n in bash to generate a sequence of numbers with](https://stackoverflow.com/questions/966020/how-to-produce-a-range-with-step-n-in-bash-generate-a-sequence-of-numbers-with); I haven't yet identified a good duplicate for the rest. – Charles Duffy Jun 27 '19 at 23:35

1 Answers1

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
  • 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? – Lucas Jun 27 '19 at 23:39
  • `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. – Charles Duffy Jun 27 '19 at 23:45
  • (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). – Charles Duffy Jun 27 '19 at 23:53
  • 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 Jun 28 '19 at 01:03
  • @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. – Charles Duffy Jun 28 '19 at 23:04