开发者

Can a function be invoked in a bash subshell as background job?

开发者 https://www.devze.com 2023-03-03 12:59 出处:网络
Let\'s say I have a bash function Yadda() { # time-consuming开发者_如何学编程 processes that must take place sequentially

Let's say I have a bash function

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
}

Am I allowed to invoke the function as a background job in a subshell? E.g.:

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

What do you think?


You can invoke the function as a background job in a subshell. It will work just like you typed in your example.

I see one problem in the way you demonstrated it in your example. If some of the processes finish simultaneously, they will try to write to the OUTFILE at the same time and the output might get mixed up.

I suggest to let each process write to it's own file then collect the files after all processes are done.

0

精彩评论

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