on Sun, May 06, 2001 at 02:22:40AM +0300, Tommi Komulainen ([EMAIL PROTECTED]) wrote: > On Sat, May 05, 2001 at 02:34:47PM -0700, Karsten M. Self wrote: > > > > In contrast, csh and derivatives have: > > > > $?MYVAR > > > > ...which allows testing of presence of a variable. > > > > ...but I'm not aware of a similar bash/korn/bourne feature. Anyone? > > Coming soon to ./configure scripts near you: > > if test "${LANG+set}" = set; then LANG=C; export LANG; fi
Close, but not quite. It's probably the nearest thing to a winner I've seen posted though. And you just edged out Alan's suggestion of an equivalent but opposite test. Note that this is a specific application of the Use Alternate Value bash parameter expansion. The value "set" isn't special, though it's a good mnemonic. > or in your case: > > if test "${MYVAR+set}" = set; then > echo "MYVAR doesn't exist" > else > echo "MYVAR exists, value: $MYVAR" > fi > > > also available in bash(1): > > In each of the cases below, word is subject to tilde > expansion, parameter expansion, command substitution, and > arithmetic expansion. When not performing substring > expansion, bash tests for a parameter that is unset or > null; omitting the colon results in a test only for a > parameter that is unset. > > ${parameter:+word} > Use Alternate Value. If parameter is null or > unset, nothing is substituted, otherwise the expan > sion of word is substituted. The possible problem" "*null or* unset". An existing, but null, environment variable will be reported as unset. It's possible to create a null environment variable, e.g.: $ foo= $ echo $foo $ echo ${foo:-unset} unset $ typeset -p foo declare -- foo="" $ unset foo $ typeset -p foo bash: typeset: foo: not found $ echo $? 1 ...which suggests if ! typeset -p myvar 2>/dev/null ...as a test under bash which accurately reports whether or not an environment variable exists, evaluated or otherwise, rather than merely has a non-null value. Note that yet another option would be to access /proc/self/environ to check for the existence of an environment variable. Which works under GNU/Linux but not other Unices. > Since it is used in configure scripts, I'd guess it's pretty portable. It does the job. It may not be optimal. And, yes, portability is an issue here. -- Karsten M. Self <kmself@ix.netcom.com> http://kmself.home.netcom.com/ What part of "Gestalt" don't you understand? There is no K5 cabal http://gestalt-system.sourceforge.net/ http://www.kuro5hin.org
pgpmSQHyaXzPp.pgp
Description: PGP signature