0

How safe is it to use export in bash scripts when using GNU Parallel?

I have a parent script. parent.sh

(echo child.sh & echo child_two.sh) || parallel bash

wait
if [[ "$STATUS1" == "0" && "$STATUS2" == "0" ]];
then
    //continue
else
    //stop the process
fi

child.sh

Getting the STATUS1 based on another process

export STATUS1

child_two.sh

Getting the STATUS2 based on another process

export STATUS2

Is is safe to use export and does the value of STATUS1 & STATUS2 get resrt eveytime it is being run?

4
  • @roaima yes its export. Sep 19, 2022 at 10:32
  • @roaima This is just a sample code to get a better understand regarding my scenario. Actual case also similar to this but little bix complex. i need to execute both child.sh from parent.sh and need to get the execution state of the child processes. Based on that i need to execute another few files. Sep 19, 2022 at 10:36
  • @roaima Nothing is being passed to parallel in the code as there is no pipe, only a conditional.
    – Kusalananda
    Sep 19, 2022 at 11:11
  • @roaima Well, it's likely that this is a typo in the pseudo-code in the question.
    – Kusalananda
    Sep 19, 2022 at 11:38

3 Answers 3

2

Environments variables are passed into the child processes, not the other way.

If you want to transmit information to the parent process, then you want to put this data into one file per child and cat those files after the child processes have ended.

tmp=$(mktemp -d)
child.sh $tmp/file & child_two.sh $tmp/file_two
wait 
cat $tmp/* 
rm $tmp -rf

If you want to make some optimization, feel free to use a higher level language.

0

I think you will benefit from looking into parset https://www.gnu.org/software/parallel/parset.html

$ parset myvar 'echo do stuff with {};(exit {}); echo $?' ::: 0 0 1 2 3 0 0
$ echo "${myvar[3]}"
0

You can't do that because export is setting a variable in a process that is about to be discarded; the parent can never see it.

However, assuming you're using bash, you also don't need to:

child.sh &
child_two.sh &

wait -n
STATUS1=$?
wait -n
STATUS2=$?

The wait -n returns the exit status from one of the processes, and repeating it gets the status from the other.

The only gotcha with this recipe is that there's no way to know which status came from which process (if you need that read up on bash Job Control).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.