What is a unary operator

4

Code:

#!/bin/bash
echo "Digite 1:"
read$TESTE

if [ $TESTE == 654 ]
    then
    echo "Usage: ./pingscript.sh [network]"
    echo "example: ./pingscript.sh 192.168.20"
fi

Error message:

Digite 1:
321
./z.sh: linha 5: [: ==: esperado operador unário

What does the interpreter refer to when you say unary?

    
asked by anonymous 29.04.2017 / 22:01

1 answer

5

Unary operators is an "instruction" composed of two symbols, which need not necessarily be between two terms (variables, etc.).

When referring to the unary operator, the interpreter refers to its == comparison sequence that is being used in the wrong way.

In the case of your code, the error is given because of the way the operator is used. The == operator compares two Strings into ShellScript .

#!/bin/bash
echo "Digite 1:"
read TESTE # Não existe cifrão na atribuição do valor em uma variável

if [ "$TESTE" == "654" ]; then
    echo "Usage: ./pingscript.sh [network]"
    echo "example: ./pingscript.sh 192.168.20"
fi

In case of comparison of numeric values, or existence of files, etc. Specific SH operators are used for this.

  • -eq (Equality)
  • -le (Minor or Equal)
  • -lt (Less Than)
  • -ge (Greater than or equal to)
  • -gt (Higher than)
#!/bin/bash
echo "Digite 1:"
read TESTE

if [ $TESTE -eq 654 ]
    then
    echo "Usage: ./pingscript.sh [network]"
    echo "example: ./pingscript.sh 192.168.20"
fi
    
29.04.2017 / 22:22