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

Syntax error when running .sh script on linux. What am I doing wrong?

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

感谢任何帮助,提前致谢。

这是我最初收到的原始任务说明:

-检查服务器上是否安装了git 如果是这样,请使用代码库

克隆回购协议

-检查服务器上是否安装了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")
#!/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 这是快速修复的第二个答案。

永远记住通过“&&”链接取决于前面命令成功的命令。这确保了如果前面的命令没有失败,下一个命令将被执行。

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

@编辑 还要确保以分号结束每个命令。

希望能帮到你。

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

这看起来你的文件有 DOS 而不是 UNIX 换行符。这将阻止像 fi 这样的语法被识别,因为它被读作 fi$'\r',这不是关键字。