Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Return variable value from a script running in background

Shell Programming and Scripting


Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 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 Unix and Linux 12-26-2012
rangarasan's Unix or Linux Image
rangarasan rangarasan is offline
Registered User
 
Join Date: Jul 2011
Last Activity: 24 February 2015, 3:14 AM EST
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,
RangaLinux
Sponsored Links
    #3  
Old Unix and Linux 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. Linux
    #4  
Old Unix and Linux 12-26-2012
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 May 2015, 12:00 PM EDT
Location: Amsterdam
Posts: 10,201
Thanks: 332
Thanked 2,692 Times in 2,408 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 Unix and Linux 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. LinuxLinux

TR,
Shaishav
Sponsored Links
Closed Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 06:08 PM.