Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

problem with an IF statement

Status
Not open for further replies.

gfunk123

IS-IT--Management
May 22, 2001
143
GB
I need an IF statement that will compare the contents of the variable CX with the actual string "CP". ie. If the contents of $CX are NOT equal to the actual string "CP" then blah blah blah.

I have tried a number of things including the following.......


if [[ $CX != `echo CP` ]]; then

if [[ $CX != "CP" ]]; then

if [[ $CX != CP ]]; then


none of which seem to work

any suggestions ?

 
if [[ "$CX" != "CP" ]]; then
 
First, a question -- are you using sh or ksh or another shell?

sh doesn't support the "[[" form, so you'd need to use the single bracket form.

As Ygor pointed out, quoting the variable is always a good idea, especially with sh. If you don't, and the variable is null, the shell will evualate the variable to then try to evaulate an expression that looks like [ != CP ] and will error out because there's nothing on the left side.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top