Date: Tue, 4 Jun 1996 11:47:19 +0200 (MET DST) From: J Wunsch <j@uriah.heep.sax.de> To: jin@george.lbl.gov (Jin Guojun[ITG]) Cc: bugs@freebsd.org, problem@bsdi.com Subject: Re: sh bug Message-ID: <199606040947.LAA21894@uriah.heep.sax.de> In-Reply-To: <199606031939.MAA26616@george.lbl.gov> from "Jin Guojun[ITG]" at "Jun 3, 96 12:39:13 pm"
next in thread | previous in thread | raw e-mail | index | archive | help
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. ;-)
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199606040947.LAA21894>