Sign up ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It's 100% free, no registration required.

I want to read the error codes or the exit status of awk script into shell to use in if condition to compare for error handling my shell script looks something like:

My script:


awk -v CONFIG_SOURCE=Testfile1.txt -f test.awk

echo $?  # prints the error code '2' in this case, if the file is actually not available
recval =  $?

if ($recval == '$2') **//this condition statement doesn't seems to work.**
echo "Some error"

test.awk file:

   ignore_line = 0;
   if ( CONFIG_SOURCE == "" )
      print "Error: Invalid embed source file.";
      exit 1
   if ( 0 != system( "[ -f " CONFIG_SOURCE " ] " ) )
      print "Error: Embed source file '" CONFIG_SOURCE "' does not exist."
      exit 2

So, if the file is not available, it will return exit status '2',which i can echo but,how to copy the value '2' or the exit status to some variable(recval) in shell)

share|improve this question

3 Answers 3

This should work:


awk -v CONFIG_SOURCE=Testfile1.txt -f test.awk

echo "$re"

if [ "$re" -eq 2 ]; then
  echo >&2 "Some error"

The problem in your script was the usage of echo $? before recval = $?. With echo $? you printed the value of $? (the exit status of the last statement), but then $? was overwritten from echo $?. The exit status of the last statement was then the exit status of echo $?, with was probably 0.

share|improve this answer
Thank you worked..thanks thanks..once again to all.. :) – user68039 May 22 '14 at 6:04

The $? contains the value of the exit code of the very last command executed. You can stack up exit codes in a case statement as well.

#!/usr/bin/env bash

awk -v CONFIG_SOURCE=Testfile1.txt -f test.awk &> /dev/null


case $recval in
    0) echo "Exit code 0"
    1) echo "Exit code 1"
    2) echo "Exit code 2"
share|improve this answer

Your basic logic is correct but your shell syntax is wrong.

  • Assignments must be a single word, you can't put spaces around the equal sign: recval = $? runs the command recval with two arguments, = and the value of $?.
  • After the command $?, the value of $? is for the echo command; saving $? to another variable must be the first thing you do. To see what a shell script is doing, make it print a trace: add set -x at the top, just below the #! line.
  • '$2' is a two-character string. For the number 2, write 2. But to test if there was an error, test if the status is nonzero: 2 is only one of the possible values.
  • There are several ways to compare integers but what you wrote isn't one.
    • [ "$x" -eq "$y" ] (portable)
    • [[ $x -eq $y ]] (in ksh, bash, zsh only, but not in /bin/sh)
    • ((x == y)) (in ksh93, bash, zsh only, but not in /bin/sh)
  • Error messages go to standard error (file descriptor 2), not standard output.
  • If awk fails, then your script should probably return a nonzero status as well.

Here's a corrected script:

awk -v CONFIG_SOURCE=Testfile1.txt -f test.awk
if [ "$awk_status" -eq 2 ]; then
  echo 1>&2 "Additional information regarding the error message from awk just above"
  exit $awk_status
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.