Processes and exit status


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Processes and exit status
# 1  
Old 07-07-2011
Processes and exit status

Hi, I can't understand why the last $? is 1? can somebody plz help me to understand it? thanks
Code:
$ ksh
$ ps -f
     UID   PID  PPID  C    STIME TTY       TIME COMMAND
msarabad 12361 12319  0 15:17:58 pts/1     0:00 ksh
msarabad 12319 12317  0 15:15:11 pts/1     0:00 -sh
msarabad 12362 12361  0 15:18:04 pts/1     0:00 ps -f
$ exit
$ print $?
0
$ ksh
$ ps -f
     UID   PID  PPID  C    STIME TTY       TIME COMMAND
msarabad 12363 12319  0 15:18:16 pts/1     0:00 ksh
msarabad 12319 12317  0 15:15:11 pts/1     0:00 -sh
msarabad 12364 12363  0 15:18:19 pts/1     0:00 ps -f
$ exit 1
$ print $?
1
$

# 2  
Old 07-07-2011
By POSIX standards for the shell

exit returns 0 to the caller

exit n returns n to the caller where n is from 0 to 255.
# 3  
Old 07-07-2011
Ok, I got it up to this point. it means that even exit 89 will tell me 89. but why do we need to do this? how is it related to the fork processes? I got this exercise while I'm doing the exercises in shell POSIX tutorial in child/parent part.
# 4  
Old 07-07-2011
The idea is for you as a programmer to have a way of knowing when running your program how it ended (0 being its OK...) the program can very well be a shell script...
Lets say in your script you have a if condition where in one case you do not want the process to continue, you would give an exit value...
If you had multiple conditions, you would be pleased to know where you got out...
# 5  
Old 07-07-2011
thanks I got it. correct me..

the below example when I say exit, it will exit and the status is 0 but when I say exit 1 it also exits and the status is 1, so its a small sample of what you just said right? because all the processes before the exit are same! it is just showing that exit can have different numbers (0 - 255) ? am I right?
# 6  
Old 07-07-2011
Because a the termination status is NOT the same thing as exit which is a command here...
print $? will print the status of last command/program etc...
If you have placed in your instructions some exits, you can find out where you came out of your program because executing the exit will end the program! the return code will tell you why...
# 7  
Old 07-07-2011
Yes, don't just exit 1 at every error condition. Exit with a unique number for each possible error so you (or another script) can query the return code to see why it failed.

Make sure to comment that at the top:
Code:
##  Returns: 0-Success
##           1-Invalid number of args
##           2-File not found

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Want to get the exit status

Hi All, I am trying to create a zip file with all the txt files(these are in large number) in the current directory. I am able to do this operation sucessfully. After this i want to get the status of the tar command executed and do accordingly. When i am trying with the below code, the status... (3 Replies)
Discussion started by: paddu
3 Replies

2. Shell Programming and Scripting

exit status from the script is always 0

Hi , I have a bash script , which does the network configuration. Messages from this script are dumped on console as well as stored in a log file . This script is invoked from a C code using system call . The script returns different exit code , to indicate different error cases. The... (1 Reply)
Discussion started by: abhirai
1 Replies

3. Shell Programming and Scripting

Exit Status

I have a shell script (#!/bin/sh) that interacts with Appworx and Banner Admin. In my script I want to check the exit status of awrun before continuing. awrun can run for 10 seconds or it can run for over a minute. So my question is, will it go through my if statement before awrun may even be... (2 Replies)
Discussion started by: smkremer
2 Replies

4. Shell Programming and Scripting

Executing multiple processes without waiting for their exit status.

Hello Friends, Hope you are doing well. I just need a help in executing multiple processes. I've written a shell script which calls another scritps. But the problem is there are too many processes to run, and each process takes about a min to finish its execution. So, I want to just... (3 Replies)
Discussion started by: singh.chandan18
3 Replies

5. Shell Programming and Scripting

Exit status

I'm preparing for exam and one of exams is to write own test command... I wonder if in unix is a command which just returns exit code you specify.. I know I can easily write a function like this: exStatus() { return $1 } -> my question is rather theoretical thank you! (9 Replies)
Discussion started by: MartyIX
9 Replies

6. Shell Programming and Scripting

How to get the exit status

Hi all, I'm running a program which return 1 upon success. But when encounters problem shell return 's '1' . How to differentiate between them the shell return value and script return value. Ex. function fn return '1' if executed successfully and '0' if failed. But when if shell encounters... (1 Reply)
Discussion started by: yhacks
1 Replies

7. UNIX for Dummies Questions & Answers

exit status conditions

Hi all, i am writing a script to test if some servers are down and prompt if test positive. i used rlogin and rsh then exit but the script when run, logs into the servers and stays. pls what can i do to salvage this? or what other options do you suggest? (6 Replies)
Discussion started by: sdcoms
6 Replies

8. Shell Programming and Scripting

retrieving exit status from background processes

Hi, I need to retrieve the exit status of 4 moves running as background processes. The wait command will not work since it can only give me the exit status of the last of the background processes. Here's a sample of what I need !#/bin/ksh mv /dir1/subdir1/*.Z /dir6/subdir6/ & mv... (2 Replies)
Discussion started by: MG537
2 Replies

9. Shell Programming and Scripting

exit status

i downloaded a text file from metalab.unc.edu called sh.txt and in this reference manual it refers to shell scripting exit status .. at the end of one of the examples that author gave an exit status of 127.. to what does a 127 exit status refer too and what is its purpose in the code. moxxx68 (1 Reply)
Discussion started by: moxxx68
1 Replies

10. UNIX for Advanced & Expert Users

ftp exit status.

Does ftp from unix have an exit status. In the sense after ftp is invoked and if the ftp fails during file transfer does it exit out with a status other than 0. What is do right now is invoke ftp and right it to a log and then grep for 'File Transferred Sucessfully'. Is this the only way to do it... (1 Reply)
Discussion started by: oracle8
1 Replies
Login or Register to Ask a Question