LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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

Reply
 
Search this Thread
Old 03-05-2013, 08:32 AM   #1
as92
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Rep: Reputation: Disabled
Dealing with duplicate usernames


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
Any help would be much appreciated, thank you!
 
Old 03-05-2013, 10:23 AM   #2
lykwydchykyn
Member
 
Registered: Mar 2006
Location: Tennessee, USA
Distribution: Debian, Ubuntu
Posts: 135

Rep: Reputation: 36
Something like this might work:

Code:
#!/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.
 
1 members found this post helpful.
Old 03-05-2013, 10:33 AM   #3
suicidaleggroll
Senior Member
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 2,843

Rep: Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006Reputation: 1006
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.
 
1 members found this post helpful.
Old 03-05-2013, 10:50 AM   #4
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 778
Blog Entries: 2

Rep: Reputation: 199Reputation: 199
If you must grep then
Code:
grep ^${USER}: /etc/passwd
Other options include Perl with the getpwnam() function.

You'll want to lock the passwd file by linking it to pwtmp before you make changes and unlink it afterwards.
 
Old 03-05-2013, 11:02 AM   #5
as92
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
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

Cheers again
 
Old 03-05-2013, 11:21 AM   #6
as92
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
I probably should have added that the file literally has a list of names, no passwords, id field etc.

I tried the solutions and it's not creating any accounts. Anyone got any advice of where to go from here?

Thanks!
 
Old 03-05-2013, 11:28 AM   #7
linosaurusroot
Member
 
Registered: Oct 2012
Distribution: OpenSuSE,RHEL,Fedora,OpenBSD
Posts: 778
Blog Entries: 2

Rep: Reputation: 199Reputation: 199
Quote:
Originally Posted by as92 View Post
not creating any accounts
Are you running it as root?
 
Old 03-05-2013, 11:30 AM   #8
as92
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Smile

I am indeed :/
 
Old 03-05-2013, 11:53 AM   #9
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Why don't you try like:
Code:
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

Last edited by shivaa; 03-05-2013 at 11:57 AM.
 
1 members found this post helpful.
Old 03-06-2013, 07:23 AM   #10
as92
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
It needs to take the names from a file, rather than being entered manually. Anyone got any ideas?
Cheers
 
Old 03-06-2013, 07:42 AM   #11
as92
LQ Newbie
 
Registered: Feb 2013
Posts: 8

Original Poster
Rep: Reputation: Disabled
Works now, thanks all
 
Old 03-06-2013, 07:52 AM   #12
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,797
Blog Entries: 4

Rep: Reputation: 285Reputation: 285Reputation: 285
Quote:
Originally Posted by as92 View Post
It needs to take the names from a file, rather than being entered manually. Anyone got any ideas?
Cheers
Then take input from file... simple
Code:
while read -r user
do
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
done < infile.txt
 
1 members found this post helpful.
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Identifying usernames having duplicate user-id in /etc/passwd file zama Linux - Software 1 03-02-2012 06:10 AM
does tar or bzip2 squash duplicate or near-duplicate files? garydale Linux - Software 6 11-19-2009 04:43 PM
See long usernames andrei048 Linux - Software 2 11-15-2009 12:00 PM
why no usernames in /etc/group?? lrt Linux - Server 4 03-18-2008 11:02 AM
Usernames Zwitterion Linux - General 1 12-25-2002 08:30 AM


All times are GMT -5. The time now is 09:45 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration