【问题标题】:Unexpected EOF error意外的 EOF 错误
【发布时间】:2015-11-23 23:11:05
【问题描述】:

你的名字的脚本

  #!/bin/bash
 echo "what is your name?"
 read name
 if test "$name" = "Daryl"
     then
     echo "Hey, how are you?"
 else
      echo "sorry, im looking for Daryl"
 fi

你的成绩脚本

  #!/bin/bash

 ./yourname
 if[ 0 -eq "$?" ]
      then
      exit 0
 else
 echo "what is your grade?"

      read grade
      if [ "$grade" -gt 90 ]
      then 
      echo "Awesome! You got an A"

           elif [ "$grade -le 90 ] && [ "$grade" -gt 80 ]
           then
           echo "Good! You got a B"

               elif [ "$grade" -lt 80 ];
               then 
          echo "You need to work harder!"

 fi

我正在尝试获取它,以便在 yourGrade 脚本中使用你的名字检查它是否是 Daryl,如果不是则停止程序。然后如果是询问成绩,则读取成绩值并根据成绩返回相应的消息。

每次我运行它我都会得到......

 root@kali:~# . yourGrade
 What is your name?
 >Daryl
 Hey how are you!
 -bash : yourGrade: line 17: syntax error near unexpected token 'elif'
 -bash : yourGrade: line 17:'        elif [ "$grade" -le 90 ] && [ "$grade" -gt 80 ]'

任何帮助将不胜感激!

【问题讨论】:

  • 感谢您的帮助,我是这方面的新手,所以这真的很有帮助!

标签: linux unix scripting


【解决方案1】:

你有几个问题。

出现'unexpected EOF'问题是因为你有一个不匹配的双引号:

elif [ "$grade -le 90 ] && [ "$grade" -gt 80 ]

你需要:

elif [ "$grade" -le 90 ] && [ "$grade" -gt 80 ]

一旦你解决了这个问题,你就会遇到以下问题:

if[ 0 -eq "$?" ]

[ 是一个命令,只有当它本身是一个单词时才被识别为命令。 if[ 之间需要有一个空格。

if [ 0 -eq "$?" ]

那么你会有一个问题,因为你的缩进不稳定,事实上你有两个if 语句(一个嵌套在另一个里面),只有一个fi;打你!

最后请注意,那些得分正好为 80 分的人不会被告知他们的评分。

#!/bin/bash

./yourname
if [ 0 -eq "$?" ]
then
    exit 0
else
    echo "what is your grade?"

    read grade
    if [ "$grade" -gt 90 ]
    then 
        echo "Awesome! You got an A"
    elif [ "$grade" -le 90 ] && [ "$grade" -gt 80 ]
    then
        echo "Good! You got a B"
    elif [ "$grade" -lt 80 ];
    then 
        echo "You need to work harder!"
    else
        echo "You scored 80; that's only barely acceptable"
    fi
fi

【讨论】:

  • “见鬼去吧”?
猜你喜欢
  • 2013-01-29
  • 1970-01-01
  • 2018-08-06
  • 1970-01-01
  • 2014-03-18
  • 2013-07-13
  • 2018-04-18
  • 1970-01-01
相关资源
最近更新 更多