How to find out the lowest available uid and gid?
Web Server forum
Back To The Forum Home!Search!Private Messaging System

Web Server Talk Web Server Talk > Unix and Linux reviews > Free Unix support > Unix Shell > How to find out the lowest available uid and gid?




Pages (2): [1] 2 »   Last Thread   Next Thread Next
  Show Printable Version Email this Page Subscribe to this Thread      Post New Thread    Post A Reply      

    How to find out the lowest available uid and gid?  
goodshi@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-17-07 06:17 PM

I need to create a new user account on Sun box. How to use a shell
script to find out the lowest and available uid and gid for this new
user?

Thanks!






[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
Kenny McCormack


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-17-07 06:17 PM

In article <1169052333.308114.242390@s34g2000cwa.googlegroups.com>,
<goodshi@gmail.com> wrote:
>I need to create a new user account on Sun box. How to use a shell
>script to find out the lowest <deleted: and> available uid and gid
>for this new user?

Here's a function I wrote some time back for precisely that purpose:

# Usage: uid=`getuid 380`       find the lowest available ID >= 380

getuid() {
sort -t: +2n -3 /etc/passwd |
nawk -F: -v val=$1 '$3 == val {val++} END {print val}'
}

You could probably do it all (i.e. w/o the external "sort") in GAWK or some
other modern AWK that has built-in sorting capability.





[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
John DuBois


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-17-07 06:17 PM

In article <1169052333.308114.242390@s34g2000cwa.googlegroups.com>,
<goodshi@gmail.com> wrote:
>I need to create a new user account on Sun box. How to use a shell
>script to find out the lowest and available uid and gid for this new
>user?

Assuming you have the system configured in such a way that all of the UIDs a
re
actually stored in /etc/passwd:

awk -F: '{ j[$3] } END { for (i = 200; i in j; i++); print i }
' /etc/passwd

Replace 200 with the lowest uid you want to use.
Modify in the obvious way for gid.

John
--
John DuBois  spcecdt@armory.com  KC6QKZ/AE  http://www.armory.com/~spcecdt/





[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
goodshi@gmail.com


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-07 12:28 AM

Thanks, John,
One thing was missing. We are using NIS. So some UIDs are not in local
/etc/passwd file.

John DuBois wrote:[vbcol=seagreen]
> In article <1169052333.308114.242390@s34g2000cwa.googlegroups.com>,
>  <goodshi@gmail.com> wrote: 
>
> Assuming you have the system configured in such a way that all of the UIDs
 are
> actually stored in /etc/passwd:
>
> awk -F: '{ j[$3] } END { for (i = 200; i in j; i++); print i
 }' /etc/passwd
>
> Replace 200 with the lowest uid you want to use.
> Modify in the obvious way for gid.
>
> 	John
> --
> John DuBois  spcecdt@armory.com  KC6QKZ/AE  http://www.armory.com/~spcecdt/[/vbcol
]






[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-07 12:28 AM

2007-01-17, 08:45(-08), goodshi@gmail.com:
> I need to create a new user account on Sun box. How to use a shell
> script to find out the lowest and available uid and gid for this new
> user?

min=1000
{
cat /etc/passwd
ypcat passwd
getent passwd
} 2> /dev/null | cut -d: -f3 | sort -un |
awk -v "min=$min" '
$0 > min {exit}
$0 == min {min=$0+1}
END {print min}'


--
Stéphane





[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-07 12:28 AM

2007-01-17, 21:17(+00), Stephane CHAZELAS:
> 2007-01-17, 08:45(-08), goodshi@gmail.com: 
>
> min=1000
> {
>   cat /etc/passwd
>   ypcat passwd
>   getent passwd
> } 2> /dev/null | cut -d: -f3 | sort -un |
>   awk -v "min=$min" '
>     $0 > min {exit}
>     $0 == min {min=$0+1}
>     END {print min}'

Though you may want to check that the value is supported by your
OS:

min=1000 max=65535
{
cat /etc/passwd
ypcat passwd
getent passwd
} 2> /dev/null |
cut -d: -f3 |
sort -un |
awk -v "min=$min" -v "max=$max" '
$0 > min {exit}
$0 == min {min++}
END {
if (min > max)
exit(1)
print min
}'

--
Stéphane





[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
bsh


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-07 12:28 AM


Kenny McCormack wrote:
> In article <1169052333.308114.242390@s34g2000cwa.googlegroups.com>,
>  <goodshi@gmail.com> wrote: 
> getuid() { ... }

This and subsequent solutions do not address the circumstance
of "holes" in used UIDs and GIDs, as per the OP's request.

The algorithm for finding the first _available_ UID/GID is to sort
the list (as above), and determine the number which is not one
greater than the previous UID/GID. The first available UID/GID is
the previous number plus one. If there is no such number,
_then_ the first available UID/GID is the last number plus one.

=Brian






[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-18-07 12:28 AM

2007-01-17, 15:07(-08), bsh:
>
> Kenny McCormack wrote: 
>
> This and subsequent solutions do not address the circumstance
> of "holes" in used UIDs and GIDs, as per the OP's request.
>
> The algorithm for finding the first _available_ UID/GID is to sort
> the list (as above), and determine the number which is not one
> greater than the previous UID/GID. The first available UID/GID is
> the previous number plus one. If there is no such number,
> _then_ the first available UID/GID is the last number plus one.
[...]

I'd suggest you reread the provided solution. All do what you
say using a more or less cleaver algorithm.


--
Stéphane





[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
bsh


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-07 12:22 AM


Stephane CHAZELAS wrote:
> bsh wrote: 
> I'd suggest you reread the provided solution. All do what you
> say using a more or less cleaver algorithm.

To look at it again, then: Darn it! You're right.

I can then only add a suggestion to avoid using sort(1)
and awk(1) for such a simple task, and instead use
"set -s -- ..." and a "for" loop to iterate through the
numbers.

=Brian






[ Post a follow-up to this message ]



    Re: How to find out the lowest available uid and gid?  
Stephane CHAZELAS


View Ip Address Report This Message To A Moderator Edit/Delete Message


 
01-19-07 12:22 PM

2007-01-18, 14:16(-08), bsh:
>
> Stephane CHAZELAS wrote: 
>
> To look at it again, then: Darn it! You're right.
>
> I can then only add a suggestion to avoid using sort(1)
> and awk(1) for such a simple task, and instead use
> "set -s -- ..." and a "for" loop to iterate through the
> numbers.
[...]

Why?

And I'm afraid set -s is ksh/zsh specific.

--
Stéphane





[ Post a follow-up to this message ]



    Sponsored Links  




 





   All times are GMT. The time now is 01:52 AM.      Post New Thread    Post A Reply      
Pages (2): [1] 2 »   Last Thread   Next Thread Next


Most Popular forums 

Forum Jump:
Rate This Thread:

Forum Rules:
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is OFF
vB code is ON
Smilies are ON
[IMG] code is OFF
 
Medical and Health forum | Computer Games Reviews | Graphics design forum

Back To The Top
Home | Usercp | Faq | Register