Bash Script with Several Nested elif Statements Not Working - bash

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:
Verify that Apache2 is installed and Running
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.

First of all, can you rewrite it like this?
Please tell me the execution result.
This is wrong.
if [pgrep -x "apache2" > /dev/null]; then
This is correct.
pgrep -x "apache2" > /dev/null
if [ $? -eq 0 ]; then

Related

chmod +x cant find build.sh file

I have been trying to make a VCS in C++ but the build file is not running in my LINUX(Ubuntu).
It is prompting the above message.
my build file is as follows:
#!/bin/bash
sudo apt-get update
udo apt-get install openssl -y
sudo apt-get install libssl-dev -y
mkdir -p ~/imperium/bin
cp imperium.sh ~/imperium
cd ..
make
cd ~/imperium/bin || echo "error"
chmod +x main
cd ..
if grep -q "source $PWD/imperium.sh" "$PWD/../.bashrc" ; then
echo 'already installed bash source';
else
echo "source $PWD/imperium.sh" >> ~/.bashrc;
fi
my imperium.sh file is also as follows:
function imperium(){
DIR=$PWD
export dir=$DIR
cd ~/imperium/bin || echo "Error"
./main "$#"
cd "$DIR" || echo "Error"
}
I will be heavily obliged if any one can solve this problem of mine. After chmod I have been doing:
./build.sh but its prompting that build.sh file does not exists.
For me it seems you have a typo right in the 3rd row "udo" ->
"sudo".
Also, You should avoid using cd .. and use relative paths for
the commands.

Is the Syntax for the Bash Script right for an if elif statement

I added another condition to my if, elseif condition for my bash shell script. I am new to shell scripting, can you guys review my code if my syntax for if conditions are right especially the "fi" implementation. Much appreciated.
if [ -f /etc/system-release ] && grep Amazon /etc/system-release > /dev/null; then
cd /tmp
sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
else
# we're either RedHat or Ubuntu
DISTRIBUTOR=`lsb_release -is`
DISTRIBUTOR2=`lsb_release -cs`
if [ "trusty" == $DISTRIBUTOR2 ]; then
cd /tmp
wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
sudo dpkg -i amazon-ssm-agent.deb
sudo start amazon-ssm-agent
elif [ "RedHatEnterpriseServer" == $DISTRIBUTOR ]; then
cd /tmp
sudo yum install -y https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/linux_amd64/amazon-ssm-agent.rpm
elif [ "xenial" == $DISTRIBUTOR2 ]; then
cd /tmp
wget https://s3.amazonaws.com/ec2-downloads-windows/SSMAgent/latest/debian_amd64/amazon-ssm-agent.deb
sudo dpkg -i amazon-ssm-agent.deb
sudo systemctl enable amazon-ssm-agent
fi
fi
sleep 10
Looks basically ok, but https://www.shellcheck.net/ has a couple of comments that you should probably address.

How do I uninstall docker-compose?

I installed docker-compose by following the official documentation: http://docs.docker.com/compose/install/
Now I want to uninstall docker-compose.
$ docker-compose -h
offers no command for uninstalling, nor does the official documentation offer any instructions.
I deleted my docker-compose.yml file and /usr/local/bin/docker-compose, but I want to make sure that's everything.
I'm using OSX Yosemite 10.10.3 on a MacbookPro.
EDIT: Regarding the installation instructions I followed, I didn't use pip. I used the documented curl command:
$ curl -L https://github.com/docker/compose/releases/download/1.3.1/docker-compose-`uname -s`-`uname -m` > /usr/local/bin/docker-compose
$ chmod +x /usr/local/bin/docker-compose
Documenation
Please note that this is now in the docs.
Coupled Installation and Removal
Note: on Mac Docker now installs Docker Compose. So the strategy for removal has changed a bit. If you uninstall Docker, and you want to uninstall both, then you also uninstall Docker Compose.
Individual Removal if Installed Using curl
It is commonly installed to /usr/local/bin/docker-compose on macs. However, you can run which docker-compose to find the exact location.
Run the following command (*nix systems) to remove:
rm $(which docker-compose)
If you get a permission denied error then you will need to prepend sudo:
sudo rm $(which docker-compose)
To verify that it was successful, run the following command which should return nothing:
which docker-compose
It should say that the command wasn't found.
Individual Removal if Installed Using PIP
If you've installed Docker Compose using PIP then you can run:
pip uninstall docker-compose
You may have to use sudo if you get a permission denied error:
sudo pip uninstall docker-compose
first get docker path by:
which docker-compose
then it will return path like:/usr/bin/docker-compose
then remove it:
sudo rm -rf /usr/bin/docker-compose
Nowadays docker-compose is part of the docker toolbox.
If you want to remove everything that comes with the Docker Toolbox (including Docker itself).
You can execute this shell script:
#!/bin/bash
# Uninstall Script
if [ "${USER}" != "root" ]; then
echo "$0 must be run as root!"
exit 2
fi
while true; do
read -p "Remove all Docker Machine VMs? (Y/N): " yn
case $yn in
[Yy]* ) docker-machine rm -f $(docker-machine ls -q); break;;
[Nn]* ) break;;
* ) echo "Please answer yes or no."; exit 1;;
esac
done
echo "Removing Applications..."
rm -rf /Applications/Docker
echo "Removing docker binaries..."
rm -f /usr/local/bin/docker
rm -f /usr/local/bin/docker-machine
rm -r /usr/local/bin/docker-machine-driver*
rm -f /usr/local/bin/docker-compose
echo "Removing boot2docker.iso"
rm -rf /usr/local/share/boot2docker
echo "All Done!"
If you still have the depreciated Boot2docker and you want to get rid of it as well.
You can uninstall it by executing the following shell script:
#!/bin/bash
# Uninstall Script
if [ "$(which boot2docker)" == "" ]; then
echo "boot2docker does not exist on your machine!"
exit 1
fi
if [ "${USER}" != "root" ]; then
echo "$0 must be run as root!"
exit 2
fi
echo "Stopping boot2docker processes..."
boot2docker stop && boot2docker delete
echo "Removing boot2docker executable..."
rm -f /usr/local/bin/boot2docker
echo "Removing boot2docker ISO and socket files..."
rm -rf ~/.boot2docker
rm -rf /usr/local/share/boot2docker
echo "Removing boot2docker SSH keys..."
rm -f ~/.ssh/id_boot2docker*
echo "Removing boot2docker OSX files..."
rm -f /private/var/db/receipts/io.boot2docker.*
rm -f /private/var/db/receipts/io.boot2dockeriso.*
echo "Removing Docker executable..."
rm -f /usr/local/bin/docker
echo "All Done!"
You can also just:
sudo yum remove docker-compose-plugin
on RPM-based like Centos
according to: https://docs.docker.com/compose/install/uninstall/
I would use pip uninstall docker-compose

