1

I need to check the return codes of two sub-scripts launched from within a wrapper script, using the Bash shell.

If either subscript fails they will produce a negative integer as a return code. If a script has minor errors it will produce a positive integer. I execution is entirely successful the return code will be 0.

I want to create set a variable to have the contents of another variable based on the outcome. Currently I'm using a big ugly if elif construct, but it feels like I should be using a case statement.

Here's my current code:

if [[ "$sumcreate_retval" -lt "0" ]] && [[ "$movesum_retval" -lt "0" ]]
then
   script_retcode="$both_warn_err"
elif [[ "$sumcreate_retval" -gt "0" ]] && [[ "$movesum_retval" -gt "0" ]]
then
   script_retcode="$both_crit_err"
elif [[ "$sumcreate_retval" -gt "0" ]] && [[ "$movesum_retval" -lt "0" ]]
then
   script_retcode="$createwarn_movecrit_err"
elif [[ "$sumcreate_retval" -gt "0" ]] && [[ "$movesum_retval" -eq "0" ]]
then
   script_retcode="$createwarn_err"
elif [[ "$sumcreate_retval" -lt "0" ]] && [[ "$movesum_retval" -gt "0" ]]
then
   script_retcode="$createcrit_movewarn_err"
elif [[ "$sumcreate_retval" -lt "0" ]] && [[ "$movesum_retval" -eq "0" ]]
then
   script_retcode="$createcrit_err"
elif [[ "$sumcreate_retval" -eq "0" ]] && [[ "$movesum_retval" -gt "0" ]]
then
   script_retcode="$movewarn_err"
elif [[ "$sumcreate_retval" -eq "0" ]] && [[ "$movesum_retval" -lt "0" ]]
then
   script_retcode="$movecrit_err"
else
   script_retcode="$success_return"
fi

How should I restructure this?

Note: If this question is better suited to another SE site please let me know.

2 Answers 2

2

Something like this should do the trick. I think it looks kind of nice this way:

case $((
  sumcreate_retval <  0 && movesum_retval <  0 ? 1 :
  sumcreate_retval >  0 && movesum_retval >  0 ? 2 :
  sumcreate_retval >  0 && movesum_retval <  0 ? 3 :
  sumcreate_retval >  0 && movesum_retval == 0 ? 4 :
  sumcreate_retval <  0 && movesum_retval >  0 ? 5 :
  sumcreate_retval <  0 && movesum_retval == 0 ? 6 :
  sumcreate_retval == 0 && movesum_retval >  0 ? 7 :
  sumcreate_retval == 0 && movesum_retval <  0 ? 8 : 
  0
)) in
  (1) script_retcode="$both_warn_err";;
  (2) script_retcode="$both_crit_err";;
  (3) script_retcode="$createwarn_movecrit_err";;
  (4) script_retcode="$createwarn_err";;
  (5) script_retcode="$createcrit_movewarn_err";;
  (6) script_retcode="$createcrit_err";;
  (7) script_retcode="$movewarn_err";;
  (8) script_retcode="$movecrit_err";;
  (0) script_retcode="success_return";;
esac
0
1

This is like @imjoris's answer, but I broke up the math. It's in ternary, where 0 is 0, 1 is positive, and 2 is negative by default. I rearranged your list to make it clearer -- hope that doesn't trip you up :-)

case $((
  sumcreate_retval == 0 ? 0 :
  sumcreate_retval >  0 ? 1 :
  2
  ))$((
  movesum_retval == 0 ? 0 :
  movesum_retval >  0 ? 1 :
  2
)) in
  00) script_retcode="$success_return" ;;
  01) script_retcode="$movewarn_err" ;;
  02) script_retcode="$movecrit_err" ;;
  10) script_retcode="$createwarn_err" ;;
  11) script_retcode="$both_crit_err" ;;
  12) script_retcode="$createwarn_movecrit_err" ;;
  20) script_retcode="$createcrit_err" ;;
  21) script_retcode="$createcrit_movewarn_err" ;;
  22) script_retcode="$both_warn_err" ;;
esac

BTW, StackOverflow is probably the better site for complex scripting questions like these.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .