2

How can variable creation be multithreaded in BASH scripts? For example, if a script contains the two lines below, then how can they be executed at the same time?

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

This example below does not work.

export BOTSUBSTITUTIONS=$(less --buffers=-1 ./conf/substitutions.xml) &
export BOTPROPERTIES=$(less --buffers=-1 ./conf/startup.xml) &
wait
6
  • 1
    Not really possible. You could put the & inside the () to background the less task, but the parent script isn't going to like that and simply skip over things, or still wait for the child process to finish before it returns control to the script and assigns the return value. bash is basically single-threaded. Commented Oct 14, 2012 at 0:33
  • Thank you, @Mark. That is what I thought, but I did not want to assume. Commented Oct 14, 2012 at 0:37
  • 1
    There is always the possibility to use temp-files. Commented Oct 14, 2012 at 0:53
  • stackoverflow.com/q/6537231/1689451 Commented Oct 14, 2012 at 0:59
  • @MarcB, make your first comment the answer, and then I can mark it as the best answer. Commented Oct 15, 2012 at 2:31

1 Answer 1

1

Redirect output of background processes to separate files, wait for the background processes finish, and cat the result back to your variables. Example:

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.