comparing two strings

Hi

How do i compare two strings in shell script. Below is an example but I am not getting the desired output, plz help


if [ $Name = 'Ragha' AND $Status = 'Active' ]
then
echo success
fi

I am not getting the desired output if I do this. plz help

doing 'man test' yields the following:

          condition1 -a condition2
                True if both condition1 and condition2 are  true.
                The -a binary primary is left associative and has
                higher precedence than the -o binary primary.

          condition1 -o condition2
                True if either condition1 or condition2 is  true.
                The -o binary primary is left associative.

hi

what if I have a condition like this


if [ $Name = 'Ragha%' -a  $Status = 'Active%' ]
then
echo success
fi

I am not getting the result in this case

what are the values of '$Name' and '$Status' ?
can you can the script with the 'set -x' option and provide the output?

Hi vgersh..

The problem is actually with the loop. The condition is working fine. In this code when the condition satisifies it has to give output an output like this

sble01
ALLTEL - WebOrders WorkMon Shutting
down
success

sble01
ALLTEL WorkMon24Hrs
Running

but it is giving output as below
sble01
ALLTEL - WebOrders WorkMon Shutting
down
success

success <----------------This shouldnt appear here
sble01
ALLTEL WorkMon24Hrs
Running

pls see the code and find out the error

until [ $i -eq ${#wfname[*]} ]
do
      
$SIEBEL_ROOT/bin/srvrmgr -g $SIEBEL_GATEWAY -e $SIEBEL_ENTERPRISE -s $SIEBEL_SERVER -u n9912499 -p n9912499 -c "list component '${wfname[$i]}' show SV_NAME, CC_NAME, CP_DISP_RUN_STATE" -o "$temp_output"  
sed -n "/^SV_NAME/,$ p" < $temp_output | sed "/rows* returned/,$ d" | awk '
NR > 2 {n = split($0,arr," ")
printf("%s|",arr[1])
for( k = 2; k <=n-1;++k) printf("%s ",arr[k])
printf("\b|%s\n",arr[n]) }' > temp
old_IFS=$IFS
IFS="|"
while read SV_NAME CC_NAME CP_DISP_RUN_STATE
do
echo $SV_NAME
echo $CC_NAME
echo $CP_DISP_RUN_STATE

if [ "${wfname[$i]}" != "ALLTEL - WebOrders WorkActn" ]
then
if [ "$CP_DISP_RUN_STATE" != "${status[0]}" ]
then
echo success
fi
elif [ "${wfname[$i]}" = "ALLTEL - WebOrders WorkActn" ] 
then
if [ "$CP_DISP_RUN_STATE" != "${status[1]}" ]
then
echo good
fi
fi
done < temp
((i=i+1))
done

thanks a lot

I really don't have much time to debug tthis, but you could start [as suggested] by putting the 'set -x' at the top of your script and see what gets executed and how things get 'compared'.

Good luck.

i did a set -x.. this is the output I get

 + read SV_NAME CC_NAME CP_DISP_RUN_STATE
+ echo sble01
sble01
+ echo ALLTEL - WebOrders WorkMon Shutting
ALLTEL - WebOrders WorkMon Shutting
+ echo down
down
+ [ ALLTEL - WebOrders WorkMon != ALLTEL - WebOrders WorkActn ]
+ [ down != Running ]
+ echo success
success
+ read SV_NAME CC_NAME CP_DISP_RUN_STATE
+ echo

+ echo
+ echo

+ [ ALLTEL - WebOrders WorkMon != ALLTEL - WebOrders WorkActn ]
+ [  != Running ]
+ echo success
success
+ read SV_NAME CC_NAME CP_DISP_RUN_STATE
+ (( i=i+1 ))
+ [ 1 -eq 2 ]
+ /siebel/sble01/siebsrvr/bin/srvrmgr -g lfs6217q.alltel.com -e esble -s sble01 -u n9912499 -p n9912499 -c list component 'ALLTEL WorkMon24Hrs' show SV_NAME, CC_NAME, CP_DISP_RUN_STATE -o /siebel/sble01/siebfile/batch//WorkflowMon/log/temp_20060920_170358.out
+ sed -n /^SV_NAME/,$ p
+ 0< /siebel/sble01/siebfile/batch//WorkflowMon/log/temp_20060920_170358.out
+ sed /rows* returned/,$ d
+ awk
NR > 2 {n = split($0,arr," ")
printf("%s|",arr[1])
for( k = 2; k <=n-1;++k) printf("%s ",arr[k])
printf("\b|%s\n",arr[n]) }
+ 1> temp
+ old_IFS=|
+ IFS=|
+ set -x
+ 0< temp
+ read SV_NAME CC_NAME CP_DISP_RUN_STATE
+ echo sble01
sble01
+ echo ALLTEL WorkMon24Hrs
ALLTEL WorkMon24Hrs
+ echo Running
Running
+ [ ALLTEL WorkMon24Hrs != ALLTEL - WebOrders WorkActn ]
+ [ Running != Running ]
+ read SV_NAME CC_NAME CP_DISP_RUN_STATE
+ echo

+ echo
+ echo

+ [ ALLTEL WorkMon24Hrs != ALLTEL - WebOrders WorkActn ]
+ [  != Running ]
+ echo success
success
+ read SV_NAME CC_NAME CP_DISP_RUN_STATE
+ (( i=i+1 ))
+ [ 2 -eq 2 ]

Can you help me with this..

this looks like your problem:

+ read SV_NAME CC_NAME CP_DISP_RUN_STATE
+ echo

+ echo
+ echo

+ [ ALLTEL WorkMon24Hrs != ALLTEL - WebOrders WorkActn ]
+ [  != Running ]
+ echo success
success

seems like none of the variables in green got 'read' - their 'echo-s' [and therefore values are 'empty'.
therefore your 'if-s' succeed outputing 'sucess'.

Can you help me rectify that. I have no idea as to how to solve that..

you have to make sure that whatever the 'read' statement reads in are valid value that you later on do comparison on.

looking at it for 1 minute, it looks like you're READing a 'blank' line whereas you're expecting at least THREE fields separated by '|'.

I actually have to read the line next to the blank line. how do i modify my code for that. sorry for the trouble. i am new to shell scripts.

the READ part was done by somebody else.

thanks

My log file looks like below. I am actually reading the values from this file. I have to extract the values under SV_NAME, CC_NAME and CP_DISP_RUN_STATE in a variable and use that for comparison. pls help when you find some time.


Siebel Enterprise Applications Siebel Server Manager, Version 7.5.3.12 [16272] LANG_INDEPENDENT 
Copyright (c) 2001 Siebel Systems, Inc.  All rights reserved.

This software is the property of Siebel Systems, Inc., 2207 Bridgepointe Parkway,
San Mateo, CA 94404.

User agrees that any use of this software is governed by: (1) the applicable
user limitations and other terms and conditions of the license agreement which
has been entered into with Siebel Systems or its authorized distributors; and
(2) the proprietary and restricted rights notices included in this software.

WARNING: THIS COMPUTER PROGRAM IS PROTECTED BY U.S. AND INTERNATIONAL LAW.
UNAUTHORIZED REPRODUCTION, DISTRIBUTION OR USE OF THIS PROGRAM, OR ANY PORTION
OF IT, MAY RESULT IN SEVERE CIVIL AND CRIMINAL PENALTIES, AND WILL BE
PROSECUTED TO THE MAXIMUM EXTENT POSSIBLE UNDER THE LAW.

If you have received this software in error, please notify Siebel Systems
immediately at (650) 295-5000.

Type "help" for list of commands, "help <topic>" for detailed help

Connected to 1 server(s) out of a total of 1 server(s) in the enterprise

srvrmgr:sble01> list component 'ALLTEL - WebOrders WorkActn' show SV_NAME, CC_NAME, CP_DISP_RUN_STATE

SV_NAME  CC_NAME                      CP_DISP_RUN_STATE  
-------  ---------------------------  -----------------  
sble01   ALLTEL - WebOrders WorkActn  Online             

1 row returned.

srvrmgr:sble01> 

a 'quick and durty' fix to a not-so-elegate code....

change

NR > 2

TO

NR > 2 && NF

good luck.

if i do what you said. i am getting syntax error like this


awk: syntax error near line 2
awk: bailing out near line 2

kindly help

@ragha: try this..

if [[ $name -eq "Ragha" && $status -eq "Active" ]]
then
        echo success
fi

this working fine..... :slight_smile:

the problem is not with comparison as I initially thought. thanks for your reply. the problem appears with the code. can you look at the code and tell me what the problem could be. thanks

pls post what you have changed your code to.
btw, what OS are you running under?

I changed the code as you told like this


NR > 2 && NF  from NR > 2

but I get an error like this


awk: syntax error near line 2
awk: bailing out near line 2

My Operating system is Solaris 5.8

Once again - pls post the ENTIRE script with the change.

also if you're running under solaris, change 'awk' to 'nawk'.