INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

find next free gid and uid (and find identical id number free in both /etc/passwd and /etc/group)

find next free gid and uid (and find identical id number free in both /etc/passwd and /etc/group)

find next free gid and uid (and find identical id number free in both /etc/passwd and /etc/group)

(OP)

hello,

the two commands below, gives me one next free uid and second one next free group id on a system (both from range 2000-10000)

I need to find first free number from that range, which is the same for /etc/passwd and /etc/group (to create a user with the same number of his uid and gid where guid are from mentined range).

awk -F: '{uid[$3]=1}END{for(x=2000; x<=10000; x++) {if(uid[x] != ""){}else{print x; exit;}}}' /etc/passwd
awk -F: '{uid[$3]=1}END{for(x=2000; x<=10000; x++) {if(uid[x] != ""){}else{print x; exit;}}}' /etc/group

thx,
kind regards,

RE: find next free gid and uid (and find identical id number free in both /etc/passwd and /etc/group)

Hi

So you want the first number starting from 2000 which is neither in /etc/passwd nor in /etc/group ? Then why not just pass both files to your Awk one-liner ? After all, you not seem to be interested why uid[$3] will be set to 1. Otherwise I would just reduce abit that if part :

CODE --> command-line

awk -F: '{uid[$3]=1}END{for(x=2000;x<=10000;x++)if(!uid[x]){print x;exit}}' /etc/passwd /etc/group 

Feherke.
http://feherke.github.com/

RE: find next free gid and uid (and find identical id number free in both /etc/passwd and /etc/group)

(OP)

thx, just one more question.

if I would like to do "the same" but for e.g. 4 files, I can't give the 4 files as parameters, and instead I could 'cat' all 4 files to one and then process one?

or maybe there is a way to give all four files "on fly" to the command?

RE: find next free gid and uid (and find identical id number free in both /etc/passwd and /etc/group)

I can't give the 4 files as parameters
Why ?

RE: find next free gid and uid (and find identical id number free in both /etc/passwd and /etc/group)

(OP)
hmmm,, indeed no problem with 4 or more files... thx.

CODE

$ first=6000
$ max=10000
$ paste file1 file2 file3 file4
a:6001  b:6000  c:6000  d:6005
a:6002  b:6002  c:6000  d:6004
a:6000  b:6002  c:6003  d:6004
a:6000  b:6001  c:6004  d:6003
a:6000  b:6000  c:6001  d:6002
        b:6000  c:6000  d:6000
                c:6000  d:6000
                        d:6000
$ awk -F: -vl=${first} -vm=${max} '{uid[$2]=1}END{for(x=l;x<=m;x++)if(!uid[x]){print x;exit}}' file1 file2 file3 file4
6006
$ echo b:10000 >>file1
$ paste file1 file2 file3 file4
a:6001  b:6000  c:6000  d:6005
a:6002  b:6002  c:6000  d:6004
a:6000  b:6002  c:6003  d:6004
a:6000  b:6001  c:6004  d:6003
a:6000  b:6000  c:6001  d:6002
b:10000 b:6000  c:6000  d:6000
                c:6000  d:6000
                        d:6000
$ awk -F: -vl=${first} -vm=${max} '{uid[$2]=1}END{for(x=l;x<=m;x++)if(!uid[x]){print x;exit}}' file1 file2 file3 file4
6006
$ echo b:6007 >>file4
$ awk -F: -vl=${first} -vm=${max} '{uid[$2]=1}END{for(x=l;x<=m;x++)if(!uid[x]){print x;exit}}' file1 file2 file3 file4
6006
$ echo b:6006 >>file4
$ awk -F: -vl=${first} -vm=${max} '{uid[$2]=1}END{for(x=l;x<=m;x++)if(!uid[x]){print x;exit}}' file1 file2 file3 file4
6008
$ 


Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close