shell编程实验报告报错,elif 附近的语法错误

今天用Linux的shell语言写模拟考勤系统时遇到如下错误

[root@master ~]# chmod +x testshell
[root@master ~]# ./testshell
./testshell:行70: 未预期的符号 `elif' 附近有语法错误
./testshell:行70: `         elif test "$fpassword" != "$password"'

将所有代码都检查了一遍发现并无问题,一开始不知道是不是Tab键和空格混用导致的错误,但重新敲还是这样。
附上全部代码:

#! /bin/bash
#filename:shelltest
exsig=0
while true; do
        echo ""
        echo "---欢迎使用本系统:---"
        echo "    1.上班签到"
        echo "    2.下班签出"
        echo "    3.考勤信息查询"
        echo "    4.退出系统"
        echo "----------------------"
        echo ""
        echo "请输入你的选项:"
        read choice
        case $choice in
          1)echo "请输入你的名字:"
            read name
            echo "请输入你的密码:"
            read password
            if test-r/home/user/userinfo.dat
            then
                while read fname fpassword
        do
                   echo "$fname"
                   echo "$fpassword"
        if test "$fname" = "$name"
                   then
                      break
                   fi
                  done < /home/user/userinfo.dat
                else
                  echo System Error:userinfo.dat does not exist!
                fi
                if test "$fname”" != "$nane"
                then
                    echo "不存在该用户!"
                elif test "$fpassword" != "$password"
                then
                    echo "密码不正确!"
                else
                    hour=`date +%H`
                    if test "$hour" -gt 8
                    then
                        echo "你迟到了!"
                        echo "$name 上班迟到---日期:'date'" >> /home/user/check.dat
                    else
                        echo "早上好,$name!”
                    fi
                fi
          ;;
          2)echo "请输入你的名字:"
            read name
            echo "请输入你的密码:"
            read password
            if test-r/home/user/userinfo.dat
            then
                while read fname fpassword
                do
                  if test "$fname" = "$name"
                  then
                     break 
                  fi
                done < /home/user/userinfo.dat
             else
                echo System Error: userinfo.dat does not exist!
             fi
             if test "$fname" != "$name"
             then
                echo ”不存在该用户!"
                elif test "$fpassword"!= "$password"
             then
                echo "密码不正确!"
             else
                hour='date +%H'
                if test "$hour"-lt 18
                then
                   echo "你早退了!"
                   echo "$nane  下班早退---日期:'date' ">> /home/user/check.dat
                else
                   echo "再见,$name!"
                fi
             fi
          ;;
          3)echo "请输入你的名字:"
            read name
            echo "请输入你的密码:"
            read password
            if test-r/home/user/userinfo.dat
            then
            while read fnane fpassword
              do
                if test "$fname" = "$name"
                then
                fi
              done < /home/user/userinfo.dat
         else
              echo System Error:userinfo.dat does not exist!
            fi
            if test "$fname" != "$name"
         then
                echo "不存在该用户!"
            elif test "$fpassword" !="$password"
            then
                echo "密码不正确!"
            else
                echo "你的记录:"
                echo ”---------"
            fi
          ;;
          4)echo "欢迎你的使用,再见!"
            exsig=1
          ;;
          *)echo "请输入合法的选项!"
          ;;
          esac
          if test "$exsig" = "1"
          then
         break 
          fi
         done

另外,每次输入进入testshell时

