Checking the value of $? is not needed if you just want to act upon the exit status of the previous command.
if docker run container/myContainer:latest; then
do_stuff
fi
The above example will run/execute do_stuff
if the exit status of docker run
is zero which is a success.
You can add an else
and elif
clause in that
Or if you want to negate the exit status of the command.
if ! docker run container/myContainer:latest; then
do_stuff
fi
The above example will run do_stuff
if the exit status of docker run
is anything but zero, e.g. 1 and going up, since the !
negates.
If the command has some output and if does not have a silent/quite flag/option you can redirect it to /dev/null
if docker run container/myContainer:latest >/dev/null; then
do_stuff
fi
- Should not output anything to
stdout
- see
help test | grep -- '^[[:blank:]]*!'
- In some cases if some output is still showing then that might be
stderr
which you can silent with >/dev/null 2>&1
instead of just >/dev/null
$?
is the exit status of the last command executed.