3

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

2 Answers 2

4

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
Sign up to request clarification or add additional context in comments.

1 Comment

When testing the result of grep -q, you want to test $? : EXACTLY Thanks for all the way you purpose, I think I learn something today :) particulary like the "no grep" syntax. And thanks by the way
3

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

1 Comment

Thanks a lot man ! it's working fine, but it needed [ "$at" != 0 ]; Thanks for the very quick reply :) –

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.