delete user account script

#!/bin/bash
cat  $1 | while read  un  gn  pw
      do
            cat  /etc/group  |  egrep $gn
                  if  [ $? == 0 ]; then
                        echo “Group $gn  already exists”
                  else
                        groupadd  $gn
                  fi
            pass=$(perl -e ‘print crypt($ARGV[0], “password”)’ $pw)
            cat /etc/passwd | egrep $un
                  if  [  $? == 0 ]; then
                        echo “user $un already exists”
                  else
                        useradd -p $pass -g $gn  $un
                        if  [ $? == 0 ]; then
                              echo “user $un successfully added”
          fi
                  fi
      done



The purpose of the code above is to add user, group, password to the system.
For example, I saved above code as adduser.sh then, I made txt file which contains name, groupName, password and saved as userlist.txt.

If I exute the code above ./adduser.sh userlist.txt, the code will add all the users, groups, password which is in the userlist.txt file.

However, how can I modify this code to delete user's account .
Thank you for your help.
bt1942Asked:
Who is Participating?
 
ozoCommented:
change
      echo “user $un already exists”
to
    userdel $un
0
 
farzanjCommented:
Use userdel command

userdel $un

Open in new window


Also noticed that for changing password, you have to do some tricks where it was straight forward like

echo $pw | passwd --stdin $un

Open in new window

0
 
bt1942Author Commented:
I replaced useradd -p $pass -g $gn  $un to userdel $un as you said but it doesn't delete user account.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
farzanjCommented:
Where you have this useradd statement, you know the user does not exist so if you put userdel, it would not work because the user doesn't exist.

Why do you need to even delete a user?  If you want to make modifications to the user's description, you can use usermod.  If you want to specifically change password, you can use the statement I told above.
0
 
bt1942Author Commented:
I know userdel -r name will delete only 1 user account but
How do I use userdel command to delete lots of user accounts in once?
0
 
farzanjCommented:
Use a loop
0
 
bt1942Author Commented:
can you please have a look at this too ozo
http://www.experts-exchange.com/OS/Linux/Q_27866590.html
0
 
ozoCommented:
If I were a user on a system where someone unfamiliar with shell scripts was adding and deleting so many users at a time that it had to be scripted into a loop, I think I would be feeling nervous about my account.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.