1830241 Members
5830 Online
109999 Solutions
New Discussion

Re: Script problem

 
Khashru
Valued Contributor

Script problem

Hi,

I am planning to use the following script for changing user password. The script working fine when i supply non existant user. It also works fine when i put an existing user but throws a error in the screen
./menu.sh[27]: Ayub: A test command parameter is not valid.

#!/bin/sh
clear # Clear the screen.
echo " ------- ----"
echo "Choose one of the following option:"
echo
echo "[P] for Password Change"
echo "[E] for Exit"
echo
read OPTION
case "$OPTION" in
"E" | "e" )
# Accept upper or lowercase input.
echo
echo "You chose to exit"
;;
# Note double semicolon to terminate each option.
"P" | "p" )
echo
echo "You choose password change"
echo "Enter the user name"
read USER
X=`finger $USER |grep "In real life" |cut -d ":" -f3`
finger $USER | grep "In real life" |tr -s " " |cut -d ":" -f2,3| cut -d " " -f2,6,7,8|grep -n " ">>list.txt
echo "Select userlogin from the following list"
cat list.txt
rm list.txt
if test $X = "???"
then
echo "No such user please try again"
else
echo "Enter the login name"
read LNAME
if test $LNAME = "jgan"
then
echo "You cannot change root password"
else
echo "password of "$LNAME" is changed"
fi
fi
;;
esac

How can i ommit that.
4 REPLIES 4
A. Clay Stephenson
Acclaimed Contributor

Re: Script problem

Your probem is a $X can be a null string and that is an error in test

if test $X = "???"
should be
if test "${X}" = "???" #note the quotes

The more generally used form is
if [ "${X}" = "???" ] #note the space after '[' and before ']' -- very important

You should quote every variable that is tested that might be a null string.
If it ain't broke, I can fix that.
Khashru
Valued Contributor

Re: Script problem

Hi,

That problem is solved but it is now not working for a invalid user.

Muthukumar_5
Honored Contributor

Re: Script problem

You can try instead of,

X=`finger $USER |grep "In real life" |cut -d ":" -f3`
finger $USER | grep "In real life" |tr -s " " |cut -d ":" -f2,3| cut -d " " -f2,6,7,8|grep -n " ">>list.txt
echo "Select userlogin from the following list"
cat list.txt
rm list.txt

as,

id ${USER}
if [[ $? -ne 0 ]]
then
echo "No such user please try again"
else
echo "Enter the login name"
read LNAME
if [[ "${LNAME}" = "jgan" ]]
then
echo "You cannot change root password"
else
echo "password of "$LNAME" is changed"
fi
fi

It will work.

--
Muthu
Easy to suggest when don't know about the problem!
Khashru
Valued Contributor

Re: Script problem

In this case i have to supply the login name. I am planning to give this to help desk. they will only know the real name. i want to display the login name from that real name so that they can enter the login name.