LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 09-18-2007, 01:54 PM   #1
crontab
Member
 
Registered: Jun 2005
Distribution: Various
Posts: 72

Rep: Reputation: 15
Figuring out the next available userid for adding a user


Just as an exercise and to pass some time, I'm looking to recreate adduser in perl. The syntax of /etc/passwd requires a userid and groupid number for the new user. adduser will default the userid to the next available, but I don't know how it determines that. Does it look in /etc/passwd, or is there another file with every userid in use?

Thanks for any help you can spare
 
Old 09-18-2007, 02:25 PM   #2
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Rep: Reputation: 15
You could use a regex to identify the UIDs from /etc/passwd:

Code:
cat /etc/passwd | perl -p -e "s/.*:.*:(.*):.*:.*:.*:.*/\1/g"
 
Old 09-18-2007, 02:30 PM   #3
crontab
Member
 
Registered: Jun 2005
Distribution: Various
Posts: 72

Original Poster
Rep: Reputation: 15
Hm, cool. And then figure out the largest number, and add 1 to it?
 
Old 09-18-2007, 03:42 PM   #4
ChrisScott
Member
 
Registered: Nov 2006
Location: St Albans, England
Distribution: Fedora c3/5, Suse pro 10/openSuse 10.2, RHES, Zenwalk.....
Posts: 97

Rep: Reputation: 15
Yeah. Or a double loop to look for the first free. Something like:

Code:
for ($X=1,1,$X++) {
   foreach $check(@arrayOfUIDs) {
      if ($check == $X) {
         $failed = 'true';
         }
      }
   if ($failed != 'true') {
      print $X;
      exit 0;
      }
   $failed = 'false';
   }
Although I'm not very up on perl so that may not work
 
  


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
Adding a user Joecks Solaris / OpenSolaris 10 03-03-2007 02:31 PM
adding a new user Cinematography MEPIS 5 07-25-2005 09:19 PM
adding user paschim Linux - Newbie 4 06-27-2004 05:05 AM
Adding user desbyleo Linux - Newbie 3 02-18-2002 07:15 PM
Why does kppp.desktop require root userid for non-root user? rdaves@earthlink.net Linux - Networking 4 08-27-2001 09:18 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 11:00 AM.

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
Open Source Consulting | Domain Registration