0

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.

1
  • That will only test that the docker client is installed. If you want to ensure that the daemon is also running, you'll need to use docker version instead of just -v. Commented Nov 14, 2016 at 21:43

1 Answer 1

2

The correct syntax is:

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

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

if [ $? -ne 0 ]; then
...
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.