How can I check the exit status using an ‘ if’ statement in a shell script?
In shell scripts, the exit status of a last executed command is stored in the special variable $?. The exit status is 0 for success and on-zero for failure. You can check the exit status in an if statement like so:
if [ $? -eq 0 ]; then echo “Success”
else echo “Failure”
fi: Checks if the previous command succeeded or failed.
To check the exit status using an ‘if’ statement:
Directly check the exit status of a command using this method :
`$?` variable:
If [ $? -eq 0 ]; then
echo “Success”
else
echo “Failure”