Returning values from child to parent shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Returning values from child to parent shell
# 1  
Old 07-14-2005
Returning values from child to parent shell

I need to send the status from child shell failure to parent shell. I would like to know how could we accomplish this.


My parent.sh is as below:

#!/bin/ksh
set -x
echo "I am in parent shell now..."
child.sh
ret_stat=$?
echo "rest_stat=$ret_stat"
echo "I am below parent shell end..."


My child.sh is as below:
#!/bin/ksh
set -x
cho "I am in child shell now..." > test.log 2>&1
if [[ $? -ne 0 ]]
then
exit 100
fi
echo "I am in child shell end.."

I would like to capture the failure status from child.sh The statement in bold in child.sh is mistyped. I would like the 100 (failure) status sent to parent shell. Note that I would also like to re-direct my output to test.log at the same time.

Per above, I get success(0) status in my parent.sh ret_stat variable after the call to child.sh. This is happening because of redirection into the log file.
I would like the child shell to return 100 (failure) to parent ret_stat variable.

Let me know how would we accomplish this with the re-direction to log in the child.

Hope I made my self clear.

Thanks,
AC
# 2  
Old 07-14-2005
No problem with the redirection. This would only be a problem if you had any output from the child.sh that was getting assigned to a variable in parent.sh.

Do you have the current working directory in your path? If not, then parent.sh will not be able to execute the child.sh script, as it will not find it in the path.

You have to change the child.sh line in your code to ./child.sh.

parent.sh
Code:
#!/bin/ksh
#set -x
echo "I am in parent shell now..."
./child.sh
ret_stat=$?
echo "rest_stat=$ret_stat"
echo "I am below parent shell end..."

child.sh
Code:
#!/bin/ksh
#set -x
cho "I am in child shell now..." > test.log 2>&1
if [[ $? -ne 0 ]]
then
exit 100
fi
echo "I am in child shell end.."

Cheers!
# 3  
Old 07-15-2005
There is a small correction in my parent.sh as below


#!/bin/ksh
set -x
echo "I am in parent shell now..."
child.sh >> logfile
ret_stat=$?
echo "rest_stat=$ret_stat"
echo "I am below parent shell end..."

Notice that I am re-directing the output of child.sh to logfile. And now when I check the status of ret_stat, it is successful even though thhe child fails...
this is because of re-direction.

I would like to capture the failure status here.....

Sorry for the confusion earlier.

Thanks in advance.
# 4  
Old 07-15-2005
Strange, I redirected the output of child.sh just like you have. The script still works fine. Return value is still 100. Have you tried to do as I had said in my previous post? The "./child.sh" change?
# 5  
Old 07-15-2005
That's pretty wierd, i tested the redirection with my shell, I got sucess 0. Can we redirect the ./child.sh to logfile?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Returning multiple values in Shell

Hi I have a code as the following #!/usr/bin/ksh set -x row() { a=$1 b=$2 c=$(($a + $b)) d=$(($a * $b)) echo $a $b } e=`row 2 3` set $e echo "The value of c is $c" echo "The value of d is $d" My requirement is I need to pass two arguments to a function and return two values... (5 Replies)
Discussion started by: Priya Amaresh
5 Replies

2. Shell Programming and Scripting

Child exiting and not returning to parent script

I am having a parent scripts which reads a file with child scripts name. I need to read one by one child script , execute it and 1. If child script fails send mail to the team with the log file 2. If the child script executes fine then proceed with the next child script execution. #!... (3 Replies)
Discussion started by: nw2unx123
3 Replies

3. Programming

two-way piping values between parent and child

Hello everyone. I'm pretty new to the topic of fork(), pipe() etc. All day I've been trying to make my code execute but it doesn't seem to be working the way I understand it. :wall: Anyway, my task is: Create a child process that will recive from its parent two intiger values. Than it will... (5 Replies)
Discussion started by: jakubs11
5 Replies

4. Shell Programming and Scripting

Propagate exist status from a child shell script to a parent.

Hi All, I have a parent shell script A and a child shell script B. 1). If a command i.e a mysqdump fails in shell script B fails then I trap the error with this code if ] then func_exit "Failed to export the cleaned DB1.${MYDBNAME} database to the ${MYARCHIVEDIR} directory"... (1 Reply)
Discussion started by: daveu7
1 Replies

5. Shell Programming and Scripting

forking a child process and kill its parent to show that child process has init() as its parent

Hi everyone i am very new to linux , working on bash shell. I am trying to solve the given problem 1. Create a process and then create children using fork 2. Check the Status of the application for successful running. 3. Kill all the process(threads) except parent and first child... (2 Replies)
Discussion started by: vizz_k
2 Replies

6. Homework & Coursework Questions

Need help with deleting childīs parent and child subprocess

1. The problem statement, all variables and given/known data: I need to make an program that in a loop creates one parent and five children with fork(). The problem i'm trying to solve is how to delete the parent and child of the childīs process. 2. Relevant commands, code, scripts,... (0 Replies)
Discussion started by: WhiteFace
0 Replies

7. Shell Programming and Scripting

Parent/child Korn shell script help

I have the following two Korn shell scripts: SHELL1.ksh #!/usr/bin/ksh nohup sas /abc/123/sasprogram1.sas & SHELL2.ksh #!/usr/bin/ksh ./SHELL1.ksh wait nohup sas /abc/123/sasprogram2.sas & My goal is to run SHELL1.ksh within SHELL2.ksh. SHELL1.ksh runs sasprogram1.sas. I would like... (1 Reply)
Discussion started by: sasaliasim
1 Replies

8. Shell Programming and Scripting

parent shell is waiting upon the child shell

Hi, I haev to devlop a script which when executed will take in a sudo privelege and run a set of commands then will go back to parent shell and execute the rest of the command But the problem I am facing is that when the script is executed it takes the sudo privelege but it waits for the... (0 Replies)
Discussion started by: ruchirmayank
0 Replies

9. Shell Programming and Scripting

returning to the parent shell after invoking a script within a script

Hi everybody, I have a script in which I'm invoking another script which runs in a subshell. after the script is executed I want to return to the parent shell as some variables are set. However i'm unable to return to my original shell as the script which i'm calling inside is invoked in... (5 Replies)
Discussion started by: gurukottur
5 Replies

10. Shell Programming and Scripting

Returning Values (shell Script)

I have an application on Informix 4GL, and I am invoking the shell script from the program, but I need to know if the shell script work fine or not, in order to continue the process. I know that we can use $? to get the final status but this is on unix command. How can I return a value from the... (3 Replies)
Discussion started by: jennifer01
3 Replies
Login or Register to Ask a Question