pep*_*uan 10 bash background function subshell
假设我有一个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
}
Run Code Online (Sandbox Code Playgroud)
我是否可以在子shell中调用该函数作为后台作业?例如:
OUTFILE=~/result
for PARM in $PARAMLIST; do
( Yadda $PARM ) &
done
wait
cat $OUTFILE
Run Code Online (Sandbox Code Playgroud)
你怎么看?
您可以在子shell中将该函数作为后台作业调用.它会像您在示例中键入的那样工作.
我在你的例子中看到了一个问题.如果某些进程同时完成,它们将尝试同时写入OUTFILE,输出可能会混淆.
我建议让每个进程写入自己的文件,然后在完成所有进程后收集文件.