Soldato
Was looking at a way to find out the lowest available UID.
As with lots of things I found someone else's script on line here.
I've changed the value 999 to 1 so it will find the lowest value from 1 rather than 999.
The script works, but I dont quite understand how the function is working.
As I don't have anyone else to ask I'm asking someone here if they would be so kind to comment on the function so I can understand why it works.
Thanks
----
#!/bin/bash
# return 1 if the Uid is already used, else 0
function usedUid()
{
if [ -z "$1" ]
then
return
fi
for i in ${lines[@]} ; do
if [ $i == $1 ]
then
return 1
fi
done
return 0
}
i=0
# load all the UIDs from /etc/passwd
lines=( $( cat /etc/passwd | cut -d: -f3 | sort -n ) )
testuid=1
x=1
# search for a free uid greater than 999 (default behaviour of adduser)
while [ $x -eq 1 ] ; do
testuid=$(( $testuid + 1))
usedUid $testuid
x=$?
done
# print the just found free uid
echo $testuid
As with lots of things I found someone else's script on line here.
I've changed the value 999 to 1 so it will find the lowest value from 1 rather than 999.
The script works, but I dont quite understand how the function is working.
As I don't have anyone else to ask I'm asking someone here if they would be so kind to comment on the function so I can understand why it works.
Thanks
----
#!/bin/bash
# return 1 if the Uid is already used, else 0
function usedUid()
{
if [ -z "$1" ]
then
return
fi
for i in ${lines[@]} ; do
if [ $i == $1 ]
then
return 1
fi
done
return 0
}
i=0
# load all the UIDs from /etc/passwd
lines=( $( cat /etc/passwd | cut -d: -f3 | sort -n ) )
testuid=1
x=1
# search for a free uid greater than 999 (default behaviour of adduser)
while [ $x -eq 1 ] ; do
testuid=$(( $testuid + 1))
usedUid $testuid
x=$?
done
# print the just found free uid
echo $testuid