Pass return value of a function in background process


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Pass return value of a function in background process
# 1  
Old 02-15-2016
Pass return value of a function in background process

Hi,
I have created a function f1 defined in script A.sh .I have called this function in background . But I want to use its return value for another function f2 in script A.sh.
I tried declaring it as a global variable, yet it always returns the status as 0. Is there any way with which I can get the values from function f1 and use it in function f2 with function f1 running in background ??

template of my script:
Code:
STATUS=0; export STATUS

f1()
{
if<>
then
   STATUS=1
else
   STATUS=2
fi
}


f2()
{
f1 >output.txt &
echo "STATUS $STATUS";
}

f2

# 2  
Old 02-15-2016
You can't pass variables from a subprocess back to the calling process. You can intercept the output and assign it to a variable.

What sense does it make to run a process in background and then wait for its status (and/or output)?

Last edited by RudiC; 02-15-2016 at 06:59 AM.. Reason: emphasizing the subprocess communication
# 3  
Old 02-15-2016
Quote:
Originally Posted by RudiC
You can't pass variables back to the calling process. You can intercept the output and assing it to a variable.

What sense does it make to run a process in background and then wait for its status (and/or output)?

Its because the function f2 is function I have used to display status of the operations going on in function f1.
Can you explain "You can intercept the output and assing it to a variable."
How can we do the above?
# 4  
Old 02-15-2016
Use "command substitution" like
Code:
f1(){ echo 1; }
STATUS=$(f1)
echo $STATUS
1

This User Gave Thanks to RudiC For This Post:
# 5  
Old 02-15-2016
Quote:
Originally Posted by ashima jain
Hi,
I have created a function f1 defined in script A.sh .I have called this function in background . But I want to use its return value for another function f2 in script A.sh.
You are bringing together a few unrelated issues:

(1) While a shell function can modify a global shell variable, this works of course only if they run in the same process, because shell programming - at least those shells I know - don't have the concept of variables which are shared between processes.

(2) Your code shows that you start a child process, but you never wait for it, so even if there would be a possibility for the child process to pass information back to the parent, there is no point in the program you posted, where you would fetch this information.

(3) If the value you want to return from the function, is a small number (between 0 and 127), you can return it as the exit code of the invoked function. Of course, you still need to fetch it. You would do this by explicitly waiting for the function to finish using the wait command. This command returns the exit status of the child. Have a look at the description of your command the man page of your shell.
# 6  
Old 02-15-2016
And, if you wait for a background process, then it's the same as if you run it in the foreground.
So, in general the answer is "no, you cannot get a result from a background process".
--
The exit status of a function is the one from the last process.
Code:
f1(){
  ls "$@"
}
f1 "/dir"; echo "exit status $?"

If you want ta return the status from a previous command then use the following
Code:
f1(){
ls "$@"
save_exit=$?
echo "another command"
return $save_exit
}

# 7  
Old 02-15-2016
Quote:
Originally Posted by MadeInGermany
And, if you wait for a background process, then it's the same as if you run it in the foreground.
This is only true, if you need the result immediately after the background process has started, but in this case, it is pointless anyway to run the function as a child process. I guess the OP really wanted to do some work in between, for why else should he have started the background process.

Now thinking of this posting again, I believe that, for giving a good advice, we really would need to know what exactly the OP wanted to achieve.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Function - Make your function return an exit status

Hi All, Good Day, seeking for your assistance on how to not perform my 2nd, 3rd,4th etc.. function if my 1st function is in else condition. #Body function1() { if then echo "exist" else echo "not exist" } #if not exist in function1 my all other function will not proceed.... (4 Replies)
Discussion started by: meister29
4 Replies

2. Shell Programming and Scripting

Return: can only `return' from a function or sourced script

Not sure where the problem is. I can run the script without any issue using the following command. . /opt/app/scripts/cdc_migration.sh But it fails with the below error when I try it this way /opt/app/scripts/cdc_migration.sh /opt/app/scripts/cdc_migration.sh: line 65: return: can only... (1 Reply)
Discussion started by: svajhala
1 Replies

3. Shell Programming and Scripting

How to return from background process and check if it is running or not?

Hi Team, i am executing 3 scripts in background from 1 script and i want to send a message once the script gets completed.these scripts usually takes 1 hr to complete. My sample script is below, Vi abc.sh sh /opt/data/Split_1.sh & sh /opt/data/Split_2.sh & sh /opt/data/Split_3.sh & ... (3 Replies)
Discussion started by: raju2016
3 Replies

4. Shell Programming and Scripting

Capturing the return code from background process

Hi All, I was out not working on unix from quite sometime and came back recently. I would really appreciate a help on one of the issue I am facing.... I am trying to kick off the CodeNameProcess.sh in PARALLEL for all the available codes. The script runs fine in parallel. Let say there are... (1 Reply)
Discussion started by: rkumar28
1 Replies

5. Shell Programming and Scripting

Background process, return code and pid.

Hey all, Okay, this one is tricky and I'm not sure there is a niec way to do it, or indeed anyway to do it. The main issue revolves around timing out a hung ssh. I am doing this by creating a wrapper script for the ssh with the following requirements. My requirements are: Defineable... (5 Replies)
Discussion started by: RECrerar
5 Replies

6. Shell Programming and Scripting

How to put FTP process as a background process/job in perl?

Hi, I am using net::ftp for transferring files now i am trying in the same Linux server as a result ftp is very fast but if the server is other location (remote) then the file transferred will be time consuming. So i want try putting FTP part as a background process. I am unaware how to do... (5 Replies)
Discussion started by: vanitham
5 Replies

7. Programming

C function to start process but to return right away

Hello, I am using C on a Centos box with gcc as the compiler. I want a function to do something, them make an http request to some server (most probably using curl but suggestions are welcome) and return right away without waiting for the server's answer on that request. What should I use... (8 Replies)
Discussion started by: JCR
8 Replies

8. Shell Programming and Scripting

Return code of background process

Hi, I have a process that I run in the background that looks like this ${BASEDIR}/ksh/sqler.ksh ${compnames003} & and I would like to get the return code of the sqler.ksh script. so my code is like this ${BASEDIR}/ksh/sqler.ksh ${compnames003} & retcode=$? (3 Replies)
Discussion started by: c19h28O2
3 Replies

9. Shell Programming and Scripting

How to include RETURN KEY with Background process "&" in Shell Script

Hello All, I am a newbie in Shell script programming, and maybe you can help me with my query. I need to write a shell script (mntServer.ksh) that will start a background process and also to be able to run another script. The mntServer.ksh script contains: #!/bin/ksh... (1 Reply)
Discussion started by: racbern
1 Replies

10. Shell Programming and Scripting

background process return code

Hi I have the following piece of code that is calling another child process archive.ksh in the background while read file; do file_name=`ls $file`; ksh archive.ksh $file_name &; done < $indirect_file The problem is, indirect_file may contain anwhere from 2 to 20 different... (5 Replies)
Discussion started by: Vikas Sood
5 Replies
Login or Register to Ask a Question