[olug] Unix Tip: TEST TROUBLES IF STRING IS EMPTY

Jay Hannah jay at jays.net
Mon Jun 9 13:47:44 UTC 2003


Ick. Perl interacts with environment variables via the reserved hash
%ENV. If you want to test existance you'd do this:

   if (exists $ENV{OPTARG}) {

To see if it's set to "foo":

   if ($ENV{OPTARG} eq "foo") {

To see if it contains "foo" (case insensitive):

   if ($ENV{OPTARG} =~ /foo/i) {

All much cleaner than the shell stuff below.

Cheers,

Jay Hannah
Omaha Perl Mongers: http://omaha.pm.org



Unix Guru Universe wrote:
> 
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> 
>                               UNIX GURU UNIVERSE
>                                  UNIX HOT TIP
> 
>                         Unix Tip 1985 - June  8, 2003
> 
>                     http://www.ugu.com/sui/ugu/show?tip.today
> 
> =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
> 
> TEST TROUBLES IF STRING IS EMPTY
> 
> (1) bad ex.
> if [ `echo "$OPTARG" | sed '/^[0-9][0-9]*$/!d` = "" ]
> 
> (2) fixed ex.
> if [ `echo "$OPTARG" | sed '/^[0-9][0-9]*$/ s//X/'`= "X" ]
> 
> (3) another fixed ex. (Bourne Shell, ksh)
> if [ -n "`echo "$OPTARG" | sed '/^[0-9][0-9]*$/!d'`" ]
> 
> (1) This produces an easy
> to overlook error.  On
> success and the lack of
> double quotes on the left
> side cause test to think
> there is no parameter,
> producing an error (1).
> This is because the result
> is an empty string without
> quotes and test doesn't
> know that it is dealing
> with a string, no parameter
> seen.
> 
> Adding a character to the
> beginning of each string is
> a trick to fix it (2).  The
> test command sees a string
> and simply compares as normal,
> passing over the pair of
> initial and equal characters.
> 
> Another fix (3) is to surround
> the empty/not empty string with
> double quotes.  Test will see
> the empty string and things
> work as normal.  This (3) works
> in Bourne Shell and ksh but NOT
> in csh.
> 
> Simple ex.
> $ foo=""
> $ test -n $foo          # fails
> $ test -n `echo $foo`   # fails
> $ test -z `echo x$foo`  # works, might throw off the logic
> $ test -n "`echo $foo`" # works in sh & ksh
> 
> This tip generously supported by: bigoldbulldog at hotmail.com
> 
> --------------------------------------------------------------------------
> To Subscribe:    http://www.ugu.com/sui/ugu/show?tip.subscribe
> To Unsubscribe:  http://www.ugu.com/sui/ugu/show?tip.unsubscribe
> To Submit A Tip: http://www.ugu.com/sui/ugu/show?tip.today
> 
> ==========================================================================
> DISCLAIMER: All UNIX HOT TIPS ARE OWNED BY THE UNIX GURU UNIVERSE AND ARE
> NOT TO BE SOLD, PRINTED OR USED WITHOUT THE WRITTEN CONSENT OF THE UNIX
> GURU UNIVERSE. ALL TIPS ARE "USE AT YOUR OWN RISK". UGU  ADVISES THAT
> ALL TIPS BE TESTED IN A NON-PRODUCTION DEVELOPMENT ENVIRONMENT FIRST.
> 
> Unix Guru Universe - www.ugu.com - tips at ugu.com - Copyright 1994-2001
> ==========================================================================
> 
> _______________________________________________
> OLUG mailing list
> OLUG at olug.org
> http://lists.olug.org/mailman/listinfo/olug


More information about the OLUG mailing list