Simple shell script

1
2
3
4
5
6
7
#!/bin/bash
if command -v docker > /dev/null 2>&1; then
echo 'docker installed'
else
curl -fsSL https://get.docker.com | sudo bash
systemctl enable --now docker
fi

Introduction

Shell scripts are an essential part of Linux systems, providing a way to automate tasks and execute commands in a systematic and efficient manner. In this article, we will discuss the issues related to a specific shell script that checks whether Docker is installed or not, and installs it if it’s not already present.

Shell Script Overview

The script starts by declaring the shebang line #!/bin/bash, which indicates that it will be executed using the Bash shell. The next line checks whether the docker command is available by using the command utility, which searches the system path for the command and returns its status. The > /dev/null 2>&1 redirects the standard output and error messages to the null device, effectively suppressing any output from the command.

If the docker command is found, the script echoes ‘docker installed’ to the console. However, if the command is not found, the script downloads and installs Docker by executing the curl command and piping its output to the bash command with root privileges using sudo. Finally, the systemctl command is used to enable and start the Docker service.

Issues and Solutions

The above script is a simple yet useful way to ensure that Docker is installed on a Linux system. However, there are some issues that should be considered:

  1. Security risks: Downloading and executing scripts from the internet using root privileges can be risky, as it can potentially expose the system to malware or other security vulnerabilities. One solution to mitigate this risk is to download and inspect the script manually before executing it.

  2. Error handling: The script does not provide any error handling for the curl or systemctl commands, which could lead to unexpected results or failure to install Docker. One solution is to use the set -e option at the beginning of the script, which will cause it to exit immediately if any command returns a non-zero status.

  3. Compatibility issues: The script assumes that the system is running a version of Linux that supports the systemctl command. However, this may not be the case for some older distributions or non-Linux systems. In these cases, an alternative command should be used to start and enable the Docker service.

Conclusion

In summary, shell scripts are a powerful tool for automating tasks and executing commands on Linux systems. However, it’s important to consider security risks, error handling, and compatibility issues when writing and executing scripts. The shell script presented in this article is a simple yet useful example of how to install Docker if it’s not already present, but it can be improved by addressing the issues discussed above.