0

我收到以下错误:

./adduser.sh: line 21: syntax error near unexpected token `else'
./adduser.sh: line 21: `        else'

我在这里被困了一个小时,我就是想不通。

#!/bin/bash
#==========================================================================================================================================
# Script Name:  adduser.sh
# By:           Tim mayo
# Date:         3/2013
# Purpose:      Add a user to the Linux system
# Command line: adduser.sh
#
#==========================================================================================================================================
        read -p "Enter username : " username
        read -s -p "Enter password : " password
        egrep "^$username" /etc/passwd >/dev/null
        if [ $? -eq 0 ]; then
                echo "$username exists!"
                exit 1
        else
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
                useradd -m -p $pass $username
                [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
        fi
                else
                        echo "Root user can only add a user to the Linux system"
                        exit 2
fi
4

2 回答 2

2

else关键字不与任何if语句关联;您在第 20 行用关键字结束了if语句。fi也许您if在两个语句之前缺少一个read

于 2013-03-16T18:53:52.377 回答
0

另一个“仅以 root 身份运行”示例:

   #!/bin/bash


if [ $UID -eq "0" ]
then
        read -p "Enter username : " username
        read -s -p "Enter password : " password
        egrep "^$username" /etc/passwd >/dev/null
        if [ $? -eq 0 ]; then
                echo "$username exists!"
                exit 1
        else
                pass=$(perl -e 'print crypt($ARGV[0], "password")' $password)
                useradd -m -p $pass $username
                [ $? -eq 0 ] && echo "User has been added to system!" || echo "Failed to add a user!"
        fi
                 else
                        echo "Root user can only add a user to the Linux system"
                        exit 2
fi
于 2013-03-16T19:33:54.823 回答