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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Comparing strings in 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 ?



 
Syntax will depend on which shell you are using but you can try:

if [ ! x$CX = xCP ] ; then
fi

the lower case x's are there to stop a null string in $CX from causing an error.
 
In the Korn shell, you do pattern matching with a [tt]@(<pattern>)[/tt]. You would code your example as...
[tt]
if [[ ${CX} != @(CP) ]]; then
[/tt]
This is actually more useful when you have wilcards. You can do something like...
[tt]
CX=ABCDEF

if [[ ${CX} != @(A*F) ]] ; then print 'Not A*F' ; fi
if [[ ${CX} = @(A*F) ]] ; then print 'IS A*F' ; fi
[/tt]
Actually, the last one you mention...
[tt]
if [[ $CX != CP ]]; then
[/tt]
...should work. That's valid syntax for the Korn shell. Are you sure you're in the Korn shell? Also, what error messages are you getting? Are you sure [tt]CX[/tt] is defined at that point in your script?

Hope this helps.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top