BASH中的多线程变量创建

时间:2012-10-14 00:31:59

标签: multithreading shell unix scripting bash4

如何在BASH脚本中创建多线程变量?例如,如果脚本包含下面两行,那么它们如何同时执行?

 export BOTSUBSTITUTIONS=$(less --buffers=-1 ./conf/substitutions.xml)
 export BOTPROPERTIES=$(less --buffers=-1 ./conf/startup.xml)

以下示例不起作用。

export BOTSUBSTITUTIONS=$(less --buffers=-1 ./conf/substitutions.xml) &
export BOTPROPERTIES=$(less --buffers=-1 ./conf/startup.xml) &
wait

1 个答案:

答案 0 :(得分:1)

将后台进程的输出重定向到单独的文件,等待后台进程完成,然后将结果返回到变量。 例如:

less --buffers=-1 ./conf/substitutions.xml >o1& o1=$!
less --buffers=-1 ./conf/startup.xml >o2& o2=$!
wait $o1 $o2
export BOTSUBSTITUTIONS=$(cat o1) ; rm -f o1
export BOTPROPERTIES=$(cat o2); rm -f o2