Home > Net >  Exit with error if bash "if statement" condition errors
Exit with error if bash "if statement" condition errors

Time:12-02

According to this post:

The exit status of the if command shall be the exit status of the then or else compound-list that was executed, or zero, if none was executed.

This leads to a bug in my bash script where:

  • I have set -euo pipefail set in the script and expect it to stop executing if any errors arise
  • I have an if statement with a condition that errors, but execution continues because -e doesn't care about the condition in if statements

Specifically, I have an if function1; then and I want to shield from the case where function1 doesn't exist.

The scenario is that someone modifies function1, it no longer exists, but the script runs fine and the user doesn't realize that the script failed.

This is meant to be for every if statement in a bash file, so it's not a matter of making sure function1 exists, I'm looking for a blanket solution for a file, like set -euo pipefail that requires as little refactoring as possible.

I've looked in the set documentation, and nothing seems to be suitable there.

Summary:

function1 does not exist

run if function1; then ...

Expected:

script exits

Actual:

whatever.sh: line ##: function1: command not found

script continues

CodePudding user response:

Specifically, I have an if function1; then and I want to shield from the case where function1 doesn't exist.

So implement that.

fn_exists() { [[ "$(LC_ALL=C type -t -- "$1" 2>/dev/null)" = function ]]; }

call_fn_or_fail() {
   if ! fn_exists "$1"; then
      echo "hatever.sh: line ##: function1: command not found" >&2
      exit 1
   fi
   "$@"
}

if call_fn_or_fail function1; then

CodePudding user response:

I don't think i get your problem. Heres a script i wrote for testing purposes

#!/bin/bash
set -euo pipefail


somefunc(){
 echo 'ok'
}


echo first if 
if somefunc; then 
    echo yea it was there alright 
    grep "gimme exit code 1" /etc/passwd
    echo $?
fi

echo second if
#type somefunk

if somefunk; then
    echo this should not happen
fi

echo end of script

if errors out after the exit code 1 for grep fails to find that string in my /etc/passwd file.

bash -x sotest.sh 
  set -euo pipefail
  echo first if
first if
  somefunc
  echo ok
ok
  echo yea it was there alright
yea it was there alright
  grep 'gimme exit code 1' /etc/passwd

This is basically exactly what you want.

comment that grep out and we get this

./sotest.sh 
first if
ok
yea it was there alright
0
second if
./sotest.sh: line 20: somefunk: command not found
end of script

Of course the second if fails and the script carries on, thats why you have an if statement, you are recognizing the possibility that you get an error and only running the "then" if that doesn't happen.

You have two options. Check that the functions exist. Don't call them with if. If you attempt to call a function that doesn't exist it will error and your script will exit.

  •  Tags:  
  • bash
  • Related