並列ループの作成

並列ループの作成

このループを並列にする方法:

for a in $(seq 1 3)
do
  for b in 0.1 0.2 
  do
    echo process with a=$a and b=$b &
  done
done

並列かどうか?実際、echo process with a=$a and b=$b各値の組み合わせに対してコマンドを並列に実行したいのですが、上記のシェルをa実行b した結果は次のとおりです。

process with a=1 and b=0.1
process with a=2 and b=0.2
process with a=2 and b=0.1
process with a=3 and b=0.2
process with a=3 and b=0.1
process with a=1 and b=0.2

ありがとうございます。

ベストアンサー1

GNU Parallelを使用すると、次のようになります。

parallel echo process with a={1} and b={2} ::: 1 2 3 ::: 0.1 0.2 
seq 1 3 | parallel echo process with a={1} and b={2} :::: - ::: 0.1 
parallel echo process with a={1} and b={2} :::: <(seq 1 3) ::: 0.1 0.2

並列化は価値がほとんどないechoので、これは単なる例であると思います。echo

おすすめ記事