意外的 EOF 错误

Unexpected EOF error

你的名字的脚本

  #!/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,如果不是则停止程序。然后如果是询问等级,则读取等级值和returns根据等级相应的消息。

每次我 运行 我得到...

 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 ]'

如有任何帮助,我们将不胜感激!

你有几个问题。

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

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

你需要:

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

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

if[ 0 -eq "$?" ]

[是一个命令,只有当它本身就是一个单词时才会被识别为一个命令。您需要在 if[ 之间添加一个 space。

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