Sponsored Content
Full Discussion: All about exit code
Top Forums Shell Programming and Scripting All about exit code Post 17031 by Perderabo on Sunday 10th of March 2002 01:31:26 PM
Old 03-10-2002
I just read that intro page. It's rather poorly written. But an accurate answer is going to take a while.

From the standpoint of a C programmer who is using the system call interface, when a program invokes exit() or _exit(), the argument to exit is anded with 0377 and the result is the exit code for the process. If a process dies as the result of a default action of a signal it will get an exit code which is non-zero but which has non of the lower 8 bits set. You're supposed to use the macros which are mentioned on wait(2) man page to portably determine the signal number. It is one or the other. Sending a signal to a process which has started the kernel portion of the exit() system call has no effect. All 8 bits are available to the programmer resulting in values 0 through 255 being legal.

And only a process that is running can invoke the exit() call. If a C program tries to run a non-existant program or one without the proper execute bits set, the exec() system call itself will fail and the program will never run.

You are asking this question in the shell programming forum, though. A programmer who writes a shell sees the above interface. But a programmer who uses a shell is at the mercy of the shell's designer.

If you type "./perderabo" you will get an error message telling you that the shell can't find the file perderabo. But most shells will also set the exit code to some non-zero number. Nothing ran, the shell either noticed that the file wasn't there via stat() or it attempted the exec() which failed. But it will set $? (or $status) anyway. This is kinda useful I guess.

The New Kornshell Command and Programming Language by Morris Bolsky and David Korn says:
Quote:
0 Normal exit.
1-125 Failure.
126 A command was found but cannot be executed.
127 A command could not be found.
128-255 Failure.
256 and above A command has exited because of reciept of a signal.
Version: With the 11/16/88 version of ksh, 129-160 indicated that a command had exited because of a signal....A command that could not be found or could not execute had a return value of 1.
So exit codes used by the shell for processes killed by signals are not fixed even within different versions of ksh, let alone across all shells.

What I do is use "exit 0" for success and "exit n" for failure where n is a small integer. I rarely go above 10 and have never reached 20. I rely on shells to be able to determine the difference between a zero exit status and a non-zero exit status. But I almost never test for different non-zero values. I will display a non-zero exit code where it can be seen by a human who can (perhaps) use it to understand what is happening.
This User Gave Thanks to Perderabo For This Post:
 

10 More Discussions You Might Find Interesting

1. Programming

Exit Code in HP-UX KSH.

In one of my programs another process is called using the system command e.g. lv_error = system("myproc"); where lv_error is declared as an int. myproc would be returning 0 for success and 1 for failure. e.g. if (success) { return(0); }else{ return(1); } When the return code... (3 Replies)
Discussion started by: mbb
3 Replies

2. UNIX for Dummies Questions & Answers

Where can I find a list of exit codes? (Exit code 64)

I'm receiving an exit code 64 in our batch scheduler (BMC product control-m) executing a PERL script on UX-HP. Can you tell me where I can find a list of exit codes and their meaning. I'm assuming the exit code is from the Unix operating system not PERL. (3 Replies)
Discussion started by: jkuchar747
3 Replies

3. Shell Programming and Scripting

where can I get exit code meanings?

I'm investigating strange behaviour on two boxes (Sun OS 5.10 and AIX 5.1) in ksh have used $? to get exit codes returned:- 137 and 34 where can I find what these mean? thank you (1 Reply)
Discussion started by: speedieB
1 Replies

4. Shell Programming and Scripting

problem with exit code when piping

i am writing a script to perform some mysqldumps and gzip them. The problem I am running into is that if the user specifies a database that doesn't exist, the error the mysql engine produces is still piped into gzip, and the exit code returned is 0. If I don't pipe into gzip, an exit code... (4 Replies)
Discussion started by: bitoffish
4 Replies

5. Filesystems, Disks and Memory

Exit code 137 on a backup

Can some one tell me what it means to get a exit code od 137 from a cron scheduled backup on HP-UX. Also if you know of a book that has the HP-UX codes that would be great. Thanks (4 Replies)
Discussion started by: twins
4 Replies

