Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get value of "$?", determine if program terminated with error.

Status
Not open for further replies.

vittopaparazzi

Programmer
Feb 14, 2005
2
CA
I'm using AWK to parse the output of an executable program written in C. In some circumstances, the C program will terminate with an error and return a non-zero value. In regular shell scripting you could always check the value of the $? variable to see if it was non-zero, and then branch accordingly. How can I read this value in AWK?

The following is pseudo-code which will give you an idea of what I'm trying to do.

SHELL SCRIPT:
Code:
#!/bin/sh
my_c_program | awk -f MY_AWK_PROGRAM.AWK

MY_AWK_PROGRAM.AWK

Code:
BEGIN {
  if ([b]$?[/b]=="1") {print "ERROR in my_c_program"; exit(1);}
}

{# Parse the output from my_c_program normally}

END {
  print "Everything went smoothly.";
  exit(0);
}

Of course, AWK does not support the $? variable! So I'm not sure how to do this. I've tried some tricks like:

Code:
#!/bin/sh
my_c_program | awk -v return_val=$? -f MY_AWK_PROGRAM.AWK

But the value of return_val is set BEFORE my_c_program finishes, so it is always 0. I even tried:

Code:
#!/bin/sh
my_c_program | awk -f MY_AWK_PROGRAM.AWK return_val=$?

But that doesn't seem to work either. Any help would be greatly appreciated.







 
You may try something like this:
tmp=/tmp/mytmp.$$
my_c_program >$tmp && awk -f MY_AWK_PROGRAM.AWK $tmp || echo "ERROR in my_c_program or in MY_AWK_PROGRAM.AWK"
rm -f $tmp

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
well... cannot do this in awk. By the time you get to awk it's already too late AND you [most likely] already had some in the 'pipe' for awk to process.

how 'bout this paradigm apart from fixing the REAL problem:
Code:
#!/bin/ksh
my_c_program > /tmp/output.$$
(( ${?} = 0 )) &&  awk -f MY_AWK_PROGRAM.AWK /tmp/output.$$ || print -u2 "my_c_program failed"

rm -rf /tmp/output.$$

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Both of your solutions save the output of the C program to disk before processing with the AWK script. I wish this was an option, but the AWK script is running on a small, embedded device with extremely limited DOC (Disk On Chip) memory.

In fact, I didn't mention this before, but the main reason why the C program would fail would be because it ran out of disk space, which would obviously proclude saving the error results to disk!

Sorry, but I need to pipe the results in.

In my Pseudo Code I wanted to perform the error check in the "BEGIN" section of the AWK script, but I suppose I could alter the script so that the error check was performed in the "END" section, after some of the data had already been read from the pipe. Does this make things any easier?

 
Can't you C program write a trailer record when all his processing is successfully done. Your awk program should then choke if this record is not found.

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
pseudo code to catch prelimitary exits that result in not having any in/output

Code:
echo -n | awk 'BEGIN {
  if ((getline) < 1) { print "no input?"; exit 1 }
  else first = $0
}
first { tmp = $0; $0 = first; _process_; $0 = tmp; first = "" }
{ _process_ }

or moving _process_ in the else in BEGIN.. without any such thing the first line gets lost.

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top