David Bolt wrote:
On Sat, 1 Nov 2008, Sandy Drobic wrote:-
David C. Rankin wrote:
Listmates,
I'm stumped on another simple BASH problem. How do I read the last line of a file with a while loop without requiring a blank line at the end of the file?
You can't, not if the loop-test is using the true/false return from read to work out whether to execute the loop body or not. Without an EOL, read will actually read the line and store it in the variables, but returns a false if there's no EOL which the 'while' then sees as a failure and so causes it to skip the body of the loop.
Okay, it's still missing the last line. Last try, and I've split this one up so it's spread over a few lines to make it easier to read, with one that will work and should even handle 0-length files:
do read LINE TEXT EXIT="$?" [ -n "${LINE}${TEXT}" ] && echo "${LINE} -> ${TEXT}" [ "${EXIT}" -ne 0 ] && break done
davjam@playing:~> while : line_one -> has_EOL line_two -> has_EOL line_three -> has_EOL line_four -> no_EOL davjam@playing:~>
With this one, the echo command is executed only if ${LINE} and/or ${TEXT} have any contents. I assigned the return value from the read command and use that after the echo command to decide whether to break out of the loop. This avoids printing out:
->
if there's a blank line in the input file.
Of course, you could add another line:
[ -z "${LINE}${TEXT}" ] && echo
to show there was a blank line present.
Regards, David Bolt
That was the trick I was looking for! Are you sure someone didn't install a BASH processor inside you head when you were asleep! Thanks David. -- David C. Rankin, J.D., P.E. Rankin Law Firm, PLLC 510 Ochiltree Street Nacogdoches, Texas 75961 Telephone: (936) 715-9333 Facsimile: (936) 715-9339 www.rankinlawfirm.com -- To unsubscribe, e-mail: opensuse+unsubscribe@opensuse.org For additional commands, e-mail: opensuse+help@opensuse.org