linkyndy
linkyndy

Reputation: 17900

Check number of lines returned by bash command

I have a command similar to this:

LIST=$(git log $LAST_REVISION..$HEAD --format="%s" | egrep -o "[A-Z]-[0-9]{1,4}" | sort -u)

Now, I need to do something if $LIST returned zero or more lines. Here's what I've tried:

if [ ! $($LIST | wc -l) -eq 0 ]; then
    echo ">0 lines returned"
else
    echo "0 lines returned"
fi

But it throws me an error. What's the correct syntax of doing this (with some details on the syntax used, if possible)?

Upvotes: 0

Views: 218

Answers (1)

rici
rici

Reputation: 241671

To check whether a variable is empty, use test -z, which can be written several ways:

test -z "$LIST"

[ -z "$LIST" ]

with bash (or many other "modern" shells):

[[ -z $LIST ]]

I prefer the last one, as long as you're using bash.

Note that what you are doing: $($LIST | ...) is to execute $LIST as a command. That is almost certain to create an error, and guaranteed to do so if $LIST is empty.

Upvotes: 3

Related Questions