Return variable value from a script running in background | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Return variable value from a script running in background

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-26-2012
Shaishav Shah Shaishav Shah is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 21 January 2014, 6:53 AM EST
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Return variable value from a script running in background

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 :

Code:
#!/bin/bash
./Back.sh &
pid=$!
echo "a=$a"
echo "b=$b"
echo "d=$((a+b))"
wait $pid

Child Script run in background

Code:
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 ?
Sponsored Links
    #2  
Old 12-26-2012
rangarasan's Avatar
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 28 August 2014, 12:00 PM EDT
Location: Chennai, India
Posts: 496
Thanks: 9
Thanked 124 Times in 120 Posts
Hi,


Check this http://www.unix.com/shell-programmin...nd-parent.html


Cheers,
Ranga
Sponsored Links
    #3  
Old 12-26-2012
Shaishav Shah Shaishav Shah is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 21 January 2014, 6:53 AM EST
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks rangarasan.
    #4  
Old 12-26-2012
Scrutinizer's Avatar
Scrutinizer Scrutinizer is online now Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 29 August 2014, 10:02 AM EDT
Location: Amsterdam
Posts: 9,368
Thanks: 272
Thanked 2,338 Times in 2,098 Posts
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:


Code:
$ 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

Last edited by Scrutinizer; 12-26-2012 at 09:18 AM..
Sponsored Links
    #5  
Old 12-27-2012
Shaishav Shah Shaishav Shah is offline
Registered User
 
Join Date: Dec 2012
Last Activity: 21 January 2014, 6:53 AM EST
Posts: 21
Thanks: 0
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
expect_out buffer no such variable running script background shellscripter Shell Programming and Scripting 0 10-01-2010 01:34 AM
Running script in background shahnazurs UNIX for Advanced & Expert Users 3 06-04-2009 01:26 AM
Running the Script in Background. prabhutkl UNIX for Dummies Questions & Answers 2 05-13-2009 12:44 AM
send a new value to a variable in a running background process razziel UNIX for Advanced & Expert Users 2 04-22-2009 10:57 AM
How to export a variable from a child process running in background to the parent aixjadoo Shell Programming and Scripting 3 11-14-2008 04:35 AM



All times are GMT -4. The time now is 10:03 AM.