Automation, Orchestration, and Scripting
A script contains these lines: grep -q "ERROR" app.log echo "Search complete" if [ $? -eq 0 ]; then echo "Errors found"; fi The script prints "Errors found" even when app.log contains no errors. What is the cause?
Show the answer
Correct answerB. $? holds the exit status of echo, not grep, and echo returned 0
$? always holds the status of the most recently completed command, which here is echo, and it succeeded. grep -q does return nonzero when nothing matches, but that value was overwritten before the test ran. Capturing $? right after grep fixes it.