我對編程相當陌生,在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
這是非常糟糕的代碼格式。換行符在sh腳本中有意義,但似乎你會隨機地將它們放入或省略它們。您在問題中引用的腳本是否正在執行?看起來您應該早在「else」行之前就遇到問題。 –