F18, F19 webalizer problem?

Chris Adams cmadams at hiwaay.net
Fri Apr 26 13:30:05 UTC 2013


Once upon a time, Jonathan Kamens <jik at kamens.us> said:
> This, however, is fine:
> 
> [ "$WEBALIZER_CRON" != yes ]
> 
> because the quotes ensure that the statement will be evaluated with an 
> expression to the left of the != even if the expression is just an empty 
> string.
> 
> This is fine too:
> 
> [ z$WEBALIZER_CRON != zyes ]
> 
> because if the variable is empty, the expression to the left will be "z" 
> rather than an empty string.

The reason some use a combination of quotes and a leading character is
for testing user-provided input.  It shouldn't matter in this case, but
it is just a little bit more defensive programming.

The problem if you are testing a user-provided variable is that they
could give input that starts with a dash, a close bracket, etc., and
that would screw up the test.  Putting a character at the start protects
against that.

Also, always quoting the variable is good programming practice; it could
have whitespace in it, in which case the non-quoted version would expand
to multiple tokens (and again break).

So, still today, the best defensive way is:

   [ "z$WEBALIZER_CRON" != zyes ]

-- 
Chris Adams <cmadams at hiwaay.net>
Systems and Network Administrator - HiWAAY Internet Services
I don't speak for anybody but myself - that's enough trouble.


More information about the test mailing list