K.S. Bhaskar on 13 Oct 2009 19:25:58 -0700 |
On Tue, Oct 13, 2009 at 7:21 PM, Walt Mankowski <waltman@pobox.com> wrote: > On Tue, Oct 13, 2009 at 03:52:24PM -0400, K.S. Bhaskar wrote: >> 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 >> 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. > > Well, I don't know if it's *better*, but if you've got perl installed > on the box you could always say > > perl -e 'exit defined $ENV{foo}' > > which will set $? to either 1 or 0. > > But to be honest, instead of trying to come up with a solution to this > problem, I'd probably concentrate on avoiding putting nulls in my > environment variables in the first place. [KSB] Perl is not an option. I'm trying to package something to run in as small a virtual machine as possible (testing on busybox on Tiny Core Linux / Micro Core Linux). But, building on Paul's suggestion, what I have at the beginning of this script is: if [ -n ${gtm_passwd+isset} ] ; then tmp_passwd=$gtm_passwd ; export tmp_passwd ; unset gtm_passwd else unset tmp_passwd ; fi and at the end I have: if [ -n ${tmp_passwd+issset} ] ; then gtm_passwd=$tmp_passwd ; export gtm_passwd ; fi So far, it seems to work, but I have more testing to do. Thanks, all. 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
|
|