【问题标题】:Syntax error when running .sh script on linux. What am I doing wrong?在 linux 上运行 .sh 脚本时出现语法错误。我究竟做错了什么?
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    #!/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 命令以同样的方式执行此操作。

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

    希望我能帮上忙。

    【讨论】:

    • 感谢您的友好回答,我会按照您的建议尝试纠正它。
    • 现在它说文件结尾意外(期待:fi)
    • 您是否尝试过这样启动它: sudo ./testscript.sh 在 sudo 之后没有 sh ?不幸的是,我首先必须回家才能进行另一次分析:/
    • 也尝试在每个调用命令的末尾添加分号,或者用“&&”链接它们。
    猜你喜欢
    • 2022-06-10
    • 2014-06-15
    • 1970-01-01
    • 2020-01-21
    • 2013-08-06
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    • 2021-04-13
    相关资源
    最近更新 更多