LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   if condition (https://www.linuxquestions.org/questions/programming-9/if-condition-822865/)

shashv2 07-29-2010 09:07 AM

if condition
 
I am trying to compare two strings using the following code but it gives me error. I have tried couple of options but it does not work.
The code is

read -p "Do you want to continue ? Y/N - " opt
if ["$opt"== "N"];then
do something
fi


and the error I get


./newtest.sh: line 20: [N==: command not found

I have tried this way as well

#!/bin/bash
S1='string'
S2='String'
if [ $S1=$S2 ];


It still gives the same errror.

Any suggestions?

Thanks,

crts 07-29-2010 09:11 AM

Quote:

Originally Posted by shashv2 (Post 4049107)

read -p "Do you want to continue ? Y/N - " opt
if ["$opt"== "N"];then
do something
fi

./newtest.sh: line 20: [N==: command not found

There has to be a space between [ and N. Also at the end of the expression between " and ].
Code:

if [  "$opt"== "N"  ];then
    do something
fi

The '[' bracket is the same as the test command.

shashv2 07-29-2010 09:52 AM

Thanks for the quick reply.

Last time (as in my previous thread) it gave an error due to space and in this case it gives error if there is a space.

The spaces in "IP = ..." could be the problem. When you initialise variables in Bash, you must not use spaces.

Your help is very much appreciated.


All times are GMT -5. The time now is 10:23 AM.