Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

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

Status
Not open for further replies.

w5000

Technical User
Nov 24, 2010
223
PL

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,
 
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 [tt]uid[$3][/tt] will be set to 1. Otherwise I would just reduce abit that [tt]if[/tt] part :
Code:
awk -F: '[teal]{[/teal]uid[teal][[/teal][navy]$3[/navy][teal]]=[/teal][purple]1[/purple][teal]}[/teal][b]END[/b][teal]{[/teal][b]for[/b][teal]([/teal][navy]x[/navy][teal]=[/teal][purple]2000[/purple][teal];[/teal]x[teal]<=[/teal][purple]10000[/purple][teal];[/teal]x[teal]++)[/teal][b]if[/b][teal](![/teal]uid[teal][[/teal]x[teal]])[/teal][teal]{[/teal][b]print[/b] x[teal];[/teal][b]exit[/b][teal]}}[/teal]' /etc/passwd /etc/group

Feherke.
[link feherke.github.com/][/url]
 

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?
 
I can't give the 4 files as parameters
Why ?
 
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
$


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top