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

IF-THEN : expression syntax error 2

Status
Not open for further replies.

RVSachin

Technical User
Nov 12, 2001
77
IN
Hi,

Can somebody point me why I get "expression syntax error" for the following Korn shell script?
Code:
      x=`awk '/Info: .*.abc/{print $2}' results.log`
	y=`awk '/Info: .*.txt/{print $2}' results.log`
	z=`awk '/Total: /{print $2}' results.log`
		if [ $x == $z ];
		then
			echo "match"  >> grep.txt					
		else
			echo "does not match" >> grep.txt
		fi

I am not initializing x, y, z.
 
Why the ; after the if statement? Doesn't this make what follows completely separate from the if itself? Have you tried removing it? HTH.
 
I made a few assumptions .

I assumed you file looked something like
[tt][blue]
Info: me.abc 123
Info: you.txt 456
Total: 579
[/blue][/tt]

I also assumed that you wanted to compare a couple of numerical values
First - I think you script is failing becasue you are using == instead of =.
== Numerical comparision
= string comparison
[tt][blue]
(( a == b )) is a mathmatical expresion
equal to true when a & b are
integers AND they are equal.
(( a = b )) is a mathmatical expresion which
sets a equal to the value of b.

In KSH mathmatical expresions need
contained in (( )) or $(( ))

(( )) - boolean
(( 1 > 3 )) false
(( 1 < 2 )) true

$(( )) - kinda of command substition
x=$(( 5 + 3 )) x=8

[[ $a = $b ]] is a logical expresion which
evaluates to true if the strings
a and b are equal

[/blue][/tt]

In your script you use == instead of =.

Like I said I assumed your file was formated like the above. Which means the values you want to compare for the Info record are in the third field. So if you are doing a number compare then
[tt][green]
x=`awk '/Info: .*.abc/{print $3}' results.log`
y=`awk '/Info: .*.txt/{print $3}' results.log`
z=`awk '/Total: /{print $2}' results.log`
if (( x == z ));
then
echo "match" >> grep.txt
else
echo "does not match" >> grep.txt
fi
[/green][/tt]

"$" is not necessary inside (( ))
If you want to compare STRING values
[tt][green]
x=`awk '/Info: .*.abc/{print $3}' results.log`
y=`awk '/Info: .*.txt/{print $3}' results.log`
z=`awk '/Total: /{print $2}' results.log`
if [[ $x = $z ]] ;
then
echo "match" >> grep.txt
else
echo "does not match" >> grep.txt
fi
[/green][/tt]


One more thing, it took me a while to figure out what the ; was used for , I usually left it off and it is not needed in your script, BUT...
Every Unix command produces a True or False, so for example lets say you wanted to echo a statement if you find the value FOOBAR in a file called my.txt
[tt][green]
if grep -q FOOBAR my.txt; then
echo "I found FOOBAR"
else
echo "I did not find FOOBAR"
fi
[/green][/tt]

JRjr [morning]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top