Shell Script = Command "Test" = Simple Question

an excerpt of a startup script looks like this:

ENABLED=1

test "$ENABLED" != "0" || exit 0

The problem is, no matter if Enabled is set to 0 or 1, the script always exits at that line. I looked up "man test" and as far as I understand it, test will always evaluate the expression and then exit(!!) with the return code? Is this correct. So This script will, at this line, always exit no matter what the value of the variable is? (so its buggy).

thanks!! jens


Solution 1:

#!/bin/bash
ENABLED=1
test "$ENABLED" != "0" || exit 0
echo "wasn't zero"

when i run this, i get

[madhatter@risby tmp]$ ./foo
wasn't zero

So, it seems to me that either

  • you're using an odd interpreter (which shell are you running this under), or
  • you're picking up the wrong `test` (could you do a `which test` and tell us the results, or explicitly use `/usr/bin/test`?), or
  • (sorry about this) you're wrong about it not getting to the next line (as Janne says above, could we see more of the shell script, or could you put a one-line canary like my echo statement immediately after the test?