在Linux上运行.sh脚本时出现语法错误。我究竟做错了什么?
我一直在试图编写一个shell脚本来检测虚拟linux = Ubuntu 16.0.4机器上的作曲家和混帐,然后在需要时安装它们。 +如果机器准备好了,则克隆所需的存储库。 现在,这是我第一次尝试编写任何类型的脚本,也很抱歉,如果我以某种方式搞砸了这个问题本身,我现在也在使用stackoverflow。在Linux上运行.sh脚本时出现语法错误。我究竟做错了什么?
任何帮助表示赞赏,在此先感谢。
这是我最初接收到的原始任务说明:
-check如果要是这样安装在服务器 混帐,克隆回购与代码库
-check如果要是这样安装在服务器 作曲家,作曲安装在laravel应用程序的根目录
- 最后,PHP工匠迁移--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")
,帮助我解决我的问题被张贴查尔斯·达菲在评论答案:
这看起来像你的文件有DOS,而不是UNIX换行。这将防止识别fi这样的语法,因为它被读作fi $'\ r',这不是关键字。
#!/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
嘿
我想这应该解决您如果条件问题。如果你想知道更多关于如何检查程序存在的情况,请看这里: Check if a program exists from a Bash script 它是quickfix的第二个答案。
一定要记住通过“& &”取决于上述命令成功的链接命令。这保证了如果前面的命令没有失败,就会执行下一个命令。
我建议使用ssh命令以相同的方式执行此操作。
@edit 还确保您以分号结尾每个命令。
希望我能帮上忙。
感谢您的答复,并提供建议 我会尽力按照您的建议进行更正。 –
现在它说文件结束意想不到(期待:fi) –
你试过像这样启动它:sudo ./testscript.sh sudo后没有sh?我首先必须先回家,然后才能做另一个分析unfortunatley:/ – JSStift
在hashbang中添加“-x”可以让你更深入地了解正在发生的事情('#!/ bin/bash -x')。其余部分,“[”和“]”需要空格,如'if [!哪个git];那么...'。但是这可能是由此处的格式造成的 – Ronald
代码中存在很多错误。尝试使用https://www.shellcheck.net。 – pacholik
我添加了空格和“-x”但没有变化,看起来好像是 –