2011-05-08 60 views
10

比方說,我有一個bash函數函數可以作爲後臺作業在bash子shell中調用嗎?

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 
} 

我是否允許調用該函數作爲一個子shell後臺作業?例如:

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

您怎麼看?

+0

試試看吧? – eggyal 2011-05-08 07:01:09

+0

@eggyal不幸的是,我在家,Linux服務器在辦公室:P – pepoluan 2011-05-08 07:05:34

回答

9

您可以將該函數作爲子殼體中的後臺作業調用。它會像你在你的例子中輸入一樣工作。

我在您的示例中以您演示的方式看到了一個問題。如果某些進程同時完成,他們將嘗試同時寫入OUTFILE,並且輸出可能會混淆。

我建議讓每個進程寫入它自己的文件,然後在全部進程完成後收集文件。

+0

感謝您的提示!標記了你的答案。 – pepoluan 2011-05-09 12:22:39

相關問題