0

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

jens
  • 1,001
  • 1
  • 10
  • 10
  • Is this supposed to be Perl or bash? – Ignacio Vazquez-Abrams Nov 03 '10 at 05:32
  • Hello Ignacio,thanks for your anser. Uh well I actually do not konw (I am no expert in this). Its a default Debian Lenny installation and the script is a startscript of a package that I installed. The Script starts with #!/bin/sh – jens Nov 03 '10 at 05:35

3 Answers3

4
#!/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?
MadHatter
  • 79,770
  • 20
  • 184
  • 232
0

With some general assumption here, in an attempt to answer the question specifically, that is:

So This script will, at this line, always exit no matter what the value of the variable is? (so its buggy).

Yes, more or less, but not necessarily exit the entire script. And also, no, it's not buggy; if my assumptions are correct.

As others have mentioned, it's difficult to give a definitive answer without seeing more of the script, but I would imagine there was reason for your unwillingness to share what package it is you've added which I make no attempt to pass judgement on. Rather, it is merely to assume your pardon in assuming that this little snippet from your script is near the beginning, shortly after some parameters are declared such as PATH, NAME, etc. and there is more to the script, probably most of it, following this. (Hence, the seemingly preposterous response as to why the script has this.)

To clarify, more than likely this part of the script is merely there to ensure that when the script launches, an instance of the function/process it calls/uses is not already launched or in use. In other words, if a script calls for program then basically, this checks if program is already running, if yes, exit quietly, then the script can continue with assurance of being able to launch program without running into an errors involving another process or instance already running. Especially if an already launched process was set with different parameters than the script calls for, then you'd blame the script.

Again, assumptions were made and the explanation is broad, but hopefully that helps clarify any passerby to this decade-old question.

pfftt, 170 views over 11 years...ah well, it just takes one vote to make it all worth it!

Doedigo
  • 1
  • 1
0

That test statement basically says "exit with return code 0 (which usually means OK in this wonderful world of Linux/Unix) if $ENABLED is set to something else than 0".

Did this clarify you at all? It's hard to tell you more without seeing the complete start-up script :-)

Janne Pikkarainen
  • 31,852
  • 4
  • 58
  • 81