How can we write a script around dscl to loop over the currently listed IDs in use and then spit out the first id under 500 that is not in use yet?
Update # 1 (Feb 17th 2013)
I found some very helpful scripts on http://wiki.awkwardtv.org/wiki/Manage_users_and_groups_scripts which I was able to water down to the point where I could get the first available ID higher than a given number but I still don't have a way of scripting it to stop looking beyond a certain upper limit like 500.
#!/bin/sh
continue="no"
number_used="dontknow"
fnumber=300
user_id=0
until [ $continue = "yes" ] ; do
if [ `dscl . -list /Users UniqueID | awk '{print $2, "\t", $1}' | sort -ug | grep -c "$fnumber"` -gt 0 ] ; then
number_used=true
else
number_used=false
fi
if [ $number_used = "true" ] ; then
fnumber=`expr $fnumber + 1`
else
user_id="$fnumber"
continue="yes"
fi
done;
echo "Next available user_id: $user_id"
Update # 2 (Feb 17th 2013)
I suppose I could work backwards but still what if every userid from 500 to 0 is taken? I still need to set a lowerbound to get out of a messy negative ID situation.
#!/bin/sh
continue="no"
number_used="dontknow"
fnumber_work_backwards_from=500
fnumber=$fnumber_work_backwards_from
user_id=0
until [ $continue = "yes" ] ; do
if [ `dscl . -list /Users UniqueID | awk '{print $2, "\t", $1}' | sort -ug | grep -c "$fnumber"` -gt 0 ] ; then
number_used=true
else
number_used=false
fi
if [ $number_used = "true" ] ; then
fnumber=`expr $fnumber - 1`
else
user_id="$fnumber"
continue="yes"
fi
done;
echo "First available user_id which is closest to and lower than $fnumber_work_backwards_from: $user_id"