Here's a quick code snippet for use in extracting the current highest
user identifier (UID) in GNU/Linux using awk to do so. For reference: a UID is the unique value your UNIX-like operating system (OS) uses to identify an account and, when used in combination with other components of the OS, determines what system resources said account may access.
UIDs are stored in the world-readable /etc/passwd
file, described thoroughly by The Linux Information Project (LINFO) here. In a given entry (e.g.:
linus:x:1001:1001::/home/linus:/bin/bash
), each of the seven (7) fields (name, password, user ID, group ID, gecos, home directory, shell) are separated by a colon (:) with no spaces.
To pull a UID from the /etc/passwd
file via awk
, then, one would use the -F
flag and set a colon (:
) as the field separator, printing the third column ({print $3}
), provided the condition exists. As we're looking for the highest UID, we pipe our results | tail -1
to pull the very last entry from /etc/passwd
. With that number in hand, we can simply add to it with a +1
.
Take a look:
get_uid() {
uid=$(awk -F ":" '/1/ {print $3}' /etc/passwd |tail -1)
increment_uid=$((uid +1))
printf "%s\\n" "Assigning uid:$increment_uid to $username"
}
Cheers.