bergman on 13 Oct 2009 13:12:03 -0700 |
In the message dated: Tue, 13 Oct 2009 15:52:24 EDT, The pithy ruminations from "K.S. Bhaskar" on <[PLUG] Distinguishing between environment variable with null value and one tha t is not set> were: => It appears that POSIX shell (and bash) have no built-in way to => distinguish between the case where an environment variable exists with => a null value and the case where it simply does not exist. The Yes, that seems to be the case. Hmmm....I know that a null value is not the same as the variable not being set, but I'm having tough time thinking of a scenario where that difference is important, especially since the shell doesn't distinguish between the two states. => simplest way I have been able to come up with is to grep the output of => env and test for zero length, e.g.: => => $ if [ -z "`env | grep foo`" ] ; then echo It doesn\'t exist\! ; fi => It doesn't exist! => $ if [ -z "`env | grep PWD`" ] ; then echo It doesn\'t exist\! ; fi => $ => => Is there a better way? Thanks in advance. I'd do something very similar: env | grep -q FOO if [ $? != 0 ] ; then echo "FOO is not set" else if [ -z $FOO ] ; then echo "FOO is set to NULL" else echo "FOO is \"$FOO\"" fi fi Is this really "better"? I don't know....but I find the use of "grep -q" to be "cleaner" and easier to examine visually than embedding a sub-shell and commands within the test ([]) operator. Mark => => Regards => -- Bhaskar ___________________________________________________________________________ Philadelphia Linux Users Group -- http://www.phillylinux.org Announcements - http://lists.phillylinux.org/mailman/listinfo/plug-announce General Discussion -- http://lists.phillylinux.org/mailman/listinfo/plug
|
|