Bash scripting: ignore delete of few user accounts during account removal process
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Bash scripting: ignore delete of few user accounts during account removal process
We are a training institute providing training on Linux and C++ . We had around 100 students in a batch and for each of the user , a separate unix account is created.
After the batch completes , we need to remove all user accounts except few exceptions like faculty and technical staff accounts.
I already identified how to remove the user accounts but am worried how to ignore the user accounts of faculty and technical staff .
My command is as follows:
#UIDMAX will contain the minimum value used by OS for ID selection
UIDMIN=`grep "UID_MIN" /etc/login.defs`
#UIDMAX will contain the mixnimum value used by OS for ID selection
UIDMAX=`grep "UID_MAX" /etc/login.defs`
#echo $UIDMAX
#echo $UIDMIN
for i in ` awk -F: -v "min=${UIDMIN##UID_MIN}" -v "max=${UIDMAX##UID_MAX}" '{ if ( $3 >= min && $3 <=max ) print $1}' /etc/passwd`
do
userdel -r $i
done
Please suggest how to ignore faculty unix accounts so that they are ignored during the deletion process .
In the best of all worlds, you would have prepared ahead and installed your student accounts using a script and list file. That list file would provide a key into deleting only student accounts.
Failing the above, did you at least give your students and faculty different group membership? That would make this easy.
Failing ALL of the above, did you at least put faculty and student home folders in different trees?
As a last choice, can you list all of your faculty and staff IDs (of UIDs) and have your script do a fast find to skip those accounts?
Next time, plan ahead. This should be really easy, but not doing the preperation up front makes for a lot more hoop jumping at the end.
maybe in /etc/passwd you can put a comment as to whether an account is a student or faculty. then it a simple if statement in a bash script could operate on the student accounts.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.