2

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:

#!/bin/sh

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.**
then 
echo "Some error"
fi

test.awk file:

BEGIN {
   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)

  • if ($recval == '$2') **//this condition statement doesn't seems to work.** so by stating '$2' your trying to test the value the second parameter to your bash script? Looks to me more like your testing for a litteral '$'+'2' string. how about if [ $recval == '2' ]; then echo "error"; fi, would that be closer to what your attempting? – Dwight Spencer Mar 30 '18 at 19:26
4

This should work:

#!/bin/sh

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

re=$?
echo "$re"

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

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.

  • Thank you all...it worked..thanks thanks..once again to all.. :) – user68039 May 22 '14 at 6:04
1

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

recval=$?

case $recval in
    0) echo "Exit code 0"
       ;;
    1) echo "Exit code 1"
       ;;
    2) echo "Exit code 2"
       ;;
esac
1

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:

#!/bin/sh
awk -v CONFIG_SOURCE=Testfile1.txt -f test.awk
awk_status=$?
if [ "$awk_status" -eq 2 ]; then
  echo 1>&2 "Additional information regarding the error message from awk just above"
  exit $awk_status
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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