Problem comparing String using IF stmt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Problem comparing String using IF stmt
# 1  
Old 04-30-2011
Problem comparing String using IF stmt

Hi frnds

Im facing an issues while trying to compare string using IF stmt, my code is:

Code:
chkMsgName=`Service Fee Detail`
 if [ $ctrlSrvcFeeMsgNm == $chkMsgName ]
     then
        if [ $ctrlSrvcFeeCnt == $cntWrdSrvcFee ]
        then 
            if [ $ctrlSrvcFeeLnItmCnt == $cntWrdSrvcFeeLnItm ]
            then
                echo "Valid File Ready for processing"
            fi
        fi
     fi

im getting the following error... Smilie

Code:
parsingIfNew.ksh[65]: [: Fee: unknown operator

Please help me in sorting out this error.... Thanks in advance...
# 2  
Old 04-30-2011
can you enclose the below variable to single or double quotes are try again...
chkMsgName=`Service Fee Detail`
# 3  
Old 04-30-2011
I tried, but no use, again the same error... Smilie
# 4  
Old 04-30-2011
Please try the following (copy it => same sequence) :

Code:
chkMsgName="Service Fee Detail"
if [ $ctrlSrvcFeeMsgNm == $chkMsgName ]
then
	if [ $ctrlSrvcFeeCnt == $cntWrdSrvcFee ]
	then
		if [ $ctrlSrvcFeeLnItmCnt == $cntWrdSrvcFeeLnItm ]
		then
			echo "Valid File Ready for processing"
		fi
	fi
fi

# 5  
Old 04-30-2011

Quote your variables:
Code:
chkMsgName="Service Fee Detail"
if [ "$ctrlSrvcFeeMsgNm" = "$chkMsgName" ]
then
   if [ "$ctrlSrvcFeeCnt" = "$cntWrdSrvcFee" ]
   then
      if [ "$ctrlSrvcFeeLnItmCnt" = "$cntWrdSrvcFeeLnItm" ]
      then
         echo "Valid File Ready for processing"
      fi
   fi
fi


Or, more concisely:
Code:
chkMsgName="Service Fee Detail"
if [ "$ctrlSrvcFeeMsgNm" = "$chkMsgName" ] &&
   [ "$ctrlSrvcFeeCnt" = "$cntWrdSrvcFee" ] &&
   [ "$ctrlSrvcFeeLnItmCnt" = "$cntWrdSrvcFeeLnItm" ]
then
   echo "Valid File Ready for processing"
fi

# 6  
Old 04-30-2011
The error is because the variables are not quoted!

Code:

if [ $ctrlSrvcFeeMsgNm == $chkMsgName ]
then
...
fi

what if $ctrlSrvcFeeMsgNm is not populated?
runtime code will looks like

Code:
if [ == $chkMsgName ] # of course its an error
then
...
fi

So if you think this variable may or may not have value, then you should put it within double quotes.

Code:

if [ "$ctrlSrvcFeeMsgNm" == "$chkMsgName" ]
then
...
fi

Now runtime code will look like

Code:

if [ "" == "some_value" ]
 then
 ...
 fi

cfajohnson code will work!

regards,
Ahamed
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing string's with space

How can i comparing string's whith blank spaces? I have this problem: DIF1="STRING 1212" DIF2="STRING 1212" if then echo "Differents" else echo "Equals" fi Error: 1212}: unknown test operator (3 Replies)
Discussion started by: Xedrox
3 Replies

2. UNIX for Advanced & Expert Users

Help comparing string, please

Good morning, I need compare this string. if || || ; then But this line not work, somebody can say me what is the error. Thank you for advanced. (5 Replies)
Discussion started by: systemoper
5 Replies

3. Shell Programming and Scripting

How to append a string by comparing another string?

Hi , I have one file like BUD,BDL BUDCAR BUD,BDL BUDLAMP ABC,CDF,KLT ABISKAR ABC,CDF,KLT CORNEL ABC,CDF,KLT KANNAD JKL,HNM,KTY,KJY JAGAN JKL,HNM,KTY,KJY HOUSE JKL,HNM,KTY,KJY KATAK JKL,HNM,KTY,KJY KOLKA The o/p should be like BUD,BDL BUDCAR,BUDLAMP ABC,CDF,KLT... (4 Replies)
Discussion started by: jagdishrout
4 Replies

4. UNIX for Dummies Questions & Answers

Comparing a String variable with a string literal in a Debian shell script

Hi All, I am trying to to compare a string variable with a string literal inside a loop but keep getting the ./testifstructure.sh: line 6: #!/bin/sh BOOK_LIST="BOOK1 BOOK2" for BOOK in ${BOOK_LIST} do if then echo '1' else echo '2' fi done Please use next... (1 Reply)
Discussion started by: daveu7
1 Replies

5. Shell Programming and Scripting

Problem in comparing 2 files string by string

Hi Champs, I am a newbie to unix world, and I am trying to built a script which seems to be far tough to be done alone by me..... " I am having a raw csv file which contains around 50 fields..." From that file I have to grep 2 fields "A" and "B"....field "A" is to be aligned vertically... (11 Replies)
Discussion started by: jitendra.pat04
11 Replies

6. Programming

string comparing in C

Hello, I need help with a program I'm trying to write for my moms science class, what it has to do is accept a user inputed string and search for it in a text file (file contains all the elements) The file looks like: H Hydrogen 1 He Helium 2 Li Lithium 3 Be Beryllium 4 ... If the... (0 Replies)
Discussion started by: duvalC
0 Replies

7. Programming

string comparing in C

Hello, I need help with a program I'm trying to write for my moms science class, what it has to do is accept a user inputed string and search for it in a text file (file contains all the elements) The file looks like: H Hydrogen 1 He Helium 2 Li Lithium 3 Be Beryllium 4 ... If the... (1 Reply)
Discussion started by: duvalC
1 Replies

8. Shell Programming and Scripting

Comparing string and integer in IF

hi, I need to create an IF condition. I read a line from a file and get the 5 word using space as a delimited. This word can have only two values either '*' or '1-5' I need to write an IF condition for two cases. I can either compare it to * or 1-5(or even 1 by cutting and getting only the... (3 Replies)
Discussion started by: kaushys
3 Replies

9. Shell Programming and Scripting

problem in comparing numeric with string

Hi all, I am having a problem in comparing numeric value with string. I have a variable in my script which gets the value dynamically. It can be a numeric value or a string. I have to do separate task based on its value numeric or sting variable VARIABLE. I grep FILE_COUNT and obtained... (7 Replies)
Discussion started by: naren_0101bits
7 Replies

10. UNIX for Dummies Questions & Answers

counter / increment problem within echo stmt

Simple script trying to increment a counter within an echo statement never gets past 1 - PLEASE HELP! Thanks. ~~~~~~~~~~~ #!/bin/sh stepup() { STEP=`expr $STEP + 1` echo $STEP } # # Initialize variables # STEP=0 echo "Counter Value: `stepup`" echo "Counter Value:... (2 Replies)
Discussion started by: blaze
2 Replies
Login or Register to Ask a Question