在linux上运行.sh脚本时出现语法错误。我究竟做错了什么?

时间:2017-09-01 12:58:24

标签: linux shell virtual-machine unexpected-token

我一直在尝试编写一个shell脚本来检测虚拟linux = Ubuntu 16.0.4计算机上的composer和git,然后根据需要安装它们。 +如果机器已准备好,请克隆所需的存储库。 现在这是我第一次尝试编写任何类型的脚本,并且如果我以某种方式搞砸了问题本身,也很抱歉,我现在也正在使用stackoverflow。

提前感谢任何帮助。

这是我最初收到的原始任务规范:

- 检查服务器上是否安装了git 如果是这样,克隆repo与代码库

-check如果在服务器上安装了composer 如果是这样,composer安装在laravel应用程序的根目录中

- 最后,php artisan migrate --seed

现在我正在努力实现这一目标:

#!/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

我收到的错误消息:

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")

2 个答案:

答案 0 :(得分:1)

帮助我解决问题的答案由Charles Duffy发表评论:

这看起来像你的文件有DOS而不是UNIX换行符。这样可以防止识别像fi这样的语法,因为它被理解为fi $&#39; \ r&#39;这不是一个关键字。

答案 1 :(得分: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

嘿那里

我认为这应该可以解决你的If问题。如果您想了解更多关于如何检查程序存在的情况,请查看: Check if a program exists from a Bash script 它是quickfix的第二个答案。

始终记住通过&#34;&amp;&amp;&#34;链接取决于前一命令成功的命令。这确保了如果前一个命令失败,将执行下一个命令。

我建议使用ssh命令以相同的方式执行此操作。

@edit 还要确保用分号结束每个命令。

希望我能帮忙。