第98行:意外标记'else'附近的语法错误

问题描述:

我对编程相当陌生,在UNIX中编写shell脚本程序时遇到语法错误。我试图创建一个数据库,允许用户在执行shell脚本后使用内置命令创建,删除和备份。第98行:意外标记'else'附近的语法错误

但是,我在使用if语句或while循环时遇到了问题。更具体地说,无论何时使用两者的组合(包括嵌套循环),将“完成”或“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 "[[email protected]]$ " 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 "[[email protected]]$ " 
> 
> 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 "[[email protected]]$ " 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 "[[email protected]]$ " 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 
+2

这是非常糟糕的代码格式。换行符在sh脚本中有意义,但似乎你会随机地将它们放入或省略它们。您在问题中引用的脚本是否正在执行?看起来您应该早在“else”行之前就遇到问题。 –

你缺少一个done相匹配的最后while,但你不能看到它,因为它埋格式化不好的代码中。

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

+0

感谢您的反馈!对不起,很晚回复。我对这种格式表示歉意,很多都是从终端到这个页面的复制/粘贴过程,而不是UNIX中的实际编码布局。再次感谢您的帮助! – user1801053