I am trying to get the exit status of grep and test a condition with it, But it does not seem to be working as expected since i am doing something wrong apparently
as per grep help
Exit status is 0 if match, 1 if no match,
and 2 if trouble.
My problem is something like this
templine - a string which has isVoid()
isVoid=`echo $tempLine | grep -o "isVoid\(\)"`
echo "isVoid: " $isVoid
if [ $isVoid = 0 ]
then
echo "Is Void matched" $isVoid
fi
When I echo $isVoid, i get the matched string, but i am not sure how to test the exit status of grep. In this case the if condition is not satisfied
Assigning the substituted output of a command to a variable will simply contain it's output, ie. the pattern grep -o found or not. It does not contain the return code of the grep!
Use Franklins example since it is the only one of that cares for the exit code which is checked by $? immediately after the command you want to observe.
It doesn't have anything to do with the type of how it is substituted ie. double `or $().
The variable contains the output but the return code corresponds to the return code of the last command (grep). Try to print $? and you'll see what happens.
Right, it just makes it more readable and permits the nesting.