I am new to shell scripting, I need to write a korn shell script that will delete user unix account and remove their home directory. detail requirement:
1. a user list that contains tens users. format as: username RealName description.
2. check each user if they have unix account in the server, create a new userlist file that only contain username of those who has valid account
3. check each username from the new userlist if they still have active logon session.
4. list out who still has active session if there is, then exit script with no further action
5. if none of them has active session, then delete user account, tar/gz user's home directory and move to a different folder
6. log all actions to a report file, and append report to a full_log file.
Here is the script I wrote, but I don't know how to add point 4 above, please also help to improve it
Quote:
#!/bin/ksh
WORK_DIR=/var/tmp/del_user
HOME_DIR=/home
LOGFILE=report.`uname -n`
typeset USER_LIST=${1}
if [ "$#" -ne 2 ]
then
echo "usage: deluser.ksh <userlist file> <SCR#>"
exit 1
fi
# convert user list file to userid only file
cut -d' ' -f1 ${USER_LIST} |tr '[A-Z]' '[a-z]' |sort > full_id_list
# check if user has vailid unix account, if yes, add to userid.out file
for USER in $(awk '{print $1}' full_id_list);
do
echo "Checking Unix account for user $USER" >> ${WORK_DIR}/$LOGFILE
/usr/bin/grep $USER /etc/passwd >> ${WORK_DIR}/$LOGFILE 2>/dev/null
if [ "$?" -eq 0 ]
then
echo $USER >> userid.out
fi
done
# If we found users who have valid account, delete them and tar and gz their home direct, remove to /home/to-be-delete folder.
# If none of the user has vaild account, echo out and exit script without any action
if [ -f userid.out ]
then
echo "Following accounts and their Unix home directories will be removed" >> ${WORK_DIR}/$LOGFILE
cat userid.out |tee -a ${WORK_DIR}/$LOGFILE
for ID in `cat userid.out`
do
rmuser -p $ID #AIX command the remove user account with all its attributes
tar cvf - ${HOME_DIR}/$ID |gzip > ${HOME_DIR}/to-be-delete/${ID}.tar.gz #tar/gz user home directory
rm -r ${HOME_DIR}/${ID}
done
else
echo "none of the user in the list has valid Unix account" |tee -a ${WORK_DIR}/$LOGFILE
echo " ----- End ----- "
fi
cat ${WORK_DIR}/$LOGFILE >> ${WORK_DIR}/fulllog.log
mv ${WORK_DIR}/$LOGFILE ${WORK_DIR}/$LOGFILE.`(date +%Y%m%d)`
rm userid.out 2>/dev/null
rm full_id_list
exit