LINUX.IE, website of the Irish Linux Users' Group
Tux rules!

   
Home
New Users
Articles
Download
Projects
Community
Vendors

  Print Version
Email to...
 
Archives:


planetILUG

Recent News

News Archive


Join the
ILUG
on FaceBook


Join the
ILUG
on LinkedIn


Join the
ILUG SETI
Group



















 
 :: Mailing Lists

[ILUG] test(1), [(1), and "X$str" [was- POTD reminder ]

[ILUG] test(1), [(1), and "X$str" [was- POTD reminder ]

Brian Foster blf at blf.utvinternet.co.uk
Mon Dec 13 20:09:00 GMT 2004


  | From: P at draigBrady.com
  | Date: Mon, 13 Dec 2004 17:31:45 +0000
  | 
  | kevin lyda wrote:
  | > On Mon, Dec 13, 2004 at 04:07:38PM +0000, P at draigBrady.com wrote:
  | >>Shell conditionals
  | >>   if [ "$str" = "1" ] || [ "$int" -eq "1" ]; then
  | >>        echo "str or int is 1"
  | >>   elif [ "$int" -lt "10" ] && [ -e "file" ]; then
  | >>        echo "int less than 10 and file exists"
  | >>   fi
  | > 
  | > it's better if you do the following:
  | >    if [ X"$str" = X"1" ] || [ "$int" -eq "1" ]; then
  | >        echo "str or int is 1"
  | >    elif [ "$int" -lt "10" ] && [ -e "file" ]; then
  | >        echo "int less than 10 and file exists"
  | >    fi
  | > that way if $str begins with a - it won't annoy test.

 good grief.
 I hereby award both of the above the
 Useless Use of Quotes Award.  multiple times.

 something like the following is sufficient (but, like the
 above, assumes that $int has an integer value (in which
 case the quotes bracketing $int are not strictly needed)):

        if [ "X$str" = X1 -o "$int" -eq 1 ]; then
             echo str or int is 1
        elif [ "$int" -lt 10 -a -e file ]; then
             echo int less than 10 and file exists
        fi

 on the "X$str" trick, however you write it, it _is_ needed
 on various (older) Bourne-ish shells.  current GNU bash(1)
 has heuristics that usually don't go wrong, but which apply
 only to its built-in test(1).  if you are (or may be) using
 some other Bourne-ish shell, or not using the built-in test,
 you are much much better off using the "X$str" trick.

  | Apart from that X"$str" trick looking horrible,
  | it's unecessary from my testing. test and the
  | builtin [ seem to be clever enough to parse options
  | only when appropriate.

 (IMHO, the only reason the above "X$str" trick looks horrible
 is because it was horribly written.)

 `test' and `[' are synonyms.  they are should be identical.
 both are builtin to most Bourne-ish shells, and both are
 also external commands (e.g., you probably have a /usr/bin/[
 linked to /usr/bin/test --- albeit having said that, I note
 that for some reason my SUSE 9.1 system has different binaries
 for /usr/bin/test and /usr/bin/[ --- they are not linked, and
 the files themselves are different, and they handle `--version'
 differently.  *sigh*)

cheers!
	-blf-
-- 
Experienced (20+ years) kernel engineer: |  Brian Foster      Montpellier,
 · Unix, ChorusOS, &tc; · documentation; |  blf at utvinternet.ie      FRANCE
 · IDL, automated testing, process, &tc. |    Stop E$$o (ExxonMobile)!
Résumé:  http://www.blf.utvinternet.ie   |        http://www.stopesso.com



More information about the ILUG mailing list
Read this without the formatting.
                                                                                                    

 

Hosted by HEAnet


Maintained by the ILUG website team. The aim of Linux.ie is to support and help commercial and private users of Linux in Ireland. You can display ILUG news in your own webpages, read backend information to find out how. Networking services kindly provided by HEAnet, server kindly donated by Dell. Linux is a trademark of Linus Torvalds, used with permission. No penguins were harmed in the production or maintenance of this highly praised website. Looking for the Indian Linux Users' Group? Try here. If you've read all this and aren't a lawyer: you should be!
RSS Version
Powered by Dell