Mailinglist Archive: opensuse (1318 mails)

< Previous Next >
Re: [opensuse] BASH - while read; howto read last line in file without blank line at end?
  • From: David Bolt <bcrafhfr@xxxxxxxxxx>
  • Date: Sat, 1 Nov 2008 14:23:16 +0000
  • Message-id: <PYPRdrgUZGDJFwGt@xxxxxxxxxxxxxxxxxxx>
On Sat, 1 Nov 2008, Randall R Schulz wrote:-


I can reproduce your result if the the last line (non-blank) has no
trailing newline.

That was how I read the source file to be formatted, no EOL (newline)
character on the last line.

Specifically, if the "lastline" line you show does
not have a newline, then the read loop ignores it.

The problem is caused by the return when read doesn't find the EOL. In
this case, read will read the line and puts the value(s) into the right
variable(s) but it then returns false as the response code. Since while
is looking for a non-zero response from read to decide whether to
execute the contents of the loop, or not, this causes it to skip the
loop and so you don't see the output from the last read.

I guess it's a BASH oddity. Vim will warn you when you open such a file.
I'm not sure if it will allow you to create one (I used a different
editor to create the test data I used).

I used printf and didn't add a final '\n' for my test data. That ensured
no final EOL was added.

Also, in this particular case, at least, none of the curly braces
(inside the quoted string or around the while loop) are necessary.

They may not be required, but always including them is still a useful
habit to have as it ensures you're going to be using them when you
actually need to use them.

David Bolt

Team Acorn: OGR-NG @ ~100Mnodes RC5-72 @ ~15Mkeys/s
SUSE 10.1 32 | | openSUSE 10.3 32b | openSUSE 11.0 32b
| openSUSE 10.2 64b | openSUSE 10.3 64b | openSUSE 11.0 64b
RISC OS 3.6 | TOS 4.02 | openSUSE 10.3 PPC | RISC OS 3.11
To unsubscribe, e-mail: opensuse+unsubscribe@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >
Follow Ups