The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-29-2008
Registered User
 

Join Date: Jan 2008
Posts: 24
Stumble this Post!
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
Here is the result:
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
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
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-30-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,614
Stumble this Post!
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
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.
Reply With Quote
  #3 (permalink)  
Old 03-30-2008
Registered User
 

Join Date: Jan 2008
Posts: 24
Stumble this Post!
Thanks again, era. You are a master in this field! Your suggestion makes my script much easier. Keep learning from you...
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
bash, bash eval, eval

Thread Tools
Display Modes




All times are GMT -7. The time now is 10:05 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0