【问题标题】:BASH Case statement, syntax error (unexpected end of file)BASH Case 语句,语法错误(文件非预期结尾)
【发布时间】:2012-08-27 17:03:55
【问题描述】:

我不断收到语法错误(文件意外结束)。语法对我来说看起来不错,但这里显然有问题。我希望外部人士能够提供见解。

read -p "Create default hosts file?: " yn

       case $yn in
        [Yy] )  echo "Creating default hosts file"
            sleep 1
            cat <<-EOF1 > /etc/hosts
               Do not remove the following line, or various programs that  require network functionality will fail.
               127.0.0.1    localhost.localdomain localhost

               192.168.1.1      test01
               192.168.1.2      test02
               192.168.1.3      test03
               EOF1
                if [ "$(wc -m /etc/hosts)" == "215 /etc/hosts" ] ; then
               echo -e "Default Hosts file\e[1;32m COMPLETE \e[0m"     
            else
               echo -e "Default hosts file\e[1;31m FAILED \e[0m"
               sleep 1
               echo "Please correct before continuing"
               echo "EXITING..."
               sleep 1
               exit
            fi;;
        [Nn] )  echo 'Searching "/etc/hosts" for test03 entry'
            sleep 1
            grep "test03" /etc/hosts >/dev/null
            if [ "$?" -eq 0 ] ; then
               echo "Entry found!"
               echo "Setup Continuing..."
            else
               echo '"test03" entry not found'
               sleep 1
               echo "Please correct before continuing"
               sleep 1
               echo "EXITING..."
               sleep 1
               exit
            fi;;
         *   )  echo "Please answer [Yy] or [Nn].";;
       esac

【问题讨论】:

    标签: bash syntax case


    【解决方案1】:

    此处文档标记的结尾应该是一行中除制表符之外的第一件事:

    cat <<-EOF1 > /etc/hosts
               Do not remove the following line, or various programs that  require network functionality will fail.
               127.0.0.1    localhost.localdomain localhost
    
               192.168.1.1      test01
               192.168.1.2      test02
               192.168.1.3      test03
    EOF1
    

    【讨论】:

    • 它不一定是第一件事——它之前也可以有文字制表符(不是空格),是&lt;&lt;-而不是&lt;&lt;;也就是说,考虑到区分各种空白是多么困难,这确实不是一个值得依赖的最佳实践。
    • 谢谢!结束此处文档结尾的 EOF1 在制表符后有几个空格。我试图将它与顶部标记对齐...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多