![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| unable to format usb stick ?strange things happening as well!!! | wrapster | SUN Solaris | 3 | 05-27-2008 05:59 AM |
| Strange problem with gettimeoftheday function | xyz123456 | Linux | 1 | 04-11-2008 02:13 AM |
| shell function help | Amresh Dubey | Shell Programming and Scripting | 0 | 11-15-2007 12:16 AM |
| how to see function in shell | shriashishpatil | UNIX for Advanced & Expert Users | 2 | 05-09-2006 08:35 PM |
| Gnome doing strange things. | Furtoes00 | UNIX for Dummies Questions & Answers | 5 | 03-18-2002 12:26 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
|||
|
Very strange things happened in the shell function
Here is the script:
Code:
#!/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
Code:
./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
Did I miss something here? Thank you |
| Forum Sponsor | ||
|
|
|
|||
|
The output from FindSOS is the output of "grep EOS" plus the output of "echo $NUM".
Code:
if grep "EOS" "$1" >/dev/null # note double quotes around $1, too 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. |