nunca use o operador '-a' ou '-o' com '['

11

Stéphane Chazelas escreveu:

a few rules like

  • always quote variables
  • never use the -a or -o operator (use several [ commands and the && and || shell operators)

Make [ reliable with POSIX shells.

Por que "nunca usar o operador -a ou -o "?

Como posso fazer "usar vários comandos [ e os operadores && e || shell)"?

    
por Tim 19.03.2016 / 08:33

1 resposta

13

Why "never use the -a or -o operator"?

Porque eles podem ser ambíguos e, portanto, não são compatíveis com POSIX :

The XSI extensions specifying the -a and -o binary primaries and the '(' and ')' operators have been marked obsolescent. (Many expressions using them are ambiguously defined by the grammar depending on the specific expressions being evaluated.) Scripts using these expressions should be converted to the forms given below. Even though many implementations will continue to support these obsolescent forms, scripts should be extremely careful when dealing with user-supplied input that could be confused with these and other primaries and operators. Unless the application developer knows all the cases that produce input to the script, invocations like:

test "$1" -a "$2"

should be written as:

test "$1" && test "$2"

How can I do "use several [ commands and the && and || shell operators)"?

Fazendo vários testes e encadeando-os usando esses operadores; por exemplo:

[ 0 -eq 0 -a \( 0 -eq 1 -o 1 -eq 1 \) ]

poderia ser reescrito como o equivalente:

[ 0 -eq 0 ] && ([ 0 -eq 1 ] || [ 1 -eq 1 ])

ou melhor:

[ 0 -eq 0 ] && { [ 0 -eq 1 ] || [ 1 -eq 1 ]; }
    
por 19.03.2016 / 08:45

Tags