1755747 Members
3772 Online
108837 Solutions
New Discussion юеВ

user account -delete

 
SOLVED
Go to solution
ani007
Super Advisor

user account -delete

Hi,
I am totally new in scripting env.I need a script for the following scnerio.

we have some user account now a days they don't exist.So we want to delete those.What we want to do we will take name one by one from the /etc/passwd field & search whether their home directory exists or not , if they exists then we will said user exists if not then it will show a msg that do u want to delete, if we press yes then it will delete that user.we want to fire the script from a centrilized server which will ssh to other servers & chec this.. Could you please help me..
Regards,
Ani
11 REPLIES 11
James R. Ferguson
Acclaimed Contributor

Re: user account -delete

Hi Anl:

This is one way to begin:

# cat ./seeker
#!/bin/sh
OLDIFS=${IFS}
IFS=":"
while read USER X X X X HOMEDIR X
do
if [ ! -d ${HOMEDIR} ]; then
echo "'${HOMEDIR}' does not exist for '${USER}'"
fi
done < /etc/passwd
IFS=${OLDIFS}
exit 0

Regards!

...JRF...
Jose Mosquera
Honored Contributor

Re: user account -delete

Hi,

Find attached a script that find users without homedir on a system.
First try a test using this syntax:
#sh scriptname.sh test
Also review the -READ ME FIRST- note placed inside.

For a real execution:
#sh scriptname.sh

Be careful with masive user deletion.

Rgds.
jona_1
Frequent Advisor

Re: user account -delete

Hi Josh,
Tnx.I will fire this in a test box first.I hv some question.
if [ ! -d $USERDIR ]
--what does it mean?

as i will fire this to test box i want to remove this if [ "$1" = "test" ]

regards,
Jose Mosquera
Honored Contributor

Re: user account -delete

Hi,

if [ ! -d $USERDIR ]
Inside of a question (if), "-d" validates if is a directory file's type, and "!" is a negation. This is the question's meaning: If the content of $USERDIR variable is not a directory?

>>as i will fire this to test box i want to remove this if [ "$1" = "test" ]<<
You do not needs delete this question, it's only be useful if you execute the shell script followed of the second argument "test", if you will fire just executes the shell script without next argument.

Rgds.
ani007
Super Advisor

Re: user account -delete

if input=yes then what it will check can you please explain the below lines

if [ "$INPUT" = "y" ]---#upto this i understood.#
then
if [ "$1" = "test" ]
then
echo "This user: $USERNAME will be removed"
else
userdel $USERNAME
James R. Ferguson
Acclaimed Contributor

Re: user account -delete

Hi (again) Ani:

Since you are totally new to shell scripting, I urge you to look at the code already provided along with a quick-read of:

http://docs.hp.com/en/B2355-90046/index.html

The aforementioned document is a good starting point for an overview of a shell and the shell's basic syntax. Look specifically at the POSIX and Korn shell section. The POSIX shell is HP's default shell. You will find that the variations of the 'ksh' (Korn) shell are natural cousins of that as is the GNU Bash shell which adds a great number of rich features.

Avoid the 'csh' shell for serious work of any kind.

This reduces your initial reading to a small subset of this guide, notably section IV.

Regards!

...JRF...
ani007
Super Advisor

Re: user account -delete

Hi Josh,
could u pls reply ...

if input=yes then what it will check can you please explain the below lines

if [ "$INPUT" = "y" ]---#upto this i understood.#
then
if [ "$1" = "test" ]
then
echo "This user: $USERNAME will be removed"
else
userdel $USERNAME

Ani
Jose Mosquera
Honored Contributor
Solution

Re: user account -delete

Hi,

Ok, I'll traduce to pseudo code the script:

Do while all lines at /etc/passwd filtered by fields 1 (username), 3 (userid) and 6 (user home dir), setting ENTRY whit this values (separated by ":" char) for each entry. ie: ENTRY=bob:1323:/home/bob
Assing to USERNAM the first value of ENTRY variable (separated by ":" char). USENAM=bob
Assing to USERIDE the second value of ENTRY variable (separated by ":" char). USERIDE=1323
Assing to USERDIR the third value of ENTRY variable (separated by ":" char). USERDIR=/home/bob
if(1) $USERDIR not is a directory
then(1) display the mesagge for deletion and read user answer (INPUT)
if(2) $INPUT = "y"
then(2) if(3) script execution have a first argument ($1) with value "test"
then(3) just display "This user: $USERNAME will be removed" no real deletion action will be done.
else(3) a real deletion of user ($USERNAME) will be done
endif(3)
endif(2)
endif(1)
endo

Also "else(3)" action includes the -READ ME FIRST- consideration. Here all lines starting with # char are interpreted as comments. Please understand this lines to avoid files with "nouser" inside your system.
NOTE: the first line is not a comment, this line indicates the shell interpreter that will be use when run the script (#/usr/bin/sh).

The class is over, you owe me a lot of points! :)

Best regards.
ani007
Super Advisor

Re: user account -delete

Hi ,

Thank you..last question ..I also want to write a script like you people..could you please give me some URL or guide me from where i will start, u explain me every thing in a very simple way but in books it's complicated. so please please give me some URL so let me know from where i will start.please guide me ..

Regards,
Ani