0

I'm getting the following error:

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

I have been stuck here for an hour and I just can not figure it out.

#!/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
1
  • can you put the whole script please, the identation suggests there are missing parts. Commented Mar 16, 2013 at 19:10

2 Answers 2

2

The else keyword isn't associated with any if statement; you ended the if statement with the fi keyword on line 20. Perhaps you are missing an if just before the two read statements?

Sign up to request clarification or add additional context in comments.

2 Comments

So simple thanks for pointing that out. I forgot to add if [ $(id -u) -eq 0 ]; then ...before the read statements
@user2177712 Welcome to SO! If any answer worked for you, consider accepting it by clicking on hollow green tick mark on the left side of answer. (below up-down votes)
0

Another 'run as root only' example:

   #!/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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.