Operating System - HP-UX
1753888 Members
7682 Online
108809 Solutions
New Discussion юеВ

Re: How to write a script to find out whether one user id is available or not

 
senthil_kumar_1
Super Advisor

How to write a script to find out whether one user id is available or not

Hi All,

I would like to write a script to find out whether one user id is available or not...

And I would like to print the message "User is available" / "User is not available".
3 REPLIES 3
James R. Ferguson
Acclaimed Contributor

Re: How to write a script to find out whether one user id is available or not

Hi Senthil:

# USER=senthil
# grep -q ^${USER}: /etc/passwd && echo "Valid" || echo "Invalid"

...set the value of 'USER' accordingly.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: How to write a script to find out whether one user id is available or not

Hi (again):

If you meant a UID (e.g. 1000) then this variation would suffice:

# UID=1000
# grep ^.*:.*:${UID}: /etc/passwd && echo "Present" || echo "Open"

Regards!

...JRF...
Steven E. Protter
Exalted Contributor

Re: How to write a script to find out whether one user id is available or not

Shalom,

Some Unix/Linux systems will not allow the same text Id to be used twice. Ran into that on Solaris.

avail.sh

echo "enter desired id:"
read idhold
echo "Enter desired uid:"
read uidhold

foundid=$(grep $idhold /etc/passwd |wc -l)
founduid=$(grep $uidhold /etc/passwd |wc -l)

if [ $foundid -eq 0 && $founduid -eq 0 ]
then
echo "Good to go with $idhold $uidhold "
else
echo "Bad admin, no re-use of ids..."
fi

Obviously tune your message to the actual users of the scripts.
Steven E Protter
Owner of ISN Corporation
http://isnamerica.com
http://hpuxconsulting.com
Sponsor: http://hpux.ws
Twitter: http://twitter.com/hpuxlinux
Founder http://newdatacloud.com