1

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

任何帮助表示赞赏,在此先感谢。

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

- 检查 git 是否安装在服务器上,如果是,则使用代码库克隆 repo

- 检查 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")
4

2 回答 2

1

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

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

于 2018-02-20T12:51:06.307 回答
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 的第二个答案。

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

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

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

希望我能帮上忙。

于 2017-09-01T13:46:14.403 回答