?
Solved

delete user account script

Posted on 2012-09-16
8
Medium Priority
?
1,054 Views
Last Modified: 2012-09-16
#!/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.
0
Comment
Question by:bt1942
  • 3
  • 3
  • 2
8 Comments
 
LVL 31

Expert Comment

by:farzanj
ID: 38403076
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
 

Author Comment

by:bt1942
ID: 38403098
I replaced useradd -p $pass -g $gn  $un to userdel $un as you said but it doesn't delete user account.
0
 
LVL 31

Expert Comment

by:farzanj
ID: 38403099
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
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 

Author Comment

by:bt1942
ID: 38403106
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
 
LVL 31

Expert Comment

by:farzanj
ID: 38403117
Use a loop
0
 
LVL 85

Accepted Solution

by:
ozo earned 2000 total points
ID: 38403124
change
      echo “user $un already exists”
to
    userdel $un
0
 

Author Comment

by:bt1942
ID: 38403141
can you please have a look at this too ozo
http://www.experts-exchange.com/OS/Linux/Q_27866590.html
0
 
LVL 85

Expert Comment

by:ozo
ID: 38403151
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

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
Often times it's very very easy to extend a volume on a Linux instance in AWS, but impossible to shrink it. I wanted to contribute to the experts-exchange community a way of providing a procedure that works on an AWS instance. It can also be used on…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
Suggested Courses
Course of the Month14 days, 10 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question