Why does the test say unexpected brackets?

I have a line ifin my shell script that looks like this:

if [ 0 -lt 2 -o (0 -gt 3 ) ]

and this gives an error:

line 3: syntax error near unexpected token `('

So, I looked at man testto make sure that parens were supported and, of course, very close to the top of the man page, they are! What gives!?

The above example does not exactly match the code, because I tried to clear it to prove the point, but this is my repo if you need context.

EDIT: the if line has changed to match the error.

+4
source share
3 answers

test . [ ... ] - test. , [ ].

, , , , :

if [ \( $# -lt 2 \) -o \( $# -gt 3 \) ]

:

if [ '(' $# -lt 2 ')' -o '(' $# -gt 3 ')' ]

:

`test'
`['
          test EXPR

     Evaluate a conditional express ion EXPR and return a status of 0
     (true) or 1 (false).  Each operator and operand must be a separate
     argument.

, :

if builtin test \( $# -lt 2 \) -o \( $# -gt 3 \)
+5

(...) [ ... ], .

:

[[ $# -lt 2 || $# -gt 3 ]]

:

[[ $# -lt 2 ]] || [[ $# -gt 3 ]]
+3

, C style , , " bash

#!/bin/bash

if (( "$#" < 2 || "$#" > 3 )); then
+3
source

Source: https://habr.com/ru/post/1661539/


All Articles