aloha
aloha

Reputation: 4784

[: -le: unary operator expected

I am writing a simple code that outputs the number of subdirectories in a directory that start with 00. Here is my code:

#!/bin/bash

maxout=2

function getnumber {
    number=`ls | grep 00 | wc -l`
    return $number
}

qs=`getnumber`
echo $qs
if [ $qs -le $maxout ]
    then
        echo 'Youpiiii !!!'
else
    echo 'Sleeping for 60 sec'
fi

However I get the following error [: -le: unary operator expected

When I trace my code, the function is working. I have

++ return 5

but

+ qs=

What am I doing wrong?

Upvotes: 4

Views: 6028

Answers (1)

ruakh
ruakh

Reputation: 183514

The `...` notation captures what ... prints, not what it returns. (return is mostly for indicating success vs. failure.)

So, change this:

number=`ls | grep 00 | wc -l`
return $number

to this:

number=`ls | grep 00 | wc -l`
echo $number

or just:

ls | grep 00 | wc -l

Upvotes: 4

Related Questions