我对编程相当陌生,在 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