user767990
user767990

Reputation: 115

Why have a “integer expression expected”?

Why I have a integer expression expected error with this:

at=`echo $1 | grep -q "@"`
if [ $at -ne 0 ]; then
    echo "blabla"
else
    echo "bloblo"
fi

$at is set, and the test working fine outside the script

Upvotes: 3

Views: 8477

Answers (2)

glenn jackman
glenn jackman

Reputation: 247012

When testing the result of grep -q, you want to test $? not the output of grep, which will be empty

at=$(echo "$1" | grep -q "@")
if [ $? -ne 0 ]; then ...

or simply

if echo "$1" | grep -q "@"; then ...

or, more bash-ly

if grep -q "@" <<< "$1"; then ...

or, without calling grep:

if [[ "$1" == *@* ]]; then ...

or

case "$1" in
  *@*) echo "match" ;;
  *) echo "no match" ;;
esac

Upvotes: 4

Rafe Kettler
Rafe Kettler

Reputation: 76965

-ne is for comparing integers. Use != to compare strings.

Upvotes: 3

Related Questions