On Fri, 2013-04-26 at 09:09 -0400, Jonathan Kamens wrote:
On 04/26/2013 08:58 AM, Cristian Sava wrote:
> Webalizer is enabled via /etc/sysconfig/webalizer but I don't understand
> why /etc/cron.daily/00webalizer has this line:
>
> [ "z$WEBALIZER_CRON" != "zyes" ] && exit 0
>
> that I think it should be (and it is working this way)
>
> [ "$WEBALIZER_CRON" != "yes" ] && exit 0
>
> What is the mistery?
>
The two versions you quoted are functionally equivalent on Linux. If
there's a z before the variable expansion and a z before the word
"yes", then the two z's cancel out and don't affect the string
comparison.
As for why they are there, it's an old shell programmers' trick to
avoid causing syntax errors when doing string comparisons to empty
strings. Let me explain...
If you do this:
[ $WEBALIZER_CRON != yes ]
and the variable WEBALIZER_CRON is empty, then the command above will
result in an error, because when it's evaluated, it will look to the
shell as if there is nothing to the left of the '!=' and therefore
there's a missing expression in the statement.
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.
It's unnecessary to use both the quotes and the extra character on
both sides of the expression. I'm not sure, but I think it may have
been necessary to use both at some point in the past, because I think
the middle example above, with just the quotes, might actually have
been buggy in some old versions of the "test" program that evaluates
such expressions. This caused shell programmers to get into the habit
of using both the quotes and the extra character. I doubt it's been
necessary to do that for many years, though for all I know webalizer
may support some UNIX versions that are so old that they still have
problems in this area. Given that possibility, there's certainly no
harm in writing the test the way it's written in the file you looked
at.
jik
Thank you very much for your explanation,
C.Sava