if test; then
stuff
and
if test
then
stuff
are functionally equivalent. I wouldn't say one or the
other `is preferred.' I use the former because I think
it's a little more readable because more compact. But
it's really a matter of style, like whether you write
if (test) {
(multi-statement block)
or
if (test)
{
(multi-statement block)
I have a stronger opinion about those who use overly-
cryptic constructions like
test && {
shell commands
}
because it means exactly the same thing as
if test; then
shell commands
but is more obscure to read. But again it's a question
of style, not of dogma.
As an aside, I think one excuse that is sometimes used
for that sort of construct is when it's
test || {
commands
}
because Bourne's original shell had no not operator.
For a long time after shell functions appeared, I would
add this function to any of my shell scripts that needed
it:
not() {
if "$@"; then
return 1
else
return 0
}
so I could say
if not test; then
commands
fi
Modern Bourne-shell descendants have a built-in ! operator:
if ! test; then
commands
fi
I'm not keen on most of what has been stuffed into bash and
ksh and the like, but ! is a real improvement. I believe
POSIX mandates it, and I think they're right.
Norman Wilson
Toronto ON
Show replies by date