Using ksh set -u breaks xinit script...

Ed Ravin eravin at panix.uucp
Fri Mar 29 01:32:42 AEST 1991


In article <mumble at arnor.uucp> prener at prener.watson.ibm.com (Dan Prener) writes:
>If all uses of undefined shell variables are to be considered errors,
>then it becomes very difficult to get first-time switches and to
>keep other state.

It's not at all difficult.  The first time you reference any shell variable,
you can specify a value to be used instead if the variable is not set or a
value to assign it to if the variable is not set.  For example:

echo $FOO ${FOO}	# normal references to a variable.  Note that
# the { } characters to delimit the variable name are optional.

echo ${FOO:-default}	# here, the word "default" will be used instead of
# the FOO variable if FOO is not set.

echo ${FOO:=intial_value}	# If FOO is not set, it will be created
# and assigned the value "initial_value"

The nice thing about doing this is that you never get surprised -- for
example, it's easy to do something like:

myvalue="Isn't today a lovely day?"
if [ $myvaleu = "No it isn't" ]
	then echo blah blah
fi

With set -u on, I would get an error message at the reference to "myvaleu",
letting me know that I had goofed.

The only hassle is that when you reference system variables like $@ and $*,
you have to allow for the possibility that your script was called without
parameters, and reference them instead with ${@:-} and ${*:-}.

These constructions work in both ksh and Bourne shell.
-- 
----------------------------------------------------------------------------
Ed Ravin            | Even if I could think of a profound, witty, insightful
cmcl2!panix!eravin  | quote to put here noone would bother reading it.
philabs!trintex!elr |



More information about the Comp.unix.aix mailing list