Setting script exit code

#!/bin/ksh
row=`sed '1!G;h;$!d' file1.xml | head -2| tail -1`
echo "$row" | awk -F"[;: ]" '{$esum=$5}'

row=`sed '1!G;h;$!d' file2.xml | head -2| tail -1`
echo "$row" | awk -F"[;: ]" '{$isum=$5+$19}'

echo "Exp:$esnum"
echo "Imp:$isum"
if [$esum -eq $isum]
then
 echo "Matched"
else
 echo "Not matched"
fi

a) In the above code my values for esum & isum are becomin 0 at the if condition. How to retain the values thru out the program?
b) Instead of "Matched", exit code should be set to "0" and in ELSE part exit code shuold be set to "1". How to do this?

Please help....

IF (condition 1)
set exit code 0
else
set exit code 1

you may not need to mention "set exit code 0"

IF ( Condition 1)
then
do something
exit 234
else
do something else
exit 242

Wherein you know the exit status post execution of the script

./script
echo $?

so if it is 234 ==> condition met
or
242 ==> condition failed

There are additional queris. Could you please have a look again?

Oops, the varaible used in the awk , won't be retained outside.

You should use something like this:

row=`sed '1!G;h;$!d' file1.xml | head -2| tail -1`
esum=`echo "$row" | awk -F"[;: ]" '{$esum=$5}'`

row=`sed '1!G;h;$!d' file2.xml | head -2| tail -1`
isum=`echo "$row" | awk -F"[;: ]" '{$isum=$5+$19}'`
echo "Exp:$esnum"
echo "Imp:$isum"
if [ $esum -eq $isum ]
then
 echo "Matched" ## you can use exit 0
else
 echo "Not matched" ## you can use exit 1
fi

Sorry for the goof up :slight_smile:

The code is not working. Still its printing blank i.e the values are still not accesible outside. do we need any modification????

You did not print the variables in the awk , so it should be :

 
row=`sed '1!G;h;$!d' file1.xml | head -2| tail -1`
esum=`echo "$row" | awk -F"[;: ]" '{print $5;}'`

row=`sed '1!G;h;$!d' file2.xml | head -2| tail -1`
isum=`echo "$row" | awk -F"[;: ]" '{print $5+$19}'`

echo "Exp:$esum"
echo "Imp:$isum"
if [ $esum -eq $isum ]
then
echo "Matched" ## you can use exit 0
else
echo "Not matched" ## you can use exit 1
fi