20

I need to check in a shell script if Docker is installed (Ubuntu server).

I came up with this, but the syntax is not correct.

if [[ which docker && docker --version ]]; then
  echo "Update docker"
  # command
else
  echo "Install docker"
  # command
fi

I also tried if [ which docker ] && [ docker --version ]; then

0

2 Answers 2

35

Using suggestions from the answer in rickdenhaan's comment:

if [ -x "$(command -v docker)" ]; then
    echo "Update docker"
    # command
else
    echo "Install docker"
    # command
fi
Sign up to request clarification or add additional context in comments.

4 Comments

What does -x do? For me if command -v docker; then is working.
It tests if a file exists and is executable. The command command returns the docker command's location, then the -x flag tests that it there and able to execute.
The brackets are an alias the the test command. Run man test to see all the flags and their descriptions.
Is there any reason why command -v docker would be preferred over which docker ?
8

This one is working for me:

if [[ $(which docker) && $(docker --version) ]]; then
    echo "Update docker"
    # command
  else
    echo "Install docker"
    # command
fi

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.