1748216 Members
3422 Online
108759 Solutions
New Discussion юеВ

Re: shell script help

 
SOLVED
Go to solution
Zee
Advisor

shell script help

cat $scriptpath/synclogs.log | while read LINE
do
case $LINE in
.............

...........
......

Quesiton:: is there anyway of finding out the line i m reading in $LINE variable is the last line in the file ?

Any help?
9 REPLIES 9
Fred Ruffet
Honored Contributor
Solution

Re: shell script help

With something like that :

lineCount=$(wc -l $scriptpath/synclogs.log)
lineNumber=0
cat $scriptpath/synclogs.log | while read LINE
do
lineNumber=$(($lineNumber+1))
if [ $lineNumber -eq $lineCount] ; then
echo Last one
fi
case $LINE in
.............

...........
......
done

Regards,

Fred
--

"Reality is just a point of view." (P. K. D.)
Rodney Hills
Honored Contributor

Re: shell script help

You could set a variable to the line count at the begining of the script.

nrec=`wc -l synclogs.log`

Then count up each record.

HTH

-- Rod Hills
There be dragons...
G. Vrijhoeven
Honored Contributor

Re: shell script help

Hi Zee,

I would do something like a
LIN=`cat file | wc -l `
NUM=0
cat file | while read LINE
do
let NUM="$NUM ++1"
case $LINE in
...
if [ $NUM -eq $LIN ]
then
exit 0
fi
done
Patrick Wallek
Honored Contributor

Re: shell script help

You could try something like:

Before the cat statement do:

LASTLINE=$(tail -1 $scriptpath/synclogs.log)

Then in your 'while do' loop:

if [[ "${LINE}" = "${LASTLINE}" ]] ; then
...do what you wanna do...
else
...do something else...
fi

The above works on the assumption that each line is somehow unique, which could be a problem if it's a log file.
Zee
Advisor

Re: shell script help

Thanks, is there any thing called EOF? can EOF be used for checking last line in the file ?
Rodney Hills
Honored Contributor

Re: shell script help

Since you are reading through "pipes", their is no way for the system to look ahead and see if the next line is EOF.

-- Rod Hills
There be dragons...
Zee
Advisor

Re: shell script help

thanks Rodney,

can u suggest any other way of reading line other than "pipe" and then used EOF marker to check last line ?

any suggestion ?
John Poff
Honored Contributor

Re: shell script help

Hi,

I'm curious. What problem are you trying to solve? Using the while-read-do-done structure will read each line of the file, and the statements in your script immediately after the done line will be executed after the last line of the file is read, so you should be able to handle whatever you need to do after the end of file at that point.

JP
Zee
Advisor

Re: shell script help

thanx