今天用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
都会先出现这个窗口,难不成和这个有关系吗

69行和107行 的引号不对 是中文的吧。 改下试试 估计还有其它错误
代码错误很多,修改的代码如下,测试代码可以正常跑了,望采纳
错误总结为3点
1)多处双引号使用错误(应该使用英文引号)
2) 多处 fname 拼写成 fnane
3) 部分if fi 中没有执行代码
#! /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语法不对
69行 echo ”不存在该用户!" 的第一个引号为中文引号。
47行 echo "早上好,$name!” 的第二个引号为中文引号。
34行 if test "$fname”" != "$nane"的第二个引号为中文引号。
107行 echo ”---------"的第一个引号为中文引号。
所以出现问题。改为英文引号即可。
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
```java
if test "$fname”" != "$nane"
echo "早上好,$name!”
echo ”不存在该用户!"
```
几处有问题得我都给你找出来了,把符号改成英文得就可以了