user3529850
user3529850

Reputation: 976

Bash test - unary operator expected

I have the following check. I try to call the isValid function that takes 2 arguments.

if [ $(isValid $road $number) -eq 0 ] ; then
(...)

In return I get:

[: -eq: unary operator expected

In that case I would like to use [] over [[]]. I escaped arguments with "" but it didn't work. What am I missing ?


EDIT:

function isValid{
    local road_name=$1
    local road_number=$2

    [[ $road_number == "lombard" && $road_number == "10" ]] && return 0
    return 1
}

Upvotes: 1

Views: 619

Answers (1)

KamilCuk
KamilCuk

Reputation: 141060

if [ $(isValid $road $number) -eq 0 ] ; then

This checks if the standard output is equal to 0. Ex.:

if [ $(echo 0) -eq 0 ] ; then

echo 0 outputs 0. Then you check with the [ command, if the output of the command echo 0 is equal to 0. The [ command returns zero status, if the expression is true. It returns non-zero status, if the expression is false.

You want to check exit status of a command. Just:

if isValid "$roud" "$number"; then

The if expression is run, when the exit status of a command is zero. You can use ! command to invert the exit status.

Upvotes: 1

Related Questions