Very strange things happened in the shell function

Here is the script:

#!/bin/bash
#set -xv
RAWDATAFILE="temp1"
GOODDATAFILE="temp2"
FindSOS()
{
local NUM=0 #return value for the id of SOS
local cnt=1
if grep "EOS" $1
then
sed -e 's/.*<CobDate>//' -e 's/<\/.*//' <$1 > ${RAWDATAFILE}
sed -n -e '/EOS/,/SOS/ s/[0-9]*/&/p' <${RAWDATAFILE} >${GOODDATAFILE}
NUM=`more ${GOODDATAFILE} |sed -e 's/<EOS\/>//' -e 's/<SOS\/>//' |grep -v '^$'`
else            #The <SOS/> must be in the second line, the required EID is in the
                # first line
while read line
do
if [ ${cnt} -eq 1 ]; then
eval NUM=${line}
break
fi
cnt=$(($cnt+1))
done <$1

fi

echo $NUM
}

echo $NUM
exit 0

Here is the result:

./FindSOS.ksh sosofile
RAWDATAFILE="temp1"
+ RAWDATAFILE=temp1
GOODDATAFILE="temp2"
+ GOODDATAFILE=temp2
FindSOS()
{
local NUM=0 #return value for the id of SOS
local cnt=1
if grep "EOS" $1
then
sed -e 's/.*<CobDate>//' -e 's/<\/.*//' <$1 > ${RAWDATAFILE}
sed -n -e '/EOS/,/SOS/ s/[0-9]*/&/p' <${RAWDATAFILE} >${GOODDATAFILE}
NUM=`more ${GOODDATAFILE} |sed -e 's/<EOS\/>//' -e 's/<SOS\/>//' |grep -v '^$'`
else            #The <SOS/> must be in the second line, the required EID is in the
                # first line
while read line
do
if [ ${cnt} -eq 1 ]; then
eval NUM=${line}
break
fi
cnt=$(($cnt+1))
done <$1

fi

echo $NUM
}
id=`FindSOS $1`
FindSOS $1
++ FindSOS sosofile
++ local NUM=0
++ local cnt=1
++ grep EOS sosofile
++ sed -e 's/.*<CobDate>//' -e 's/<\/.*//'
++ sed -n -e '/EOS/,/SOS/ s/[0-9]*/&/p'
more ${GOODDATAFILE} |sed -e 's/<EOS\/>//' -e 's/<SOS\/>//' |grep -v '^$'
+++ more temp2
+++ sed -e 's/<EOS\/>//' -e 's/<SOS\/>//'
+++ grep -v '^$'
++ NUM=2080370166
++ echo 2080370166
+ id='<EOS/>
2080370166'
echo $id
+ echo '<EOS/>' 2080370166
<EOS/> 2080370166
exit 0
+ exit 0

I feel strange about that in the function, the NUM=2080370166, but why out of the function, id get the value, the value changes to "<EOS/> 2080370166"

Did I miss something here? Thank you

The output from FindSOS is the output of "grep EOS" plus the output of "echo $NUM".

if grep "EOS" "$1" >/dev/null  # note double quotes around $1, too

should fix it. (Or grep -q, if you have a fairly modern grep.)

The use of more | sed is an inventive way of avoiding a Useless Use of Cat Award (google for that), but I certainly would not recommend it.

As another stylistic issue, you could avoid the use of temporary files. Pipes are your friend and you are using them already. A file which only gets used once (by the immediately following command, too) can usually be turned into a pipe.

That while loop looks fairly mysterious, too. You will break out on the first line, correct? So you don't need the magic with $cnt and in fact it will never get executed. The idiomatic way to do that is head -n 1 anyway.

The funny | grep -v '^$' is superfluous too, as long as you are echoing $NUM unquoted, any blanks will be lost anyway.

Hope this helps.

Thanks again, era. You are a master in this field! Your suggestion makes my script much easier. Keep learning from you...