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 C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
  • Date: Tue, 04 Nov 2008 03:09:22 -0600
  • Message-id: <49101142.9070507@xxxxxxxxxxxxxxxxxx>
Brian K. White wrote:
----- Original Message -----
From: "David C. Rankin" <drankinatty@xxxxxxxxxxxxxxxxxx>
To: "suse" <opensuse@xxxxxxxxxxxx>
Sent: Friday, October 31, 2008 10:51 PM
Subject: [opensuse] BASH - while read; howto read last line in file without
blank line at end?


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?
Simple example I'm stuck on:

{ while read alias url; do
echo -e "${alias}\t${url}"
done } < ~/linux/scripts/config/repos

The repos flat text file (shown with --- above and below to show no blank
line
at the end) is:

When read reaches end-of-file instead of end-of-line, it does read in the
data and assign it to the variables, but it exits with a non-zero status.
If your loop is constructed "while read ;do stuff ;done <data"
then the while command sees the non-zero exit and does not do stuff, exactly
as requested.

So instead of testing the read exit status directly, test a flag, and have
the read command set that flag from within the loop body. That way regardless
of reads exit status, the entire loop body runs, because read was just one of
the list of commands in the loop like any other, not a deciding factor of if
the loop will get run at all.

DONE=false
until $DONE ;do
read || DONE=true
# process $REPLY here
done < /path/to/file.in


Thanks Brian - I see light at the end of this tunnel.....

--
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@xxxxxxxxxxxx
For additional commands, e-mail: opensuse+help@xxxxxxxxxxxx

< Previous Next >