|
Chapter 45 Shell Programming for the Initiated
|
|
Some people think that the Bourne shell's
:
is a comment character.
It isn't, really.
It evaluates its arguments and returns a zero
exit status (
44.7
)
.
Here are a few places to use it:
-
Replace the UNIX
true
command to make an endless
while
loop (
44.10
)
.
This is more efficient because the shell doesn't have to start a new
process each time around the loop
(as it does when you use
while true
):
while :
do
commands
done
(Of course, one of the
commands
will probably be
break
,
to end the loop eventually.)
-
When you want to use the
else
in an
if
(
44.8
)
,
but leave the
then
empty,
the
:
makes a nice "do-nothing" place filler:
if
something
then :
else
commands
fi
-
If your Bourne shell doesn't have a true
#
comment character,
you can use
:
to "fake it."
It's safest to use quotes so the shell won't try to interpret
characters like
>
or
|
in your "comment":
: 'read answer and branch if < 3 or > 6'
-
Finally, it's useful with
parameter substitution (
45.12
)
like
${
var
?}
or
${
var
=
default
}
.
For instance, using this line in your script will print an error and exit
if either the
USER
or
HOME
variables aren't set:
: ${USER?} ${HOME?}
|
|