I have a script which runs a script in the background. Now the script running in background returns some variable value and i want to catch return value in the parent script.
e.g.
Parent Script :
#!/bin/bash
./Back.sh &
pid=$!
echo "a=$a"
echo "b=$b"
echo "d=$((a+b))"
wait $pid
Child Script run in background
a=4
b=5
c=6
Now when i run ./BackGroundProcess.sh , expected output is :
a=4
b=5
d=9
what i get is
a=
b=
d=0
Any suggestions or approach how I can retrieve a variable value from a script running in background ?
A child script cannot pass variables to a parent script. There are ways to read the output of the child script in the parent script. For example:
$ cat back.sh
echo 4 5
$ cat fore.sh
TESTFIFO=./testfifo
mkfifo "$TESTFIFO"
./back.sh > "$TESTFIFO" &
echo hello
read a b < "$TESTFIFO"
echo "a=$a"
echo "b=$b"
echo "d=$((a+b))"
rm "$TESTFIFO"
$ ./fore.sh
hello
a=4
b=5
d=9
Alternatively, ksh93 and bash 4 can use coprocesses
Thanks dude.
Though I already tried this approach by redirecting output to TEMP.sh from the script running in background and then calling ./TEMP.sh from the script running in forground.
Though thanks for your input.
TR,
Shaishav