10

假设我有一个 bash 功能

Yadda() {
  # time-consuming processes that must take place sequentially
  # the result will be appended >> $OUTFILE
  # $OUTFILE is set by the main body of the script
  # No manipulation of variables in the main body
  # Only local-ly defined variables are manipulated
}

我可以在子外壳中将函数作为后台作业调用吗?例如:

OUTFILE=~/result
for PARM in $PARAMLIST; do
  ( Yadda $PARM ) &
done
wait
cat $OUTFILE

你怎么看?

4

1 回答 1

10

您可以将该函数作为子shell 中的后台作业调用。它将像您在示例中键入的那样工作。

我发现您在示例中演示的方式存在一个问题。如果某些进程同时完成,它们将尝试同时写入 OUTFILE,输出可能会混淆。

我建议让每个进程写入自己的文件,然后在所有进程完成后收集文件。

于 2011-05-08T09:32:03.183 回答