vittopaparazzi
Programmer
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:
MY_AWK_PROGRAM.AWK
Of course, AWK does not support the $? variable! So I'm not sure how to do this. I've tried some tricks like:
But the value of return_val is set BEFORE my_c_program finishes, so it is always 0. I even tried:
But that doesn't seem to work either. Any help would be greatly appreciated.
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.