我对编程很新,在UNIX中编写shell脚本程序时遇到语法错误。我正在尝试创建一个数据库,允许用户在执行shell脚本后使用内置命令进行创建,删除和备份。
但是,每当使用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 "[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
答案 0 :(得分:0)
您错过了done
以匹配上一个while
,但您无法看到它,因为它隐藏在格式不佳的代码中。
尝试重新格式化代码,在echo
语句之前添加换行符并正确缩进。