Unix 第98行:意外标记'else';

Unix 第98行:意外标记'else';,unix,if-statement,nested,syntax-error,nested-loops,Unix,If Statement,Nested,Syntax Error,Nested Loops,我是编程新手,在UNIX中编写shell脚本程序时遇到语法错误。我正在尝试创建一个数据库,允许用户在执行shell脚本后使用内置命令创建、删除和备份 然而,每当使用if语句或while循环时,我都会遇到问题。更具体地说,当我同时使用“完成”或“fi”时,包括嵌套循环,在何处放置“完成”或“fi”。好了,开始了 > #!/bin/bash > > > echo "*****Welcome to User Administration Program*****" >

我是编程新手,在UNIX中编写shell脚本程序时遇到语法错误。我正在尝试创建一个数据库,允许用户在执行shell脚本后使用内置命令创建、删除和备份

然而,每当使用if语句或while循环时,我都会遇到问题。更具体地说,当我同时使用“完成”或“fi”时,包括嵌套循环,在何处放置“完成”或“fi”。好了,开始了

> #!/bin/bash
> 
> 
> echo "*****Welcome to User Administration Program*****"
>
> for ((i=0;i<3;++i))
> #for i in `seq 1 3`
> do
>
> count=`expr $count + 1`
> echo -n "Please enter adminstrative username: "
> read input
> if [ $input == admin ]; then
> break
> if [ $count -eq 3 ]; then
> break fi fi done
> 
> 
> #so far so good
> 
> echo "Welcome admin!" echo -n "[administator@shell]$ " read userin
> while [ "$userin" = "createuser" -o "$userin" = "deleteuser" -o
> "$userin" = "ba$  do
>        command1="createuser"
>          command2="deleteuser"
>          command3="backup"
> 
> if [ "$userin" = "$command1" ]; then
>         echo "Please enter a username "
>         echo -n "[administrator@shell]$ "
> 
>  read username while read line do if  [ "$username" = "$line" ]; then
> #not reading lines correctly
>         echo "$line is taken! choose another" else
>       newUser="$username"
>         echo -n "Hello $newUser ! , enter password "
>         read passEntered
>         newPass="$passEntered"
>         echo $newUser $newPass >> userdb
>         echo "Thank you for creating password, $newUser !" <"userdb" echo "Program terminated!" exit fi done
> 
> #create user works and is completed, with exception of minor error
> 
> elif [ "$userin" == "$command2" ]; then echo "Which user do you wish
> to delete?" echo -n "[administrator@shell]$ " while    read delete do
>          username=$(echo $delete | cut -d" " -f2)
>          usernameExists=$(grep -w -c $username userdb)
>          sed -i '/$username:/d' userdb
> 
> 
> 
> if [ "$delete"  == "admin" ]; then
>          echo "You do not have necessary powers to perform this action."
>         echo "Try  again!" else command=$(echo $input | cut -d" " -f1)
> 
> dir=$(echo $input | cut -d" " -f2) fi done
> 
> 
> elif [ "$userin" == "$command3" ]; then
>          echo "How do you wish to backup?" echo -n "[administrator@shell]$ " while read input do backup=true if [
> "$input" != "$BACKUP" ]; then echo "Invalid command! Try again" fi
> 
> else #where error is happening echo "Invalid command. Please Try
> again!" fi done
#/bin/bash
> 
> 
>echo“****欢迎使用用户管理程序*****”
>
>对于((i=0;i#对于序列13中的i)`
>做
>
>count=`expr$count+1`
>echo-n“请输入管理用户名:”
>读取输入
>如果[$input==admin];则
>中断
>如果[$count-等式3];则
>打破僵局
> 
> 
>#到目前为止还不错
> 
>echo“欢迎管理员!”echo-n[administator@shell]$“读取用户输入”
>而[“$userin”=“createuser”-o“$userin”=“deleteuser”-o
>“$userin”=“ba$do”
>command1=“createuser”
>command2=“删除用户”
>command3=“备份”
> 
>如果[“$userin”=“$command1”];则
>echo“请输入用户名”
>回声-n“[administrator@shell]$ "
> 
>在读取行时读取用户名如果[“$username”=“$line”];则
>#没有正确读行
>echo“$行已被占用!请选择另一行”其他
>newUser=“$username”
>echo-n“Hello$newUser!”,输入密码
>读密码输入
>newPass=“$passEntered”
>echo$newUser$newPass>>userdb
>echo“感谢您创建密码$newUser!”
>#创建用户作品并完成,除了小错误
> 
>elif[“$userin”==“$command2”];然后echo“您希望哪个用户?”
>删除“echo-n”[administrator@shell]$“读取时删除do
>用户名=$(echo$delete | cut-d”“-f2)
>usernameExists=$(grep-w-c$username userdb)
>sed-i'/$username:/d'userdb
> 
> 
> 
>如果[“$delete”==“admin”];则
>echo“您没有执行此操作所需的权限。”
>echo“重试!”else命令=$(echo$input | cut-d”“-f1)
> 
>dir=$(echo$输入| cut-d”“-f2)fi完成
> 
> 
>elif[“$userin”==“$command3”];然后
>echo“您希望如何备份?”echo-n[administrator@shell]$“读取输入时执行备份=如果[
>“$input”!=“$BACKUP”];然后回显“无效命令!重试”fi
> 
>else#发生错误的地方echo“无效命令。请重试
>又来了

您缺少一个
done
来匹配上一个
while
,但是您看不到它,因为它隐藏在格式不良的代码中


尝试重新格式化代码,在
echo
语句之前添加换行符,并正确缩进。

这是非常糟糕的代码格式。换行符在sh脚本中有意义,但似乎是随机放入或省略的。您在问题中引用的脚本是否与您正在执行的脚本完全相同?看起来是这样的就像你应该在
else
行之前很久就遇到问题一样。谢谢你的反馈!很抱歉回复得太晚。我对格式表示歉意,这与从终端到此页面的复制/粘贴过程有关,而不是UNIX中的实际编码布局。再次感谢你的帮助!