1. Need help parsing a file in sh
I need to read a file line-by-line in Bourne shell, parsing the lines as I
read, and I need to remember how many lines I read before EOF. The code that
I tried looks like this:
$!/bin/sh
i=1
IFS='|'
while [ $eof -ne 0 ]
do
read a b c
if [ $? -ne 0 ]
then
last=`expr $i -1`
eof=0
else
echo $i) $a $b $c
i=`expr $i + 1`
fi
done < file.dat
echo $last $i
Ok, so my first question is why/how does this work. The idea to redirect
from file.dat on the done line was borrowed from a Korn shell script that
does the same thing, and it is not at all apparent why or how it works, at
least not to me.
My next question concerns an unintended effect of the redirection, which
probably would help to answer the first question. If you run my script as
above you will notice that when the last line is executed, nothing is
printed out for $last and that the value of i is 1, the value to which it
was initialized outside the while loop.
After some experimenting it became aparent that any variables assigned within
the while loop were not available outside the loop. Thinking that the
redirection may have something to do with this, I changed the while condition
to just count to 10 and removed the redirection operator from the done line.
Now both $last and $i printed out the expected values.
I suspected that the shell might be forking another process to handle the
while loop with redirection, so I inserted some appropriate pauses and debugs
and watched my processes from another window. And, as I expected, another
process was present when the script was inside the loop.
So, since another process is being forked to run the while loop, how can I
get the value of any variables assigned within the loop. And, why does it
do this?
Thanks in advance.
--
--------------------------------------------------------------------------
Dave Humes | Johns Hopkins University Applied Physics Laboratory
---------------------------------------------------------------------------
2. fuser on Solaris 2.3
3. help with parsing files needed
4. Appsfilter port
5. Need Help :: GCC parse error in include header files.
6. want to know x86 price
7. HELP - parsing text file as input to script - HELP
8. UNSUSCRIBE
9. Need to parse a file
10. YACC/LEX parsing help needed ../
11. HELP! Need to parse UNIX binary on Mac
12. Need Apache help, "server-parsed" with "send-as-is".
13. Need help with string parsing from piped stdout