What is an unary operator

Asked

Viewed 7,588 times

6

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 the interpreter refers to when he says operative?

1 answer

7


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 sequence comparison == that is being misused.

In the case of your code, the error is given because of the way the operator is used. The operator == makes the comparison of two Strings in 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 the case of comparison of numerical values, or existence of files, etc. The SH specific to this.

  • -eq (Equality)
  • -le (Smaller or equal)
  • -it’s (Less than)
  • -ge (Greater or equal)
  • -gt (Greater 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
  • 1

    You may still fall into a problem in this example with the -eq if the guy doesn’t enter the test value; I don’t know, maybe it’s a pipeline where the input has already finished or he just hit enter on the keyboard. I always recommend expanding the variables with the around quotes, like [ "$TESTE" -eq 654 ]

Browser other questions tagged

You are not signed in. Login or sign up in order to post.