0

I have a script suppose script.sh it has two scripts to run parallaly inside

sh script1.sh &
sh script2.sh &
wait;
echo $var1
echo $var2

i want to get these two variable(var1,var2) printed while running script.sh

cat script1.sh

var1=1;
export $var1

cat script2.sh

var1=1;
export $var1
6
  • Is it strictly defined on which shell interpreter/operating system should it run or solution should be portable? Commented Jun 6, 2017 at 16:59
  • unix bash shell@mpasko256 Commented Jun 6, 2017 at 17:04
  • Is'nt it some kind of duplicate of stackoverflow.com/questions/42067435/… question? Commented Jun 6, 2017 at 17:21
  • that method is not working as i am running script1 and script2 with &, both will run in background Commented Jun 6, 2017 at 17:29
  • "UNIX" doesn't say anything useful. There's a big difference between something that only runs on Linux, or something that only runs on Linux and MacOS, vs something that also needs to run on Solaris and HPUX and IRIX and AIX. Commented Jun 6, 2017 at 17:36

2 Answers 2

1

This can be done with coprocesses in bash 4.x, if your script1 and script2 write their variables to stdout:

#!/usr/bin/env bash
#              ^^^- Must be bash, not /bin/sh; must NOT start this script with "sh name".

start_seconds=$SECONDS
run_script1() { sleep 5; echo "one"; }  # replace these with your script1 or script2
run_script2() { sleep 5; echo "two"; }

coproc coproc_script1 { run_script1; }
coproc coproc_script2 { run_script2; }

read -u "${coproc_script1[0]}" var1
read -u "${coproc_script2[0]}" var2
echo "End time: $(( SECONDS - start_seconds )); var1=$var1; var2=$var2"

If the values weren't running in parallel, your End time would be 10 or more. Instead, it should in practice be 5 or 6 with the above code.

Sign up to request clarification or add additional context in comments.

Comments

0

Things that come to mind:

  1. If you use bash, you can run a script using dot:

    .script1.sh

    It will run script in the same environment, so you can modify environment variables of the "invoker" script.

  2. If it is a single output data, you can just print into a standard output of the inner script and intercept in the outer script:

    inner_result=`script1.sh`

  3. If it is a numeric value, you can return it as a exit code of the script (but I do not recommend this method as it is a bad practice in script development)

Edit:

  1. Use temporary file to store result and then remove it (but depending on operating system, named queues could be best dedicated solution)

3 Comments

both script1.sh and script2.sh returns single output data. but both has to be triggered together and val1 and val2 should get printed after wait call
I missed requirement that scripts need to be runned in parallel, so then added fourth solution
If you only need bash 4.x support, I'd actually suggest coprocesses. BTW, since 1-2 don't address the question at all and 3 would need to pass individual PIDs to wait to tie exit status back to them, I'd suggest heavy revision for this answer.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.