2012-11-09 130 views
-1

我對編程相當陌生,在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」行之前就遇到問題。 –

回答

0

你缺少一個done相匹配的最後while,但你不能看到它,因爲它埋格式化不好的代碼中。

嘗試重新格式化代碼,在echo語句前添加換行符並正確縮進。

+0

感謝您的反饋!對不起,很晚回覆。我對這種格式表示歉意,很多都是從終端到這個頁面的複製/粘貼過程,而不是UNIX中的實際編碼佈局。再次感謝您的幫助! – user1801053

相關問題