LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Creating unique users accounts using count. (https://www.linuxquestions.org/questions/linux-newbie-8/creating-unique-users-accounts-using-count-868972/)

Linux_Newbie90 03-16-2011 11:51 AM

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.

Snark1994 03-16-2011 01:58 PM

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,

Linux_Newbie90 03-16-2011 02:53 PM

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

grail 03-16-2011 07:06 PM

It would seem you could just put a simple 'if' within your while to test your grep.

Tinkster 03-16-2011 08:00 PM

Quote:

Originally Posted by Linux_Newbie90 (Post 4292755)
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


All times are GMT -5. The time now is 03:37 AM.