|
Home > Archive > Unix Shell > October 2006 > User-defined exit values
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
User-defined exit values
|
|
|
| Hi,
As I know that there are some exit values are reserved by the Unix, say
1 to 125 are exit values of command exited with failure, is there a
range of exit value which are not used by Unix or can I define the exit
value?
Yam.
| |
| Chris F.A. Johnson 2006-10-20, 7:23 pm |
| On 2006-10-20, Yam wrote:
> Hi,
>
> As I know that there are some exit values are reserved by the Unix, say
> 1 to 125 are exit values of command exited with failure, is there a
> range of exit value which are not used by Unix or can I define the exit
> value?
You can use any value you like, from 0 to 255.
--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
| |
| Janis Papanagnou 2006-10-20, 7:23 pm |
| Yam wrote:
> Hi,
>
> As I know that there are some exit values are reserved by the Unix, say
> 1 to 125 are exit values of command exited with failure, is there a
> range of exit value which are not used by Unix or can I define the exit
> value?
You can define any value in the range 0 to 255. But if you want the exit
code distinguished from exits triggered by signals it depends on the shell.
With ksh88 you have the ranges that you mentioned above, and the exit codes
from 129 to 160 represent exits by the signals 1 to 32 respectively.
With ksh93 you have the signal exit codes shifted to values above 256, thus
extending the exit code range usable by your scripts by the interval 128
to 255. Exit code 1 (ksh88) and exit codes 126 and 127 (ksh93) respectively
have also predefined meanings (command not found, command not executable).
Janis
>
> Yam.
>
| |
|
|
Chris F.A. Johnson wrote:
> On 2006-10-20, Yam wrote:
> You can use any value you like, from 0 to 255.
0..127 for ksh88, and 0..255 for ksh93.
http://kornshell.com/doc/faq.html # See II.Q11
=Brian
| |
|
|
Janis Papanagnou wrote:
> Yam wrote:
>
> You can define any value in the range 0 to 255. But if you want the exit
> code distinguished from exits triggered by signals it depends on the shell.
> With ksh88 you have the ranges that you mentioned above, and the exit codes
> from 129 to 160 represent exits by the signals 1 to 32 respectively.
> With ksh93 you have the signal exit codes shifted to values above 256, thus
> extending the exit code range usable by your scripts by the interval 128
> to 255. Exit code 1 (ksh88) and exit codes 126 and 127 (ksh93) respectively
> have also predefined meanings (command not found, command not executable).
>
> Janis
>
Hi,
What I mean is the I have a script that returns 1 if the input file is
of zero-length. However, I know that return code 1 has already
reserved by unix, say changing to an invalid directory may also return
1. So, I want to change my script's return code 1 to another value so
as to distinguish from unix, which value should I change to? According
to what you mentioned, can I change to any value between 128 to 255?
In addition, how do I know which ksh (ksh93 or ksh88) I'm using?
Yam:o
| |
| Janis Papanagnou 2006-10-22, 1:16 pm |
| Yam wrote:
> Janis Papanagnou wrote:
>
>
>
> Hi,
>
> What I mean is the I have a script that returns 1 if the input file is
> of zero-length. However, I know that return code 1 has already
> reserved by unix, say changing to an invalid directory may also return
> 1. So, I want to change my script's return code 1 to another value so
> as to distinguish from unix, which value should I change to? According
> to what you mentioned, can I change to any value between 128 to 255?
You can also use the values in the lower range if you like. There's
nothing "reserved by unix" with the exception that a vitally important
convention is to use 0 for success and other values for failure (with
signals as a special case mapped to exit codes as described above).
The Unix _libraries_ have some standard return values, "errno"; these
values are _not_ mapped to the return/exit code of the Unix tools, so
don't confuse them.
Each Unix tool has its own set of exit codes.
For example, grep will give you an exit code of 0 on success (match),
or 1 if there's no match, and 2 if some error occurred (e.g. if no file
was found). A ksh93 shell would give you a return code of 127 in case a
command was not found. Moving files to a directory without permissions
would also give you an exit code 1 from the mv command.
Keep in mind that, if you don't catch and return exit status to the
caller, your script will return the exit status of your last command
executed; so better always define well defined exit codes. You may
define the values for the codes as appropriate for your application.
>
> In addition, how do I know which ksh (ksh93 or ksh88) I'm using?
Maybe you are not even using a ksh; the default shell depends on the
Unix system you are using (and what the sysadmins may have changed).
If you are using ksh you'll know the version if you type set -o vi
and then hit the keys <Esc> and <Ctrl-V>.
Janis
>
> Yam:o
>
| |
| Barry Margolin 2006-10-22, 7:19 pm |
| In article <1161497482.743187.270750@b28g2000cwb.googlegroups.com>,
"Yam" <yamyam2501@yahoo.com.hk> wrote:
> What I mean is the I have a script that returns 1 if the input file is
> of zero-length. However, I know that return code 1 has already
> reserved by unix, say changing to an invalid directory may also return
> 1.
The return code 1 is *not* reserved. The only return code that has any
special conventional meaning is 0, wich means "success". All non-zero
values imply some form of failure. If different non-zero values have
different meanings, that's up to the particular application (for
instance, grep uses 1 to indicate that there were no matching lines, and
2 to indicate that an error occurred). Programs that don't need to
distinguish different types of failures *tend* to use 1 as a failure
value (I think this is the usual value of the EXIT_FAILURE macro in
<stdlib.h> ).
--
Barry Margolin, barmar@alum.mit.edu
Arlington, MA
*** PLEASE post questions in newsgroups, not directly to me ***
*** PLEASE don't copy me on replies, I'll read them in the group ***
| |
|
| Barry Margolin wrote:
> "Yam" <yamyam2501@yahoo.com.hk> wrote:
> wich means "success".
This is mostly true.
OGBS6 / IEEE Std 1003.1-2004 section 2.8.2, "Exit Status for
Commands", states (although admittedly this does not affect
shell scripting directly) that "If a command is not found, the exit
status shall be 127" and that the number shall conform to the
output of wait(2) and waitpid(3). The information that I remember,
and alas cannot now locate, is the recommendation that 1 be
reserved for general failure, and 2 be reserved for options parsing
errors.
Of course, the many examples of the wide variation of error
returns above show tshe fact that such programs were designed
before the above standardizations were extant.
=Brian
|
|
|
|
|