Exit Codes

  • Follow


I'm having a little trouble with exit codes.  For instance, cmp returns 0 
if the two files are the same, 1 if they're different, and >1 if there's 
an error.  And the -s option only returns the exit code.

But where is the exit code?  If I try, for instance,

  cmp a2 a2

I get nothing.  If I try

  a=`cmp a2 a2`

I get a blank variable.  How do I get the 0, 1, or >1 so I can do 
something with it?

-- 
"'No user-serviceable parts inside.'  I'll be the judge of that!"
0
Reply glhansen (396) 12/6/2003 10:12:41 PM

Gregory L. Hansen <glhansen@steel.ucs.indiana.edu> wrote:

> I'm having a little trouble with exit codes.  For instance, cmp returns 0 
> if the two files are the same, 1 if they're different, and >1 if there's 
> an error.  And the -s option only returns the exit code.

> But where is the exit code?  If I try, for instance,

>   cmp a2 a2

> I get nothing.  If I try

>   a=`cmp a2 a2`

> I get a blank variable.  How do I get the 0, 1, or >1 so I can do 
> something with it?

When you use

a=`cmp a2 a2`

you get the output of the command stored in a. To get the return code
check the variable '$?' immediately after you executed the command,
i.e.

> a=`diff a2 a2`
> echo $?
0
> a=`diff a1 a2`
> echo $?
1
> a=`diff a a1`
cmp: a: No such file or directory
> echo $?
2
                                     Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.physik.fu-berlin.de/~toerring
0
Reply Jens.Toerring (807) 12/6/2003 10:32:52 PM


In article <bqtlek$25rjjj$2@uni-berlin.de>,
 <Jens.Toerring@physik.fu-berlin.de> wrote:
>Gregory L. Hansen <glhansen@steel.ucs.indiana.edu> wrote:
>
>> I'm having a little trouble with exit codes.  For instance, cmp returns 0 
>> if the two files are the same, 1 if they're different, and >1 if there's 
>> an error.  And the -s option only returns the exit code.
>
>> But where is the exit code?  If I try, for instance,
>
>>   cmp a2 a2
>
>> I get nothing.  If I try
>
>>   a=`cmp a2 a2`
>
>> I get a blank variable.  How do I get the 0, 1, or >1 so I can do 
>> something with it?
>
>When you use
>
>a=`cmp a2 a2`
>
>you get the output of the command stored in a. To get the return code
>check the variable '$?' immediately after you executed the command,
>i.e.
>
>> a=`diff a2 a2`
>> echo $?
>0
>> a=`diff a1 a2`
>> echo $?
>1
>> a=`diff a a1`
>cmp: a: No such file or directory
>> echo $?
>2

Ah, thank you!  I've seen a few introductions to the Unix command line and 
scripting, but I don't think I've ever come across that peice of 
information.  I just checked my main book, Inside Unix 2nd ed. to see if 
I'd seen it and forgot, but I couldn't find it.  That's kind of 
frustrating when the book says an error code is returned, but doesn't say 
how to get it.

-- 
"Let us learn to dream, gentlemen, then perhaps we shall find the
truth... But let us beware of publishing our dreams before they have been
put to the proof by the waking understanding." -- Friedrich August Kekul�
0
Reply glhansen (396) 12/6/2003 10:57:53 PM

Gregory L. Hansen <glhansen@steel.ucs.indiana.edu> wrote:
> In article <bqtlek$25rjjj$2@uni-berlin.de>,
>  <Jens.Toerring@physik.fu-berlin.de> wrote:
>>> a=`diff a2 a2`
>>> echo $?
>>0
>>> a=`diff a1 a2`
>>> echo $?
>>1
>>> a=`diff a a1`
>>cmp: a: No such file or directory
>>> echo $?
>>2

> Ah, thank you!  I've seen a few introductions to the Unix command line and 
> scripting, but I don't think I've ever come across that peice of 
> information.  I just checked my main book, Inside Unix 2nd ed. to see if 
> I'd seen it and forgot, but I couldn't find it.  That's kind of 
> frustrating when the book says an error code is returned, but doesn't say 
> how to get it.

There are a few more of these, at least for the Bourne (sh), bash and
Korn shell, that sometimes are useful. Straight from "UNIX in a Nutshell"
(O'Reilly):

$#    Number of command-line arguments
$-    Options currently in effect
$?    Exit value of last command
$$    Process number of current process
$!    Process number of last background command
$0    First word; that is, command name
$n    Individual arguments on command line (positional parameters).
      Bourne (sh) shell allows only nine parameters to be referenced
      (n=1-9), other shells have methods to access more
$*    All arguments on the command line ("$1 $2...")
"$@"  All arguments on the command line, individually quoted ("$1" "$2"...)

                                     Regards, Jens
-- 
  \   Jens Thoms Toerring  ___  Jens.Toerring@physik.fu-berlin.de
   \__________________________  http://www.physik.fu-berlin.de/~toerring
0
Reply Jens.Toerring (807) 12/7/2003 3:24:17 PM

On 7 Dec 2003 15:24:17 GMT
Jens.Toerring@physik.fu-berlin.de wrote:

> There are a few more of these, at least for the Bourne (sh), bash and
> Korn shell, that sometimes are useful. Straight from "UNIX in a
> Nutshell"(O'Reilly):

> $n    Individual arguments on command line (positional parameters).
Yes, but there are ways around this. BASH allows more than 9: e.g. ${20}
On the Bourne shell you can use the shift command to cycle through or
the for command.

-- 
Jerry Feldman <gaf-nospam-at-blu.org>
Boston Linux and Unix user group
http://www.blu.org PGP key id:C5061EA9
PGP Key fingerprint:053C 73EC 3AC1 5C44 3E14 9245 FB00 3ED5 C506 1EA9
0
Reply gaf-noSPAM (33) 12/7/2003 3:36:29 PM

[This followup was posted to comp.unix.questions]

In article <bqtk8p$2h2$1@hood.uits.indiana.edu>, 
glhansen@steel.ucs.indiana.edu says...
> 
> I'm having a little trouble with exit codes.  For instance, cmp returns 0 
> if the two files are the same, 1 if they're different, and >1 if there's 
> an error.  And the -s option only returns the exit code.
> 
> But where is the exit code?  If I try, for instance,
> 
>   cmp a2 a2
> 
> I get nothing.  If I try
> 
>   a=`cmp a2 a2`
> 
> I get a blank variable.  How do I get the 0, 1, or >1 so I can do 
> something with it?

In most shells (if not all shells) after your run a command the exit 
status is available in the special shell variable "$?" as in :

       cmp -s a2 a2
       echo "cmp exit status is $?"

But be warned : EVERY command you run resets this variable.
0
Reply raisin3 (34) 12/12/2003 2:11:04 PM

5 Replies
27 Views

(page loaded in 0.092 seconds)

2/19/2013 7:25:04 PM


Reply: