1847156 Members
5845 Online
110263 Solutions
New Discussion

Re: Test in script

 
SOLVED
Go to solution
Khashru
Valued Contributor

Test in script

Hi,

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.
7 REPLIES 7
VK2COT
Honored Contributor

Re: Test in script

Hello,

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
VK2COT - Dusan Baljevic
James R. Ferguson
Acclaimed Contributor
Solution

Re: Test in script

Hi:

If 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...
Khashru
Valued Contributor

Re: Test in script

Hi,

I am using a trusted system. how can i check weither a user is existant or not.
James R. Ferguson
Acclaimed Contributor

Re: Test in script

Hi :

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...
VK2COT
Honored Contributor

Re: Test in script

Hello,

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
VK2COT - Dusan Baljevic
Muthukumar_5
Honored Contributor

Re: Test in script

You can simply use as,

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
Easy to suggest when don't know about the problem!
Arunvijai_4
Honored Contributor

Re: Test in script

Hello,

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
"A ship in the harbor is safe, but that is not what ships are built for"