vendredi 13 septembre 2019

Bash Script with Several Nested elif Statements Not Working

I am trying to create a script I can provide to those who will use an academic game I am making. I am trying to do the following:

  1. Verify that Apache2 is installed and Running
  2. If Apache2 is installed, move a folder containing the website files to /var/www/html while backing up apache's original index.html

The code is as follows:

#!/bin/sh
acm=[]
cnn=[]
gnu=[]
ieee=[]
if [pgrep -x "apache2" > /dev/null]; then
    echo "Apache 2 Installed and Running!"
    if [ -d "$HOME/acmDL" ]; then
        sudo mkdir /var/www/bak
        sudo mv /var/www/html/index.html /var/www/bak
        sudo mv acmDL /var/www/html/
        cd /var/www/html/
        sudo mv acmDL/* .
        exit
    elif [ -d "$HOME/cnnDL" ]; then
        sudo mkdir /var/www/bak
        sudo mv /var/www/html/index.html /var/www/bak
        sudo mv cnnDL /var/www/html/
        cd /var/www/html/
        sudo mv cnnDL/* .
        exit
    elif [ -d "$HOME/gnuDL" ]; then
        sudo mkdir /var/www/bak
        sudo mv /var/www/html/index.html /var/www/bak
        sudo mv gnuDL /var/www/html/
        cd /var/www/html/
        sudo mv gnuDL/* .
        exit
    elif [ -d "$HOME/ieeeDL" ]; then
        sudo mkdir /var/www/bak
        sudo mv /var/www/html/index.html /var/www/bak
        sudo mv ieeeDL /var/www/html/
        cd /var/www/html/
        sudo mv ieeeDL/* .
        exit
    else
        echo "Provided websites not found... Are you using a custom website?"
    fi
else
    echo "Please check apache2... It may not have installed correctly"
fi

The error I keep getting is syntax error near unexpected token `elif' on line 15.

As you can see, I even tried moving the boolean expression [ - d "$HOME/site" ] to their own variables, but then the error becomes : -d: command not found and the error on line 15.

Is what I am trying to do impossible, or am I missing something undocumented and yet completely obvious (like a handful of my previous posts)?

This is being run on a minimal installation of Ubuntu 18 on a Virtual Machine. The site directories are shared by Filezilla. Script written in Notepad++ on Windows 7 x64.

Aucun commentaire:

Enregistrer un commentaire