```bash
vi testshell
都会先出现这个窗口,难不成和这个有关系吗


![img](https://img-mid.csdnimg.cn/release/static/image/mid/ask/155601169766117.jpg "#left")

69行和107行 的引号不对 是中文的吧。 改下试试 估计还有其它错误

代码错误很多,修改的代码如下,测试代码可以正常跑了,望采纳
错误总结为3点
1)多处双引号使用错误(应该使用英文引号)
2) 多处 fname 拼写成 fnane
3) 部分if fi 中没有执行代码

img

#! /bin/bash
#filename:shelltest
exsig=0
while true; do
  echo ""
  echo "---欢迎使用本系统:---"
  echo "    1.上班签到"
  echo "    2.下班签出"
  echo "    3.考勤信息查询"
  echo "    4.退出系统"
  echo "----------------------"
  echo ""
  echo "请输入你的选项:"
  read choice
  case $choice in
  1)
    echo "请输入你的名字:"
    read name
    echo "请输入你的密码:"
    read password
    if test -r /home/user/userinfo.dat; then
      while read fname fpassword; do
        echo "$fname"
        echo "$fpassword"
        if test "$fname" = "$name"; then
          break
        fi
      done </home/user/userinfo.dat
    else
      echo System Error:userinfo.dat does not exist!
    fi
    if test "$fname" != "$name"; then
      echo "不存在该用户!"
    elif test "$fpassword" != "$password"; then
      echo "密码不正确!"
    else
      hour=$(date +%H)
      if test "$hour" -gt 8; then
        echo "你迟到了!"
        echo "$name 上班迟到---日期:'date'" >>/home/user/check.dat
      else
        echo "早上好,$name!"
      fi
    fi
    ;;
  2)
    echo "请输入你的名字:"
    read name
    echo "请输入你的密码:"
    read password
    if test -r /home/user/userinfo.dat; then
      while read fname fpassword; do
        if test "$fname" = "$name"; then
          break
        fi
      done </home/user/userinfo.dat
    else
      echo System Error: userinfo.dat does not exist!
    fi
    if test "$fname" != "$name"; then
      echo "不存在该用户!"
    elif test "$fpassword"!= "$password"; then
      echo "密码不正确!"
    else
      hour='date +%H'
      if test "$hour"-lt 18; then
        echo "你早退了!"
        echo "$nane  下班早退---日期:'date' " >>/home/user/check.dat
      else
        echo "再见,$name!"
      fi
    fi
    ;;
  3)
    echo "请输入你的名字:"
    read name
    echo "请输入你的密码:"
    read password
    if test -r /home/user/userinfo.dat; then
      while read fname fpassword; do
        if test "$fname" = "$name"; then
          break
        fi
      done </home/user/userinfo.dat
    else
      echo System Error:userinfo.dat does not exist!
    fi
    if test "$fname" != "$name"; then
      echo "不存在该用户!"
    elif test "$fpassword" !="$password"; then
      echo "密码不正确!"
    else
      echo "你的记录:"
      echo "---------"
    fi
    ;;
  4)
    echo "欢迎你的使用,再见!"
    exsig=1
    ;;
  *)
    echo "请输入合法的选项!"
    ;;
  esac
  if test "$exsig" = "1"; then
    break
  fi
done


#! /bin/bash
userInfoFile='./userinfo.dat'
userCheck='"$userCheck"'
exsig=0
fname=''
fpassword=''
while true; do
  echo ""
  echo "---欢迎使用本系统:---"
  echo "    0.用户注册"
  echo "    1.上班签到"
  echo "    2.下班签出"
  echo "    3.考勤信息查询"
  echo "    4.退出系统"
  echo "----------------------"
  echo ""
  echo "请输入你的选项:"
  read choice
  case $choice in
  0)
    echo "请输入注册名字:"
    read name
    echo "请输入注册密码:"
    read password
    echo "$name $password" >> "$userInfoFile"
  ;;
  1)
    echo "请输入你的名字:"
    read name
    echo "请输入你的密码:"
    read password
    if [ -r "$userInfoFile" ]; then
      while read fname chomped_line; do
        fpassword=$chomped_line

        if test "$fname" = "$name"; then
          break
        fi
      done <"$userInfoFile"
    else
      echo System Error:userinfo.dat does not exist!
    fi
    if [[ $fname != $name ]]; then
      echo "不存在该用户!"
    elif [[ $fpassword != $password ]]; then
      echo "密码不正确!"
    else
      hour=$(date +%H)
      if [[ $hour -gt 8 ]]; then
        echo "你迟到了!"
        echo "$name 上班迟到---日期:'date'" >>"$userCheck"
      else
        echo "早上好,$name!"
      fi
    fi
    ;;
  2)
    echo "请输入你的名字:"
    read name
    echo "请输入你的密码:"
    read password
    if [ -r "$userInfoFile" ]; then
      while read fname chomped_line; do
        fpassword=$chomped_line

        if test "$fname" = "$name"; then
          break
        fi
      done <"$userInfoFile"
    else
      echo System Error:userinfo.dat does not exist!
    fi
    if [[ $fname != $name ]]; then
      echo "不存在该用户!"
    elif [[ $fpassword != $password ]]; then
      echo "密码不正确!"
    else
      hour=$(date +%H)
      if [[ $hour -lt 18 ]]; then
        echo "你早退了!"
        echo "$nane  下班早退---日期:'date' " >>"$userCheck"
      else
        echo "再见,$name!"
      fi
    fi
    ;;
  3)
    echo "请输入你的名字:"
    read name
    echo "请输入你的密码:"
    read password
    if [ -r "$userInfoFile" ]; then
      while read fname chomped_line; do
        fpassword=$chomped_line

        if test "$fname" = "$name"; then
          break
        fi
      done <"$userInfoFile"
    else
      echo System Error:userinfo.dat does not exist!
    fi
    if [[ $fname != $name ]]; then
      echo "不存在该用户!"
    elif [[ $fpassword != $password ]]; then
      echo "密码不正确!"
    else
      echo "你的记录:"
      echo "---------"
    fi
    ;;
  4)
    echo "欢迎你的使用,再见!"
    exsig=1
    ;;
  *)
    echo "请输入合法的选项!"
    ;;
  esac
  if test "$exsig" = "1"; then
    break
  fi
done

67行test语法不对

参考链接,希望有帮助哦

执行shell脚本报错:$‘\r‘: 未找到命令/未预期的符号 `elif‘ 附近有语法错误_韧小钊的博客-CSDN博客_未预期的符号 `elif' 附近有语法错误 原因是脚本中是Windows下的换行符:执行替换即可: sed -i “s%\r%\n%g” 脚本名称或者同notepad转换一下:edit–>eol conversion–>uinx但是每次打包都会出现这个问题,通过设置行尾系统解决拓展:回车 \r r的英文return,控制字符为CR,即Carriage Return;换行 \n n的英文newline,控制字符为LF,即Line Feed;\n: Unix系统里,每行的结尾是“<换行>”,即“\n https://blog.csdn.net/qq_36434219/article/details/118528890?ops_request_misc=&request_id=&biz_id=102&utm_term=shell%E7%BC%96%E7%A8%8B%E5%AE%9E%E9%AA%8C%E6%8A%A5%E5%91%8A%E6%8A%A5%E9%94%99%EF%BC%8Celif%20%E9%99%84%E8%BF%91%E7%9A%84%E8%AF%AD%E6%B3%95%E9%94%99%E8%AF%AF&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~sobaiduweb~default-0-118528890.nonecase&spm=1018.2226.3001.4450

69行 echo ”不存在该用户!" 的第一个引号为中文引号。
47行 echo "早上好,$name!” 的第二个引号为中文引号。
34行 if test "$fname”" != "$nane"的第二个引号为中文引号。
107行 echo ”---------"的第一个引号为中文引号。
所以出现问题。改为英文引号即可。

img

              fi
             if test "$fname" != "$name"
             then
                echo ”不存在该用户!"
                elif test "$fpassword"!= "$password"
             then
                echo "密码不正确!"
             else
                hour='date +%H'
                if test "$hour"-lt 18
                then
                   echo "你早退了!"
                   echo "$nane  下班早退---日期:'date' ">> /home/user/check.dat
                else
                   echo "再见,$name!"
                fi
             fi
```bash

img

img


我建议先理顺标点吧



```java
 if test "$fname”" != "$nane"
                                
echo "早上好,$name!”

echo ”不存在该用户!"

```
几处有问题得我都给你找出来了,把符号改成英文得就可以了