bug-bash
[Top][All Lists]
Advanced

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

Re: Option for read to handle incomplete last line


From: Greg Wooledge
Subject: Re: Option for read to handle incomplete last line
Date: Sun, 24 Oct 2021 10:39:59 -0400

On Sun, Oct 24, 2021 at 04:26:44PM +0200, Martin Schulte wrote:
> Hi Greg, hi *!
> 
> > For bash scripts using this, I'd go a little bit fancier:
> > 
> > read_line() {
> >   if (($# == 0)) || [[ ${!#} = -* ]]; then
> >     declare -n _rl_lastvar=REPLY
> >   else
> >     declare -n _rl_lastvar=${!#}
> >   fi
> >   read -r "$@" || test -n "$_rl_lastvar"
> > }
> 
> Great, thanks - this solution seems to solve more real world problems than it 
> introduces ;-)
> 
> > This intentionally skips a trailing incomplete line that has too few
> > fields, as in:
> >
> > ...
> >
> > The incomplete line here only has one field, so the "lastvar" (b) is
> > empty, and therefore the incomplete line isn't counted.  I consider
> > this a feature, but others may not.
> 
> Yes, one might discuss this...
> 
> Nevertheless, am I right that this solution relies on an undocumented feature?

Which "undocumented" feature did you have in mind?  Most things are
documented, somewhere.

Are you thinking of -d '' signifying NUL as the delimiter?  Chet told
us that he supports this "happy accident" and won't take it away.

Are you thinking of the equivalence of "$array" and "${array[0]}"?  I
think that's documented somewhere, but I'm not going to dig for it right
now.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]