how to determine last line in a while do loop

Hey guy, I have a very simple question, I have the following script,

while read $test
do
echo "this is $test"
done < /testfile

What I want is instead of printing "this is $test" for each line, I want the script to print out "this is last line" for last line, how should I do it?

You won't know it's the last line until you fall out of the loop

do something like


LINE=

while read N
do
      if test "$LINE" != ""; then echo this is $LINE; fi
      LINE="$N"
done <file

echo this is last line $LINE

Fedora,
If you just want to display the last line, here is one way:

tail -1 input_file

thanks guys, i think I did not explain clearly of my question,

I have a bunch of files to process, which contain many usernames, I want every last name in every file to be treated special, something like this (I know this is not a real shell scipt):

while read name

do

if "the position of name"!="last name in the file"; then

function1

else

function2

fi

done < /tmp/filename/*

Here is one way:

typeset -i mTotLines
typeset -i mCntLines=0
mTotLines=`cat input_file | wc -l`
while read mLine
do
  mCntLines=${mCntLines}+1
  if [ ${mCntLines} -eq ${mTotLines} ]; then
    echo "Last line"
  else
    echo "Not the last line"
  fi
done < input_file
prev=
while IFS= read -r line
do
   [ -n "$prev" ] && function1 "$prev"
   prev=$line
done < FILE
function2 "$prev"



ah, never thought this, thank you Shell_Life!

Cfajohnson,
Your solution will break for an empty file.
To fix it:

...
done < FILE
[ -n "$prev" ] && function2 "$prev"

very cool! thanks a lot, guys!

There is also a problem with empty lines.

prev=
proceed=
while IFS= read -r line
do
   [ -n "$proceed" ] && function1 "$prev"
   prev=$line
   procedd=yes
done < FILE
[ -n "$proceed" ] && function2 "$prev"

hi aigles

u have written the code and used the function in the code

can u explain the code what it does

thanks
regards