Home / ... / Scripts / User Accounts Scripts / Adding and Deleting Users
Page options

Adding and Deleting Users



Adding and Deleting Users Using a Bash Script

Use this script to Add and Delete Users

AddingDeletingUsers.sh  (Downloads)

Script Author : Paul Coletti & Anthony Brooks
 

#!/bin/bash
 #-------------------------------------------------------------------------------#
#                                                        #
#                               
Adding and Deleting Users                                            #
#                                                                         #
#------------------------------------------------------------------------------#
#This section of the script is where users are added to the system either manually or from a text file.

  cr_usrs()
{
    r_uid=0      #This will show that the root user will have a UID of 0
    if      [ "$r_uid" -ne 0 ];
        then
            echo "You must verify yourself as the root user to run this script!"
            exit
    fi

    if    [ "$r_uid" == 0 ];
        then
        echo "Identity confirmed"
    fi

    echo "==================================================="
    echo "    Please select an option:

    1. Create a user
    2. Exit"
    echo "==================================================="

    read cr_option
    case $cr_option in

    1)
       
        echo "Please enter a User name:"
        read usr_nme
       
        echo "Please enter a User Group"
        read usr_grp
        groupadd $usr_grp
               useradd -g $usr_grp -m $usr_nme
               
        echo "Please enter a Password for the User $usr_nme"
         passwd $usr_nme ;;

    2)    exit

esac
}
#-------------------------------------------------------------------------------------------
#This section of the script will delete the users from the home directory

dl_usrs()
{
    r_uid=0      #The root user has a UID of 0
   
    if      [ "$r_uid" -ne 0 ];
        then
            echo "You must verify yourself as the root user to run this script!"
            exit
    fi

    if    [ "$r_uid" == 0 ];
        then
        echo "Identity confirmed"
    fi
   
   
 #This is the Menu to select an option to delete the users, you can either delete a selected user or delete all the users you have in the 'user_file' text file.

    echo "============================================"
    echo "Please select an option:

       1.Delete a certain user
       2 Exit"
    echo "============================================"

    read del_opt
    case $del_opt in

    1)
        echo "You currently have the following Users added to your system"
        cat /etc/passwd | grep user #This will search the /etc/passwd file for users
        echo "Type the name of the User you want to delete: "
        read user_name
        userdel -r $user_name ;;

    2)    exit
 
esac
}

#----------------------------------------------------------------------------------------- 
#This section of the script is where user accounts on the system are verified either on the whole system or the users that are present in the text file."     

cnfrm_usrs()
{
echo    "============================================"
echo    "    Please Select the Mode:
   
    1. Confirm all the Users of the System
    2. Exit"
echo    "============================================"
   
    read confirm_user
    case $confirm_user in
    1) cat /etc/passwd | grep bash;;

    2) exit

esac                     
}

#---------------------------------------------------------------------------
#This section of the script is the menu system that the script operates from.

main_menu()
{
choice=1
    while [ $choice -le 4 ]
    do
    clear
    echo "======= Main Menu =======
           
    1. Add Users
    2. Confirm Users
    3. Delete Users
    4. EXIT"

read choice
case $choice in

    1) cr_usrs ;;
    2) cnfrm_usrs ;;
    3) dl_usrs ;;
    4) exit 0 ;;
    *) echo "You have selected an invalid choice."
esac

echo "Would you like to run the script again? Yes=1 & No=4."
read choice
done
}
main_menu
exit 0


    Post a comment

    Your Name or E-mail ID (mandatory)

     

    No Attachments Found. Click here to upload new file.




     RSS of this page

    Author: mustafa_h   Version: 1.3   Last Edited By: mustafa_h   Modified: 06 Apr 2010



    Sign in