Sweety Sweety - 18 days ago 7
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 $?
0


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 $?
127


Here is my script:

#!/bin/bash

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


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.

Answer

The correct syntax is:

if [ $? -eq 128 ]; then
...

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

if [ $? -ne 0 ]; then
...
Comments