[comp.unix.questions] Exit Value from Awk

mills@ccu.umanitoba.ca (Gary Mills) (07/18/90)

Does anyone know how to set the exit code from awk within an awk
script?  My AT&T and Sun manuals are silent on this.  I want to
be able to do: `awk -f file file || cat file'.
-- 
-Gary Mills-             -University of Manitoba-             -Winnipeg-

stevens@hsi.UUCP (Richard Stevens) (07/18/90)

In article <1990Jul17.203037.22283@ccu.umanitoba.ca> mills@ccu.umanitoba.ca (Gary Mills) writes:
>
>Does anyone know how to set the exit code from awk within an awk
>script?

Just say  "exit <expression>".  I just tried it and it works under
V7 awk, SVR3.1 awk, nawk, and gawk.

	Richard Stevens
	3M Health Information Systems, Wallingford, CT
	   stevens@hsi.com

steinbac@hpl-opus.HP.COM (Gunter Steinbach) (07/18/90)

On p.  50 of the awk bible "The AWK programming Language", by A., W.,
and K. themselves, it says that the statement "exit <expression>" will
return <expression> as the program's exit value.  I tried it with things
like
awk 'END {exit 1}' < /dev/null || print "ok"

and it works.  I highly recommend the book, by the way.  Addison-Wesley
is the publisher.

(This was "awk" on HP-UX 6.5 on an HP9000/350 work station.)

Good luck -

	 Guenter Steinbach		gunter_steinbach@hplabs.hp.com

bob@wyse.wyse.com (Bob McGowen x4312 dept208) (07/18/90)

In article <1990Jul17.203037.22283@ccu.umanitoba.ca> mills@ccu.umanitoba.ca (Gary Mills) writes:
>
>Does anyone know how to set the exit code from awk within an awk
>script?  My AT&T and Sun manuals are silent on this.  I want to
>be able to do: `awk -f file file || cat file'.
>-- 
>-Gary Mills-             -University of Manitoba-             -Winnipeg-

Use the `exit' function:

   awk '{  # your code
	   if (some_condition)
	      exit 0
	   else
	      exit 1
	}' ...

Cheers- :-)

Bob McGowan  (standard disclaimer, these are my own ...)
Product Support, Wyse Technology, San Jose, CA
..!uunet!wyse!bob
bob@wyse.com

arnold@audiofax.com (Arnold Robbins) (07/18/90)

In article <1990Jul17.203037.22283@ccu.umanitoba.ca> mills@ccu.umanitoba.ca (Gary Mills) writes:
>Does anyone know how to set the exit code from awk within an awk
>script?

Yes.  Simply say

	exit <expression>

in the action part of a rule.  The value of <expression> will be the
exit value of awk itself.  The expression is optional, in which case awk
will exit with 0.  This works on all variants of awk.
-- 
Arnold Robbins				AudioFAX, Inc. | Laundry increases
2000 Powers Ferry Road, #220 / Marietta, GA. 30067     | exponentially in the
INTERNET: arnold@audiofax.com	Phone: +1 404 933 7600 | number of children.
UUCP:	  emory!audfax!arnold	Fax:   +1 404 933 7606 |   -- Miriam Robbins

andyb@coat.com (Andy Behrens) (07/18/90)

stevens@hsi.UUCP (Richard Stevens) writes:
> Gary Mills writes:
>>
>> Does anyone know how to set the exit code from awk within an awk
>> script?
>
> Just say  "exit <expression>".  I just tried it and it works under
> V7 awk, SVR3.1 awk, nawk, and gawk.

That's correct.  

You should be aware, though, that if you have an END{ } block, awk will
execute it when it processes the "exit <expression>" statement.  When
the END{ } block is completed, awk will terminate with the exit code
specified in the expression.

Andy 

guy@auspex.auspex.com (Guy Harris) (07/19/90)

>Does anyone know how to set the exit code from awk within an awk
>script?

Set the exit code without exiting, no.  Exit with a particular exit
code, yes:

	$ awk 'END { exit 17 }' </dev/null
	$ echo $?
	17

If this isn't documented, it should be....

nreadwin@miclon.uucp (Neil Readwin) (07/20/90)

In article <1990Jul17.203037.22283@ccu.umanitoba.ca> mills@ccu.umanitoba.ca (Gary Mills) writes:
>Does anyone know how to set the exit code from awk within an awk
>script?  My AT&T and Sun manuals are silent on this.  I want to
>be able to do: `awk -f file file || cat file'.

According to an old posting from jwp%chem@sdcsvax.ucsd.edu

# The user manual says that using the exit function causes the script to
# behave as if end-of-input has been reached.  Not mentioned explicitly 
# is the fact that this will cause the END block to be executed if it 
# exists.  Also, two things are omitted:
#
#   exit(expr) causes the script's exit status to be set to the value of expr.
# 
#   If exit is called within the END block, the script exits immediately.
-- 
 Disclaimer: 818  Phone: +44 71 528 8282  E-mail: nreadwin@micrognosis.co.uk