在gnu并行进程之间保持变量

时间:2015-09-17 23:03:22

标签: bash gnu-parallel

我有一个像这样的简单脚本:

#!/usr/bin/env zsh

count=0

while read -A words
do
    # set count for processed records
    ((count++))
    printf ":%d:\n" $count
    ..
    ..
    ..

    # go find something in somefile
    grep "$regex" somefile
    [ $? -ne 0 ] && echo "NOTHING" # check if we found any
    echo "--------------------------------"
done

然后我使用gnu parallel在多个线程中运行它:

time head -2 fileA | parallel --bar -k -j 4 "echo {} | ./myscript.sh" > result

除了我的count变量的值始终为1之外,一切正常 我希望它能像我在开始之前那样在每条读取线上递增 使用gnu parallel ..

以下是示例输出:

:1:  # is one
Num of words: (3)
acrylic, plastic, sheets
NOTHING
--------------------------------
:1:  # should be 2 here
Num of words: (2)
act, creators
NOTHING
--------------------------------

我将如何实现这一目标?

1 个答案:

答案 0 :(得分:1)

GNU Parallel有{#}这是工号。您可以使用它:

myfunc() {
  echo Looking at line number "$1"
  echo Input on stdin is:
  cat
}
export -f myfunc

cat /usr/share/dict/words | parallel -k --pipe -N1 myfunc {#}