From owner-freebsd-bugs Tue Jun 4 02:53:59 1996 Return-Path: owner-bugs Received: (from root@localhost) by freefall.freebsd.org (8.7.5/8.7.3) id CAA29616 for bugs-outgoing; Tue, 4 Jun 1996 02:53:59 -0700 (PDT) Received: from irz301.inf.tu-dresden.de (irz301.inf.tu-dresden.de [141.76.1.11]) by freefall.freebsd.org (8.7.5/8.7.3) with SMTP id CAA29603 for ; Tue, 4 Jun 1996 02:53:39 -0700 (PDT) Received: from sax.sax.de by irz301.inf.tu-dresden.de (8.6.12/8.6.12-s1) with ESMTP id LAA21079; Tue, 4 Jun 1996 11:51:36 +0200 Received: (from uucp@localhost) by sax.sax.de (8.6.12/8.6.12-s1) with UUCP id LAA29022; Tue, 4 Jun 1996 11:51:20 +0200 Received: (from j@localhost) by uriah.heep.sax.de (8.7.5/8.6.9) id LAA21894; Tue, 4 Jun 1996 11:47:19 +0200 (MET DST) From: J Wunsch Message-Id: <199606040947.LAA21894@uriah.heep.sax.de> Subject: Re: sh bug To: jin@george.lbl.gov (Jin Guojun[ITG]) Date: Tue, 4 Jun 1996 11:47:19 +0200 (MET DST) Cc: bugs@freebsd.org, problem@bsdi.com Reply-To: joerg_wunsch@uriah.heep.sax.de (Joerg Wunsch) In-Reply-To: <199606031939.MAA26616@george.lbl.gov> from "Jin Guojun[ITG]" at "Jun 3, 96 12:39:13 pm" X-Phone: +49-351-2012 669 X-PGP-Fingerprint: DC 47 E6 E4 FF A6 E9 8F 93 21 E0 7D F9 12 D6 4E X-Mailer: ELM [version 2.4ME+ PL17 (25)] MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Sender: owner-bugs@freebsd.org X-Loop: FreeBSD.org Precedence: bulk As Jin Guojun[ITG] wrote: > The sh in BSD does not take "-" as the argument in if statement if the > statement has more than one comparsions. Not the shell -- test(1) is a separate program. > The following line are generating errors: > > if [ "$1 = "-h" -o "$1" = "-help" ]; then > ... > fi > > if [ "$1 = "-h" -o 1 -le 2 ]; then > ... > fi > > if [ \( "$1 = "-h" \) -o \( "$1" = "-help" \) ]; then > ... > fi > > ERROR: > + [ -h = -h -o -h = -help ] > [: syntax error: Undefined error: 0 > > > but, it works in a single argument if statement: > > if [ "$1 = "-h" ]; then > ... > fi You are apparently running into an operator precedence problem. Anyway, your method is not portable programming, in case you are not sure whether the first operand might start with a hyphen, better prepend it with a knwon to be alpha character, as in: if [ "X$1" = "X-h" ] ; then etc. Our test(1) implementation fully agrees with Posix.2: The algorithm for determining the precedence of the operators and the 1 return value that shall be generated is based on the number of arguments 1 presented to test. (However, when using the [...] form, the right- 1 bracket final argument shall not be counted in this algorithm.) In the 1 following list, $1, $2, $3, and $4 represent the arguments presented to 1 test. 1 0 arguments: 1 ... 1 argument: 1 ... 2 arguments: 1 ... 3 arguments: 1 - If $2 is a binary primary, perform the binary test of $1 and 2 $3. 2 - If $1 is !, negate the two-argument test of $2 and $3. 1 - Otherwise, produce unspecified results. 1 4 arguments: 1 - If $1 is !, negate the three-argument test of $2, $3, and $4. 1 - Otherwise, the results are unspecified. 1 >4 arguments: 1 The results are unspecified. 1 The ``> 4 arguments'' is the case you are complaining about. As you can see, it's up to the implementation of the test command to decide about the operator precedence. It apparently decides that the first operator is to be taken as the -h flag (test for a symbolic link), and the following equation sign is taken as the filename argument to -h. The remaining arguments finally result in a syntax error. -- cheers, J"org joerg_wunsch@uriah.heep.sax.de -- http://www.sax.de/~joerg/ -- NIC: JW11-RIPE Never trust an operating system you don't have sources for. ;-)