0

please help me with this.

I am doing a grep test file.txt and I want to catch the whole value and print it.I am doing this,

grep test file.txt
if [ "$?" -eq 0]
then
echo success
else
echo fail
fi

In this code, whether the grep returns a value or not always the exit code is 0 as the command is successful.

I do not want to do the following as my actual code has repetitive greps, say 10 times and it is not working.

a=`grep test file.txt`
if [ "$a" -eq "" ]
then
echo fail
else
echo success
fi

I am looking for a simple solution like this,

echo $(anything) should catch the value.

  • Are you doing like foo | grep bar | grep baz and you need to know if an arbitrary grep failed/succeeded (set -o pipefail), or you want to save the $? return value you got from grep and return it after echoing your success/fail message (foo=$?; if [ "$foo" -eq 0 ]; then ...)? – drewbenn Feb 4 '16 at 18:26
4

Your results don't match mine

echo 'some test here' > file.txt
grep test file.txt
echo $?    # returns 0

echo 'something else here' > file.txt
grep test file.txt
echo $?    # returns 1

Furthermore, when I run your own complete code example, I get the "success" or "fail" according to whether or not the keyword exists in the file. (I've added the missing space between the 0 and ] because otherwise you would be getting the error, -bash: [: missing `]' and you didn't report that.)

grep test file.txt
if [ "$?" -eq 0 ]
then
    echo success
else
    echo fail
fi

However, as a style suggestion I would test the grep command directly, like this

if grep test file.txt
then
    echo success
else
    echo fail
fi

It's just struck me that you might want to be using the return value from the grep after your if...fi block. In that case just save the value and continue on:

grep test file.txt
ss=$?
if [ 0 -eq $ss ]
then
    echo success
else
    echo fail
fi
# Here $ss still contains the return value from grep
2

Your original code should work as expected if you add a space between the 0 and the ].

grep test file.txt
if [ "$?" -eq 0 ]
then
echo success
else
echo fail
fi
0
grep test file.txt
if [ "$?" -eq 0]
then
  echo success
  exitcode=0
else
  echo fail
  exitcode=1 
fi
exit ${exitcode}

I am not sure what you are looking for but if you want your script to exit with the same code as your grep command did, this should work for you.

If you are looking for something else, you should try to explain yourself better.

0

Could pipe through awk instead of relying on the return code.

grep posix test_file.txt|awk 'BEGIN{matches=0}{matches++}END{if(matches>0){print "success";}else{print "fail";}}'

Print only the first match:

grep testvalue test_file.txt|awk 'BEGIN{matches="";}{matches=$0;}END{if(matches!=""){print "success";print matches;}else{print "fail";}}'

And with the value catching feature where every grep match is shown:

grep testvalue test_file.txt|awk 'BEGIN{matches="";}{matches=matches $0 "\n";}END{if(matches!=""){print "success";print matches;}else{print "fail";}}'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.