LinuxQuestions.org
Register a domain and help support LQ
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-16-2011, 12:51 PM   #1
Linux_Newbie90
LQ Newbie
 
Registered: Mar 2011
Posts: 4

Rep: Reputation: 0
Creating unique users accounts using count.


I am trying to create users accounts reading from a file. I can get it to show if the user already exists but I want it to instead of not creating the user account to create e.g. Scott1 etc.

The code i have so far is :

#!/bin/bash
while read line
do
grep -c $line /etc/passwd
useradd -m $line
done <"users.txt"

Thanks for your help.
 
Old 03-16-2011, 02:58 PM   #2
Snark1994
Senior Member
 
Registered: Sep 2010
Location: Wales, UK
Distribution: Arch
Posts: 1,632
Blog Entries: 3

Rep: Reputation: 345Reputation: 345Reputation: 345Reputation: 345
Have you had a look at the 'newusers' command? It sounds like you're doing something rather similar

No point re-inventing the wheel... unless you've got a fascination with the way the wheel works.

Hope this helps,
 
Old 03-16-2011, 03:53 PM   #3
Linux_Newbie90
LQ Newbie
 
Registered: Mar 2011
Posts: 4

Original Poster
Rep: Reputation: 0
Will the 'new user' command will make sure each username is unique. I there are some duplicates within the file and I dont want to remove them I want to make two accounts with the name but add a number on the end of the username. My idea was to use a count but not sure how to implement.

Thanks for replying
 
Old 03-16-2011, 08:06 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,253

Rep: Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686Reputation: 2686
It would seem you could just put a simple 'if' within your while to test your grep.
 
Old 03-16-2011, 09:00 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,066
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
Quote:
Originally Posted by Linux_Newbie90 View Post
I am trying to create users accounts reading from a file. I can get it to show if the user already exists but I want it to instead of not creating the user account to create e.g. Scott1 etc.

The code i have so far is :

#!/bin/bash
while read line
do
grep -c $line /etc/passwd
useradd -m $line
done <"users.txt"

Thanks for your help.

Something like this?
Code:
#!/bin/bash
while read line
do
  cur=$(grep -c $line /etc/passwd)
  if [ $cur -eq 0 ]; then
    echo useradd -m $line
  else
    echo useradd -m $line$(( cur +1 ))
  fi
done <"users.txt"
Remove the "echo"s if it looks right for your use-case ;D



Cheers,
Tink
 
  


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
How to count occurrences of unique words in a file beeblequix Linux - General 3 01-14-2007 04:47 PM
search / count unique patterns in text file logicalfuzz Linux - Newbie 2 10-14-2006 08:58 AM
LXer: 5 Unique Tips for New Ubuntu Users LXer Syndicated Linux News 0 06-30-2006 11:54 PM
creating a unique id string in shell gumby Programming 4 05-07-2003 06:56 PM
Creating User Accounts Winux Linux - Newbie 6 04-01-2003 06:35 AM


All times are GMT -5. The time now is 12:54 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration