1

I've been attempting to write a shell script to detect composer and git on a virtual linux = Ubuntu 16.0.4 machine and then install them if needed. + clone the required repository if the machine is ready for it. Now this is my first attempt to write any kind of script and also sorry if somehow I messed up the question itself, I'm quite now on stackoverflow as well.

Any help is appreciated, thanks in advance.

Here's the original task specification I received initially:

-check if git is installed on server if so, clone repo with codebase

-check if composer is installed on server if so, composer install in the root directory of the laravel application

-finally, php artisan migrate --seed

Now this is how I was trying to achieve this:

#!/bin/bash
echo "The installation process is about the begin..."


if ! which git;
    then echo "Git has been located on the destination system. Cloning begins..." 
git clone <link to the gitlabe repo>

else echo "There is no Git installed on the system. Git installation     commences..."

        sudo apt-get update
        sudo apt-get upgrade
        sudo apt-get install git

        echo "Cloning begins..."
        git clone <link to the gitlabe repo>
fi

if ! which composer;
then
    echo "Composer has been located on the destination system."
else
    echo "There is no Composer installed on the system. Composer installation commences..."
        sudo apt-get update
        sudo apt-get upgrade
        sudo apt-get install composer
fi

sudo apt-get update
sudo apt-get install curl php5-cli git
curl -sS https://getcomposer.org/installer | sudo php -- --install-    dir=/usr/local/bin --filename=composer





composer global require "laravel/installer"


sudo apt-get update

'Now the preferred version in the vagrantfile has to be edited to be 1.8.1             instead of 1.9'

'Generate a ssh key' 
ssh-keygen -t rsa -b 4096 -C "< e-mail adress that I used >"

'Start ssh agent eval $'  
ssh-agent -s

'Add your SSH private key to the ssh-agent' 
ssh-add -K ~/.ssh/id_rsa


php artisan migrate --seed

The error message I recieve:

sudo sh ./testscript.sh
[sudo] password for linuxtest: 
The installation process is about the begin...
: not foundt.sh: 3: ./testscript.sh: 
: not foundt.sh: 4: ./testscript.sh: 
: not foundt.sh: 5: ./testscript.sh: 
./testscript.sh: 72: ./testscript.sh: Syntax error: end of file unexpected (expecting "then")
rayaqin
  • 187
  • 2
  • 14
  • 1
    adding "-x" to the hashbang will give you more insight on what's happening (`#!/bin/bash -x`). For the rest, the "[" and "]" need spaces, like `if [ ! which git ]; then ...`. But that can be caused by the formatting here – Ronald Sep 01 '17 at 13:06
  • 1
    There is a lot of mistakes in your code. Try using https://www.shellcheck.net . – pacholik Sep 01 '17 at 13:06
  • I added the spaces and the "-x" but no change, seemingly – rayaqin Sep 01 '17 at 13:10
  • okay pacholik, i'm on it, thank you – rayaqin Sep 01 '17 at 13:12
  • Also please provide https://stackoverflow.com/help/mcve . Your error message sends us to line 72 which we don't have. – pacholik Sep 01 '17 at 13:15
  • pacholik, after the shellcheck this is what the if part looks like: if ! which git; then echo "Git has been located on the destination system. Cloning begins..." ;git clone < repository > ; else echo "There is no Git installed on the system. Git installation commences..."; – rayaqin Sep 01 '17 at 13:18
  • line 72 is the very last line of the code – rayaqin Sep 01 '17 at 13:19
  • If you've made changes then edit your question. – pacholik Sep 01 '17 at 13:23
  • okay i have edited the question, the same error persists after the corrections – rayaqin Sep 01 '17 at 13:27
  • I have only 54 lines. – pacholik Sep 01 '17 at 13:27
  • This looks like your file has DOS rather than UNIX newlines. This will prevent syntax like `fi` from being recognized, because it's read as `fi$'\r'`, which isn't a keyword. – Charles Duffy Sep 01 '17 at 17:12
  • Cherles Duffy's solution seems to be the one that solved it for me. Thanks. – rayaqin Sep 06 '17 at 10:42

2 Answers2

1

The answer that helped me solve my problem was posted by Charles Duffy in a comment:

This looks like your file has DOS rather than UNIX newlines. This will prevent syntax like fi from being recognized, because it's read as fi$'\r', which isn't a keyword.

rayaqin
  • 187
  • 2
  • 14
0
#!/bin/bash
echo "The installation process is about the begin...";
if [ -x "$(command -v git)" ]; then
    echo "Git has been located on the destination system. Cloning; begins..." 
    git clone <link to the gitlabe repo>;
else 
    echo "There is no Git installed on the system. Git installation commences...";
    sudo apt-get update && sudo apt-get upgrade && sudo apt-get install git;

    echo "Cloning begins...";
    git clone <link to the gitlabe repo>;
fi

if [ -x "$(command -v composer)" ]; then
    echo "Composer has been located on the destination system.";
else
    echo "There is no Composer installed on the system. Composer installation commences...";
    sudo apt-get update && sudo apt-get upgrade && sudo apt-get install composer;
fi

Hey there

I think this should fix your If condition problem. If you want to know more about how you should check for the case that a programm exists, look here: Check if a program exists from a Bash script Its the second answer for a quickfix.

Always remember to chain commands which are depending on the success of the preceding command via "&&". This secures that the next command will just be executed if the preceding doesn't fail.

I recommend doing it the same way with the ssh commands.

@edit also make sure that you end each command with a semicolon.

hope I could help.

JSStift
  • 3
  • 4
  • thank you for your kind answer, and the advices I will try to correct it as you suggested. – rayaqin Sep 01 '17 at 14:07
  • now it says end of file unexpected ( expecting : fi ) – rayaqin Sep 01 '17 at 14:16
  • Have you tried to start it like that: sudo ./testscript.sh without the sh after the sudo ? I First have to get home before i can do another analysis unfortunatley :/ – JSStift Sep 01 '17 at 14:39
  • Also try to add semicolons at the end of each which invoces a command, or chain them with "&&". – JSStift Sep 01 '17 at 14:45