6. UNIX for Dummies Questions & Answers

How to capture exit code for a bg job

If I execute a job in background (in ksh or bash), how would I capture the exit code for that job? Thanks, - CB (1 Reply)
Discussion started by: ChicagoBlues
1 Replies

7. Shell Programming and Scripting

Setting script exit code

#!/bin/ksh row=`sed '1!G;h;$!d' file1.xml | head -2| tail -1` echo "$row" | awk -F"" '{$esum=$5}' row=`sed '1!G;h;$!d' file2.xml | head -2| tail -1` echo "$row" | awk -F"" '{$isum=$5+$19}' echo "Exp:$esnum" echo "Imp:$isum" if then echo "Matched" else echo "Not matched" fi ... (6 Replies)
Discussion started by: skyineyes
6 Replies

8. UNIX for Dummies Questions & Answers

UNIX exit code 11

We have a batch Unix process that runs during the day and it is getting an exit code 11 from Unix. It finishes a sqlplus step and gets the exit code 11 before it starts the next step. This used to happen once a year and now is happening more often (but not every time the process runs). We have... (2 Replies)
Discussion started by: msol
2 Replies

9. Shell Programming and Scripting

Exit code

can anyone tell what the exit status - 137 belongs in unix shell scripting. (3 Replies)
Discussion started by: ramkumar15
3 Replies

10. Shell Programming and Scripting

Understanding exit code status

I have a file with size as 120,371 bytes in local machine and trying to FTP it to a remote server using put command. say that it transferred exactly the same size of 120,371 bytes and hence it returns a 226 code. I have a doubt on how the rule is made for 226 - if both size on local and remote... (7 Replies)
Discussion started by: penqueen
7 Replies
exit(1)                                                            User Commands                                                           exit(1)

NAME
exit, return, goto - shell built-in functions to enable the execution of the shell to advance beyond its sequence of steps SYNOPSIS
sh exit [n] return [n] csh exit [ ( expr )] goto label ksh *exit [n] *return [n] DESCRIPTION
sh exit will cause the calling shell or shell script to exit with the exit status specified by n. If n is omitted the exit status is that of the last command executed (an EOF will also cause the shell to exit.) return causes a function to exit with the return value specified by n. If n is omitted, the return status is that of the last command exe- cuted. csh exit will cause the calling shell or shell script to exit, either with the value of the status variable or with the value specified by the expression expr. The goto built-in uses a specified label as a search string amongst commands. The shell rewinds its input as much as possible and searches for a line of the form label: possibly preceded by space or tab characters. Execution continues after the indicated line. It is an error to jump to a label that occurs between a while or for built-in command and its corresponding end. ksh exit will cause the calling shell or shell script to exit with the exit status specified by n. The value will be the least significant 8 bits of the specified status. If n is omitted then the exit status is that of the last command executed. When exit occurs when executing a trap, the last command refers to the command that executed before the trap was invoked. An end-of-file will also cause the shell to exit except for a shell which has the ignoreeof option (See set below) turned on. return causes a shell function or '.' script to return to the invoking script with the return status specified by n. The value will be the least significant 8 bits of the specified status. If n is omitted then the return status is that of the last command executed. If return is invoked while not in a function or a '.' script, then it is the same as an exit. On this man page, ksh(1) commands that are preceded by one or two * (asterisks) are treated specially in the following ways: 1. Variable assignment lists preceding the command remain in effect when the command completes. 2. I/O redirections are processed after variable assignments. 3. Errors cause a script that contains them to abort. 4. Words, following a command preceded by ** that are in the format of a variable assignment, are expanded with the same rules as a vari- able assignment. This means that tilde substitution is performed after the = sign and word splitting and file name generation are not performed. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | |Availability |SUNWcsu | +-----------------------------+-----------------------------+ SEE ALSO
break(1), csh(1), ksh(1), sh(1), attributes(5) SunOS 5.10 15 Apr 1994 exit(1)
All times are GMT -4. The time now is 10:57 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy