| Author |
How to find out the lowest available uid and gid?
|
|
| goodshi@gmail.com 2007-01-17, 1: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!
| |
| Kenny McCormack 2007-01-17, 1: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.
| |
| John DuBois 2007-01-17, 1: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 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/
| |
| goodshi@gmail.com 2007-01-17, 7:28 pm |
| Thanks, John,
One thing was missing. We are using NIS. So some UIDs are not in local
/etc/passwd file.
John DuBois wrote:
> 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/
| |
| Stephane CHAZELAS 2007-01-17, 7:28 pm |
| 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
| |
| Stephane CHAZELAS 2007-01-17, 7:28 pm |
| 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
| |
|
|
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
| |
| Stephane CHAZELAS 2007-01-17, 7:28 pm |
| 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
| |
|
|
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
| |
| Stephane CHAZELAS 2007-01-19, 7:22 am |
| 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
| |
| Michal Nazarewicz 2007-01-19, 7:29 pm |
| gazelle@xmission.xmission.com (Kenny McCormack) writes:
> In article <1169052333.308114.242390@s34g2000cwa.googlegroups.com>,
> <goodshi@gmail.com> wrote:
>
> 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}'
#v+
nawk -F: -v val=$1 '$3 == val {++val} $3 > val {exit} END {print val}'
#v-
That should speed it up a bit. :]
> }
>
> 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.
--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
|
|
|
|