Unable to install parse.com command line tool on Mac OSX 10.10 Yosemite

Running the command
curl -s https://www.parse.com/downloads/cloud_code/installer.sh | sudo /bin/bash
does not install the tool
I was able to install it easily on my other computer running 10.9.2
STEP : 1
Make a copy of this
#!/bin/bash
TMP_FILE=/tmp/parse.tmp
if [ -e /tmp/parse.tmp ]; then
echo "Cleaning up from previous install failure"
rm -f /tmp/parse.tmp
fi
echo "Fetching latest version ..."
curl --progress-bar https://www.parse.com/downloads/cloud_code/parse -o /tmp/parse.tmp
if [ ! -d /usr/local/bin ]; then
echo "Making /usr/local/bin"
mkdir -p /usr/local/bin
fi
echo "Installing ..."
mv /tmp/parse.tmp /usr/local/bin/parse
chmod 755 /usr/local/bin/parse `
to a file named install.sh and run it in your terminal as bash install.sh. This will install you parse in your Terminal.
STEP :2
Download the Gist from this link and run the file named install.sh in your Terminal preceded by bash

mkdir always creates a file instead a directory

First I want to say that I don't really know what I should look for, here in Stack Overflow and what could be a good query for my problem.
In simple words I want to create a new directory and than do some file operations in it. But with the script that I have crafted I got always a file instead of a directory. It seems to be absolutely regardless how I stick the code together there is always the same result. I hope tat masses can help me with their knowledge.
Here is the script:
#!/bin/bash
DLURL=http://drubuntu.googlecode.com/git'
d7dir=/var/www/d7/'
dfsettings=/var/www/d7/sites/default/default.settings.php
settings=/var/www/d7/sites/default/settings.php
#settiing up drush
drush -y dl drush --destination=/usr/share;
#Download and set up drupal
cd /var/www/;
drush -y dl drupal;
mkdir "$d7dir"; #this is the line that always produces a file instead a directory
# regardless if it is replaced by the variable or entered as
# /var/www/d7
cd /var/www/drup*;
cp .htaccess .gitignore "$d7dir";
cp -r * "$d7dir";
cd "$d7dir";
rm -r /var/www/drup*;
mkdir "$d7dir"sites/default/files;
chmod 777 "$d7dir"sites/default/files;
cp "$dfsettings" "$settings";
chmod 777 "$settings";
chown $username:www-data /var/www/d7/.htaccess;
wget -O $d7dir"setupsite $DLURL/scripts/setupsite.sh; > /dev/null 2>&1
chmod +x /var/www/setupsite;
echo "Login Details following...";
read -sn 1 -p "Press any key to continue...";
bash "$d7dir"setupsite;
chown -Rh $username:www-data /var/www;
chmod 644 $d7dir".htaccess;
chmod 644"$settings";
chmod 644"$dfsettings";
exit
I hope someone got the reason for that.
There are many way to debug a shell-scripting.
Add set -x in your beginning script
Get the return value.
mkdir 'the-directory'
ret=$?
if test $ret -eq 0; then
echo 'Create success.'
else
echo 'Failed to create.'
fi
Set to verbose mode $ mkdir -v 'the-directory'
Try this command $ type mkdir, to checking mkdir command.

Resources