Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Path: utzoo!utgpu!water!watnot!watmath!clyde!rutgers!lll-lcc!pyramid!prls!mips!dce From: dce@mips.UUCP Newsgroups: comp.bugs.4bsd Subject: Re: Bug/Feature in `test' - (nf) Message-ID: <256@quacky.mips.UUCP> Date: Fri, 3-Apr-87 09:53:57 EST Article-I.D.: quacky.256 Posted: Fri Apr 3 09:53:57 1987 Date-Received: Sun, 5-Apr-87 05:38:35 EST References: <6200001@iaoobelix.UUCP> Reply-To: dce@quacky.UUCP (David Elliott) Organization: MIPS Computer Systems, Sunnyvale, CA Lines: 51 In article <6200001@iaoobelix.UUCP> wagner@iaoobelix.UUCP writes: >The file `try' contains the following: > > #! /bin/sh > if [ "$1" = "-d" ] > then > echo ok > fi > >The I typed in: > > % try 8 > bad > % try -d This answer may not be the one you are fishing for, but I think it is useful nonetheless. When you say "try -d", the test line gets interpreted as [ "-d" = "-d" ] which is syntactically incorrect given the definition of test, which says that a -d is followed by a name which is checked to see if it is a directory. It doesn't specifically say that '=' takes precedence over '-d', so you can never assume that it does (or that '-d' takes precedence). You have to be completely clear (I want to say unambiguous, but won't). My own habit is to always say [ " value" op " value" ] This has three advantages: 1. It is syntactically clear. 2. It forces you to put quotes around the values, which avoids problems with variables containing spaces/tabs, and makes the values stand out. 3. It is aesthetically pleasing (as opposed to the more common [ xvalue op xvalue ] which breaks when spaces/tabs are used) As an alternative, 'case' works quite well for string comparisons, and for number equality if you know what the numbers look like (i.e., you can check for $# being equal to a specific value, since $# will be expanded with no extra spaces or tabs or leading 0s). -- David Elliott {decvax,ucbvax,ihnp4}!decwrl!mips!dce