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
Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
abhisek
  • 27
  • 6
  • Is it strictly defined on which shell interpreter/operating system should it run or solution should be portable? – mpasko256 Jun 06 '17 at 16:59
  • unix bash shell@mpasko256 – abhisek Jun 06 '17 at 17:04
  • Is'nt it some kind of duplicate of https://stackoverflow.com/questions/42067435/how-to-return-a-variable-from-one-shell-script-to-another-script?rq=1 question? – mpasko256 Jun 06 '17 at 17:21
  • that method is not working as i am running script1 and script2 with &, both will run in background – abhisek Jun 06 '17 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. – Charles Duffy Jun 06 '17 at 17:36
  • Possible duplicate of [bash background process modify global variable](https://stackoverflow.com/questions/13207292/bash-background-process-modify-global-variable) – Charles Duffy Jun 06 '17 at 17:37

2 Answers2

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
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)
mpasko256
  • 811
  • 14
  • 30
  • 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 – abhisek Jun 06 '17 at 17:21
  • I missed requirement that scripts need to be runned in parallel, so then added fourth solution – mpasko256 Jun 06 '17 at 17:35
  • 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. – Charles Duffy Jun 06 '17 at 17:38