While loop running after reaching limit

Hi frnds

i am newbie in linux and trying to write a simple script for adding users.the problem is i am running a while loop under function and loop is running for 3 time more than limit.Here is my Script and output pls help me out :

 # CREATE N NO. OF USERS WITH PASSWORD IN SYSTEM



# ./users to run the script

# CREATE A FUNCTION CALLED ADD()

ADD ()

{

   while [ $i  -le $no ]

      do
          echo -e " Please enter the user name :\t\c"
          read USER

#  Search in /etc/passwd file for user existence

          output=`cat /etc/passwd | grep $USER | cut -d: -f1`


            if [ "$output" = "$USER" ]

            then

    echo " Opps ! $USER is already exist Please Enter again"


## Call ADD Function in case of User already exist

                  ADD
            fi

           useradd $USER

          echo -e " Please enter the Password of the user :\t\c"

          read PASSWD

          echo $PASSWD | passwd --stdin $USER

echo "$USER creates successfully"

          i=`expr $i + 1`


done

}


clear

echo -e " Please Enter How many users want to create :\t\c"

read no

i=1


# Call ADD Function

ADD

OUTPUT of Script :

 Please Enter How many users want to create :    2
 Please enter the user name :    viv

 Opps ! viv is already exist Please Enter again

 Please enter the user name :    vishnu
 Opps ! vishnu is already exist Please Enter again

 Please enter the user name :    ram
useradd: user 'ram' already exists
 Please enter the Password of the user :    kev

Changing password for user ram.
passwd: all authentication tokens updated successfully.
ram creates successfully

 Please enter the user name :    john
 Please enter the Password of the user :    john
Changing password for user john.
passwd: all authentication tokens updated successfully.
john creates successfully

useradd: user 'john' already exists
 Please enter the Password of the user :    john
Changing password for user john.
passwd: all authentication tokens updated successfully.
john creates successfully

useradd: user 'john' already exists
 Please enter the Password of the user :    jhon
Changing password for user john.
passwd: all authentication tokens updated successfully.
john creates successfully
[root@Riyansh Scripts]# 

pls help me out

thanks

vaibhav

Instead of calling the "ADD" function again, use continue

...
    if [ "$output" = "$USER" ]
    then
      echo " Opps ! $USER is already exist Please Enter again"
      ## Call ADD Function in case of User already exist
      continue
    fi
...

Some points to note... The below code is better than using cat... Also check for the users at the start...

output=`grep ^$USER /etc/passwd | cut -d: -f1`

HTH
--ahamed

1 Like

Hi Ahamed

Thanks a lot. Can u pls tell what "continue" does in if condition also i want to increase specific size of my output fonts can u pls give some solution as i have searched on internet but couldn't find

continue belong to while. If a while encounters continue it will continue its execution from the beginning of while...

HTH
--ahamed

Can be further improved by including the colon in the grep (this allows for users with similar names like "jack" and "jackie":

output=`grep \^"${USER}:" /etc/passwd | cut -d: -f1`

Beware. On my system "users" is the name of a unix command. While you invoke it as "./users" you will not clash. In the commercial world you will probably add a scripts directory to "$PATH" and could then clash.

2 Likes