Delete user and directory

This script check the user if available if yes will prompt to delete the user and the directory.

#***********************************************************************

#!/bin/bash
#Script by Jeffrey Dante

#Check User if available

if [ $(id -u) -eq 0 ]; then
echo “Checking user $1″
cat /etc/passwd | awk -F: ‘{print $1}’ | grep -w $1
if [ $? -eq 0 ]; then

#Check User Disk Usage
LETTER=`/bin/echo $1 | /bin/awk ‘BEGIN{FS=””}{print $1}’`
DU=/usr/bin/du
echo “Disk Usage:”
$DU -msh /mnt/emaildata/$LETTER/$1/Maildir | awk ‘{print $1}’

while true;
do
read -p “Do you wish to delete this user $1 ? ”  yn
case $yn in
[Yy]* ) userdel $1; rm -r /mnt/emaildata/$LETTER/$1; echo “User $1 has been deleted”; exit;;
[Nn]* ) exit;;
* ) echo “Please answer yes or no.”;;
esac
done

exit 1

else
echo “$1 does not exists!”
fi

else
echo “Only root may delete a user”
exit 2
fi

#***********************************************************************

Advertisements

Mag-iwan ng Tugon

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Baguhin )

Twitter picture

You are commenting using your Twitter account. Log Out / Baguhin )

Facebook photo

You are commenting using your Facebook account. Log Out / Baguhin )

Google+ photo

You are commenting using your Google+ account. Log Out / Baguhin )

Connecting to %s