- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Test in script
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2006 11:36 AM
02-01-2006 11:36 AM
i am using the following syntext for checking a condition.
echo "Enter the user name"
read USER
X=`finger $USER |grep "In real life" |cut -d ":" -f3`
if test $X = "???"
then
echo "No such user please try again"
else
echo "Enter the login name"
read LNAME
While there is no user ie the condition is true it works fine but when i enter a valid user name it works but puts a screen output
./menu.sh[25]: Ayub: A test command parameter is not valid.
How can i resolve this.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2006 12:12 PM
02-01-2006 12:12 PM
Re: Test in script
a) A user can be perfectly valid and not
have the GECOS filed set up
(the fifth coma-separated field in the
standard password file).
Hence, finger(1) can give you a VERY WRONG
answer.
We have lot of sites that for one reason or other have no GECOS fields for certain
usernames.
b) If the username is valid you are asking
for another input in your script
("read LNAME"). There is no need for it.
c) Otherwise, if you are not using Trusted Systems password database, it is easier to
rely on the password file. The data that you
are extracting via finger(1) are actually the fifth coma-seperated field in the password record.
Something like this (wrote and tested in less that a minute):
#!/bin/sh
echo "Enter the user name"
read USER
X=`awk -F: '{if ($1 == "'$USER'") print}' /etc/passwd`
GECOS=`echo $X | awk -F: '{print $5}'`
if ! test "$X"
then
echo "No such user please try again"
else
echo "Valid user $USER"
if test "$GECOS"
then
print "User $USER described as \"$GECOS\""
else
print "User $USER not listed in password file GECOS"
fi
fi
exit 0
Regards,
Dusan
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2006 12:16 PM
02-01-2006 12:16 PM
SolutionIf you enclose the variable you are testing in double quotes you will avoid the error "A test command parameter is not valid" when the variable is empty.
The other essential problem with the logic you wrote is that 'finger'ing a valid user returns more than one line of output.
You could amend your script to something along these lines:
#!/usr/bin/sh
while true
do
echo "Enter the user name"
read USER
[ -z "${USER}" ] && continue
X=`finger $USER |head -1|cut -d ":" -f3`
if [ "${X}" = " ???" ]
then
echo "No such user please try again"
else
echo "(ok)"
fi
done
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2006 12:36 PM
02-01-2006 12:36 PM
Re: Test in script
I am using a trusted system. how can i check weither a user is existant or not.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2006 12:53 PM
02-01-2006 12:53 PM
Re: Test in script
A simple way to test for the presence of a user in '/etc/passwd' (which works on trusted or untrusted systems) is to use 'grep' and its return status to yield a true or false answer:
# U=jrf;grep -q "^${U}:" /etc/passwd && echo "ok" || echo "no_user"
The caret (^) anchors the match to the beginning of the line. The ":" serves as the field's boundry. A return status of zero means "ok" otherwise if no match occurs, "no_user" is printed.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2006 02:26 PM
02-01-2006 02:26 PM
Re: Test in script
If you just want to find out if an account
exist in TCB database (Trusted Systems),
then:
/usr/lbin/getprpw username
It is not now clear what exactly you wish
to accomplish (find out if a username
exists, or find out a GECOS field for a
user :))
Regards,
VK2COT
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2006 04:31 PM
02-01-2006 04:31 PM
Re: Test in script
echo "Enter the user name"
read USER
id ${USER} 1>/dev/null 2>&1
if [[ ${?} -eq 0 ]]
then
echo "No such user please try again"
else
echo "Enter the login name"
read LNAME
fi
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-01-2006 04:34 PM
02-01-2006 04:34 PM
Re: Test in script
You asked about "I am using a trusted system. how can i check weither a user is existant or not."
You can simply use, # /usr/lbin/getprpw
-Arun