Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Hi everyone. I have a script that creates user accounts from a list in a file, but I need to be able to deal with duplicate usernames.
I want to be able to add a number on the end of the username if there is a duplicate entry but I'm not sure how to do this.
This is my script so far:
Code:
#!/bin/bash
NEW_USERS="/home/users.txt"
HOME_BASE="/home/"
cat ${NEW_USERS} | \
while read USER
do
if grep ${USER} "/etc/passwd"; then
useradd -m -d ${HOME_BASE}${USER} ${USER} << need to add a number here if it exists
else
useradd -m -d ${HOME_BASE}${USER} ${USER}
fi
#!/bin/bash
NEW_USERS="/home/users.txt"
HOME_BASE="/home/"
cat ${NEW_USERS} | \
while read USER
do
if grep ${USER} "/etc/passwd"; then
NUM=$(grep ${USER} "/etc/passwd" |wc -l)
USER=$USER$NUM
fi
useradd -m -d ${HOME_BASE}${USER} ${USER}
The only problem with the approach of grepping /etc/passwd to determine duplicates is that you'll get false hits on a partial match. For example, if you have a user "sjohnson", grepping for "sjohns" will match. But at worst, you'll get false positives and needlessly have numbers on some user names.
It would probably be more robust to use id instead of grepping /etc/passwd, then just cycle through numbers until id reports that nobody has that name yet.
Something like
Code:
#!/bin/bash
NEW_USERS="/home/users.txt"
HOME_BASE="/home/"
cat "$NEW_USERS" | \
while read USER; do
id $USER > /dev/null 2>&1
if [[ $? -eq 1 ]]; then
useradd -m -d ${HOME_BASE}${USER} ${USER}
else
add=1
id ${USER}${add} > /dev/null 2>&1
while [[ $? -eq 0 ]]; do
add=$((add+1))
id ${USER}${add} > /dev/null 2>&1
done
useradd -m -d ${HOME_BASE}${USER}${add} ${USER}${add}
fi
done
Last edited by suicidaleggroll; 03-05-2013 at 10:37 AM.
Thanks for the replies guys. I don't have to use grep at all, but I didn't know how to do it any other way.
I'll try the scripts you suggested and report back
echo -n "Enter new user to be created: "; read user
exuser=$(awk -v u=$user -F":" '$1 ~ /u/ {print $1}' /etc/passwd)
if [ $user == $exuser ]; then
useradd -m -d ${HOME_BASE}${USER} $USER$$num
fi
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.