3

I am pretty new to Bash scripting and currently, working on creating an installer to install a Docker client with Docker-Compose, according to the user's OS. This script is not planned to work on each OS, the scope of this is only for Ubuntu 16.04, 18.04, CentOS 7 and 8.

I have written the below code to verify the user's OS and start the installation process (as for now, I'm trying to make it work for Ubuntu 18.04):


################
### Check OS ###
################

if [ -f /etc/os-release ]; then
    # freedesktop.org and systemd
    . /etc/os-release
    OS=$NAME
    VER=$VERSION_ID
    RESULT="$OS $VER"
    printf -v $RESULT "Ubuntu 18.04"
elif type lsb_release >/dev/null 2>&1; then
    # linuxbase.org
    OS=$(lsb_release -si)
    VER=$(lsb_release -sr)
elif [ -f /etc/lsb-release ]; then
    # For some versions of Debian/Ubuntu without lsb_release command
    . /etc/lsb-release
    OS=$DISTRIB_ID
    VER=$DISTRIB_RELEASE
elif [ -f /etc/debian_version ]; then
    # Older Debian/Ubuntu/etc.
    OS=Debian
    VER=$(cat /etc/debian_version)
elif [ -f /etc/SuSe-release ]; then
    # Older SuSE/etc.
    ...
elif [ -f /etc/redhat-release ]; then
    # Older Red Hat, CentOS, etc.
    ...
else
    # Fall back to uname, e.g. "Linux <version>", also works for BSD, etc.
    OS=$(uname -s)
    VER=$(uname -r)
fi

echo $RESULT

#################################
### Ubuntu 18.04 Installation ###
#################################

if [ $RESULT = "Ubuntu 18.04" ]; then

    echo "Installing on Ubuntu 18.04"
    sudo apt update
    sudo apt install apt-transport-https ca-certificates curl software-properties-common -y
    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
    sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu bionic stable"
    sudo apt update
    apt-cache policy docker-ce
    sudo apt install docker-ce
    sudo systemctl status docker
    sudo curl -L https://github.com/docker/compose/releases/download/1.21.2/docker-compose-`uname -s`-`uname -m` -o /usr/local/bin/docker-compose
    sudo chmod +x /usr/local/bin/docker-compose
    docker-compose --version

else
    echo "not working"
fi 

Running the above on Ubuntu 18.04 results with:

Ubuntu 18.04                                
./test.sh: line 46: [: too many arguments   
not working

How can I compare the output of the $RESULT to a string? Any pointers or recommendations are more than welcome!

Dave Pinhas
  • 33
  • 1
  • 4
  • You should almost always double-quote variable references in shell scripts; this is just one of the many problems that leaving them unquoted can cause. BTW, your `printf -v $RESULT ...` command is also almost certainly wrong. Are you trying to assign to `RESULT`? If so, do not use `$` there -- that *gets* the value of the variable. – Gordon Davisson Mar 31 '20 at 01:20

1 Answers1

5

$RESULT contains whitespaces, quote it to avoid word splitting:

if [ "$RESULT" = "Ubuntu 18.04" ]; then
Arkadiusz Drabczyk
  • 11,227
  • 2
  • 25
  • 38