tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: sh read on files missing the final newline



On Tue, Jul 07, 2026 at 15:38:47 +0200, Edgar Fuß wrote:

> So, are you saying that a POSIX-compliant shell's read command is
> required to read (and split into the arg vars) a trailing
> "incomplete line"?

Yes, and it also returns 1 to indicated the EOF was encountered before
the delimiter was found.


> If yes, if I substitute the usual
> 	while read foo bar baz; do
> 		...
> 	done
> with
> 	while read foo bar baz || [ -n "$foo" ]; do
> 		...
> 	done
> would that correctly deal with "incomplete lines" for a POSIX
> compliant shell?

I ran into this gotcha (`while read` vs incomplete last line) a while
back.  The quick workaround I used was to throw in awk to absorb the
shock:

  awk -e '{print $1, $3}'            \
      "$component"/META-INF/symlinks \
  | while read dst src; do
      # ...
  done


-uwe


Home | Main Index | Thread Index | Old Index