开发者

Linux shell passing variable values to main programme

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开发者_Go百科 once and takes in all the 5 +i arguments. How could I do that without hardcoded it.

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

Thanks.


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


This should work:

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


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}


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[@]}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