web123456

shell determines whether grep's result contains a specific string method

Use the value of the command end code $? executed by grep to determine whether grep has been greped to a specific value.

When $? is equal to 0, it means that it has been found.

When $? is not equal to 1, it means that no found.

When $? is greater than 1, it means that the command execution error may be caused by the error of the parameter or something, causing the command to not be successfully executed.

Sample code:

echo who am i | grep -q "^Kenny$"

if [ $? -ne 0 ] ;then

    echo "grep return a none-zero value, not find Kenny"

else

    echo "grep return zero value, find Kenny"

fi