tags:

views:

51

answers:

4

I have a programme, ./a that I run in a loop in shell.

for((i=1 ; i<=30; i++)); do
 ./a arg1 5+i &//arg2 that I need to pass in which is the addition with the loop variables
done

How could I passed in the arg2 which is the addition with loop variables?

Also, I has another programme which is ./b which I need to run once and takes in all the 5 +i arguments. How could I do that without hardcoded it.

./b arg1 6\
         7\
         8\ 
         9\.....

Thanks.

+2  A: 

Addition is performed with the same (()) you are already using, while concatenation is done simply with "":

for((i=1 ; i<=30; i++)); do
 let j=$((5+i))
 list="$list $j"
 ./a arg1 $j
done

./b $list
Kilian Foth
+1 This is the correct answer. Alternate syntax: `(( j = 5 + i ))`
Dennis Williamson
+1  A: 

This should work:

( for((i=5 ; i<=30; i++)); do ./a $((5+i)); echo $((5+i)); done ) | xargs ./b
mouviciel
A: 

For your second part I would do it like this

./b arg1 $(seq 6 35)

Or if you really require the addition within a loop

declare -a list
for n in $(seq 1 30) ; do
    list=("${list[@]}" $((5+n)))
done
./b arg1 ${list[@]}
Sorpigal
+1  A: 

In current bash versions you can use the {a..b} range notation. E.g.

for i in {1..30}; do
  ./a arg1 $i
done

./b arg1 {6..35}
Jon Brett