If condition problem

Hi Guys,

I want to use if conition for my script. Before I used it tried it with some small test scripts. But it was not succeeded. My script and screen output as follows,

Script:

echo 'Do you think Yes or No (y/n) : '
read ans
echo You input anser as $ans
ans1=y

if ( $ans == $ans1 )
then

 echo good!

else
echo bad!
fi

screen output

Do you think Yes or No (y/n) :
y
You input anser as y
y: not found
bad!

Do you think Yes or No (y/n) :
n
You input anser as n
n: not found
bad!

Can somebody please pleasse tell me what is the error ?
I tried this several times with different formats in tcshell.

Regards,
Mahesh Fernando.
:slight_smile:

the operator "==" is not valid in shell script.
you can try as " if ( $ans = $ans1 )"

Hi budy,

Thanks for the quick reply.
This syntax also not working.

Maeshsri
:rolleyes:

Why dont you try it in a different shell say bash.

Look at this

sh-2.05b$ cat mahesh.sh 
#! /bin/sh

echo 'Do you think Yes or No (y/n) : '
read ans
echo You input anser as $ans
ans1=y

if [ $ans == $ans1 ] ; then
echo good!
else
echo bad!
fi ;
sh-2.05b$ ./mahesh.sh 
Do you think Yes or No (y/n) : 
y
You input anser as y
good!
sh-2.05b$ ./mahesh.sh 
Do you think Yes or No (y/n) : 
n
You input anser as n
bad!

vino

Hi
You should use square bracket in place of parenthesis.
I mean U write like this "if [ $ans = $ans1 ]".
it should work.

Hi Guys,

Thank you very much for the suppot you all gave me to solve this proble.
It is working fine now.

Warm Regards,
Maheshsri
:slight_smile: