Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!wuarchive!hsdndev!cmcl2!panix!eravin From: eravin@panix.uucp (Ed Ravin) Newsgroups: comp.unix.aix Subject: Re: Using ksh set -u breaks xinit script... Summary: You can assign defaults at the first reference Message-ID: <1991Mar28.153242.8791@panix.uucp> Date: 28 Mar 91 15:32:42 GMT References: <1991Mar26.051930.16308@panix.uucp> <1991Mar28.012949.1375@arnor.uucp> Organization: Unix Sezchuan Enlightment Team Lines: 39 In article prener@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 |