【发布时间】:2017-09-01 12:58:23
【问题描述】:
我一直在尝试编写一个 shell 脚本来检测虚拟 linux = Ubuntu 16.0.4 机器上的 composer 和 git,然后在需要时安装它们。 + 如果机器准备好,则克隆所需的存储库。 现在这是我第一次尝试编写任何类型的脚本,如果我把问题本身搞砸了,我也很抱歉,我现在也在 stackoverflow 上。
任何帮助表示赞赏,在此先感谢。
这是我最初收到的原始任务规范:
-检查服务器上是否安装了 git 如果是这样,请使用代码库克隆 repo
-检查作曲家是否安装在服务器上 如果是这样,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")
【问题讨论】:
-
在 hashbang 中添加“-x”将使您更深入地了解正在发生的事情 (
#!/bin/bash -x)。对于其余部分,“[”和“]”需要空格,例如if [ ! which git ]; then ...。但这可能是由此处的格式引起的 -
你的代码有很多错误。尝试使用shellcheck.net。
-
我添加了空格和“-x”,但似乎没有变化
-
好的,pacholik,我同意了,谢谢
-
另外请提供stackoverflow.com/help/mcve。您的错误消息将我们发送到我们没有的第 72 行。
标签: linux shell virtual-machine unexpected-token