Sweety Sweety - 2 months ago 23
Bash Question

shell script to verify docker installation on linux

I want to write a shell script which verifies whether docker is installed or not.

If docker is installed:

$ docker -v
Docker version 1.7.0, build 0baf609
$ echo $?

If docker is not installed:

$ docker -v
The program 'docker' is currently not installed. You can install it by typing:
apt-get install docker
$ echo $?

Here is my script:


docker -v
if echo $? = 128 ; then
echo "The program 'docker' is currently not installed."
echo "Continuing with dockerized way"

here for testing purpose, I ran it on the machine where docker is not installed, I kept 127 = 128, condition is wrong, so it should go in else, but still it prints The program 'docker' is currently not installed. I would like to know what I am missing here.


The correct syntax is:

if [ $? -eq 128 ]; then

To make it even more robust, you might want to check:

if [ $? -ne 0 ]; then