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