Operating System - HP-UX
1752815 Members
6442 Online
108789 Solutions
New Discussion юеВ

Re: Scripting the creation of HP-UX user accounts

 
John M Clark
Occasional Advisor

Scripting the creation of HP-UX user accounts

Hi, not sure if any one can help, we have a lagacy application where all database authentication is controlled from the /etc/passwd.

We have UID and GIG ranges for each department. I am trying to write a script the looks and creates the next available UID in the assigned range.

I believe I will need somthing like

UID must must greater than "UIDLOW" and less than "UIDHIGH" and create UID one greater than the lowest UID in the range.

Any help would be most appreciated

Regards

John
2 REPLIES 2
Matti_Kurkela
Honored Contributor

Re: Scripting the creation of HP-UX user accounts

You'll need a shell script/function that iterates over the assigned range from UIDLOW to UIDHIGH and stops when it finds a free UID.

How about something like this:

#!/bin/sh

iterateUID () {
UIDLOW="$1"
UIDHIGH="$2"

ASSIGN_UID=none
CURR_UID="$UIDLOW"

while [ "$CURR_UID" -lt "$UIDHIGH" ]; do
grep -q "^[^:]*:[^:]*:$CURR_UID:" /etc/passwd
if [ $? = 0 ]; then
# UID is in use
CURR_UID=$( expr "$CURR_UID" + 1 )
continue
else
# found an UID
ASSIGN_UID="$CURR_UID"
break
fi
done
if [ "$ASSIGN_UID" = "none" ]; then
# could not find an UID
echo "ERROR: could not find a free UID. Range full?" >&2
echo "none"
return 1
fi

echo "$ASSIGN_UID"
return 0
}

If the function manages to find a suitable UID, it outputs it to stdout. If no free UID was found, it outputs an error message to stderr, sends the string "none" to stdout and returns with an error code.

After defining this function near the beginning of your script, you can use it like this:

# to find an UID between 100 and 200
USER_UID=$( iterateUID 100 200 )
if [ $? -gt 0 ]; then
# things to do if no UID found
fi

There are probably other, more resource-saving ways to do this.

For GIDs, change the variable names as appropriate and the "grep" line to:

grep -q "^[^:]*:[^:]*:$CURR_GID:" /etc/group

MK
MK
John M Clark
Occasional Advisor

Re: Scripting the creation of HP-UX user accounts

Excellent feedback
thanks again