Exit from n th child shell


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Exit from n th child shell
# 1  
Old 09-25-2008
Exit from n th child shell

Hi,
I am using ksh to write my shell script. I need to create multiple-level of nested sub shells in my script. Lets say I have at n th subshell. My question is how do I come out from there to main login shell.

If I use 'exit' command then it is exiting from just one subshell and back to (n-1) th subshell. But I want to come out all way to login shell.

Any idea, pointer, opinion would be highly appreciated.

Thanks
Rabi
# 2  
Old 09-25-2008
There is no way to directly cause the parent shell to exit. The usual method is to invoke the subshell in such a way that the parent is told to exit if the subshell exits with an error. Perhaps you could use this construct. Why do you need so many levels of shell scripts anyway?

Code:
# run subscript, exit if it fails
subscript || exit $?

# 3  
Old 09-25-2008
Quote:
Originally Posted by era
There is no way to directly cause the parent shell to exit. The usual method is to invoke the subshell in such a way that the parent is told to exit if the subshell exits with an error. Perhaps you could use this construct. Why do you need so many levels of shell scripts anyway?

Code:
# run subscript, exit if it fails
subscript || exit $?

I am not trying to exit from parent shell. But I want to come back to main parent shell, i.e. from n th level to 1st level.
# 4  
Old 09-25-2008
In each of the subshells, exit with a defined value:

(
# In subshell 5
........
........
exit 225 # exit with a defined value when you want to exit out of the program.
)

# In subshell4, check this exit value in the statement immediately where subshell5 ends.

[ "$?" -eq 225 ] && exit 224 # Similarly, subshell3 can check for this value and exit

And so on... till you reach your parent shell
# 5  
Old 09-26-2008
By "parent", I mean the n-1 level shell, for any n
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to exit from the parent script while the child is running?

hi, i want to call a child shell script from a parent shell script. the child will be running for 5 mins. normally when the child is running, parent will wait till the child completes. so in the above case parent will be paused for 5 mins. is there a way so that the parents does not wait for the... (3 Replies)
Discussion started by: Little
3 Replies

2. Shell Programming and Scripting

Kill child processes when exit

Hi, I have parent script which is invoking multiple child scripts. I would want to kill all the child processes before the parent process exit. > cat ./parent #!/bin/ksh while do . ./child arg1 & if ; then break fi done Is there a way to get the process group id for all the child... (3 Replies)
Discussion started by: midhun19
3 Replies

3. Shell Programming and Scripting

How to capture exit code of child script and send it to parent script?

#!/usr/local/bin/bash set -vx /prod/HotelierLinks/palaceLink/bin/PalacefilesWait /prod/HotelierLinks/palaceLink/bin/prodEnvSetup 03212013 & if then echo "fatal error: Palace/HardRock failed!!!!" 1>&2 echo "Palace Failed" | mail -s "Link Failed at Palace/HardRock" -c... (1 Reply)
Discussion started by: aroragaurav.84
1 Replies

4. Shell Programming and Scripting

Send correct exit code from child script back to parent

Hello all; hope someone can help me cause I am going crazy trying to find a solution for (what I think is simple) issue...looked hard up and down this forum and tried several "solutions" with no avail...so here's my issue: I have this (parent) script: copylsofdcmcadefttosftpwithmove.sh ... (3 Replies)
Discussion started by: gvolpini
3 Replies

5. Shell Programming and Scripting

Track Child process exit

hi, I have a job that spawns multiple child processes in background.. Catch is i want to wait for some jobs to finish before i spawn more background processes. (each job creates a file and deletes at the end of it . so i don't want start new jobs after x amount of disk size is used up) now,... (2 Replies)
Discussion started by: ak_saravanan
2 Replies

6. Programming

Why does my child process not exit?

Im sure it has something to do with the wait() call, but everything ive tried either leaves me with a zombie or with the exec executing indefinitely. switch(pid = fork()) { case -1:perror("fork failed"); exit(1); case 0: if(key == "cd") { execl("/bin/cd", "cd",... (2 Replies)
Discussion started by: p00ndawg
2 Replies

7. Shell Programming and Scripting

Getting exit status of child in trap handler

Hi, I have a trap problem when calling a child script in the background. I know there are a lot of threads here on the issue of traps and signals, I think I have read all the relevant ones, but still haven't found an answer to my problem. I'm working on Linux or HP, the script as you can see... (4 Replies)
Discussion started by: rimon
4 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
Login or Register to Ask a Question