Re: Option for read to handle incomplete last line

2021-10-24 Thread Chet Ramey
On 10/24/21 12:22 PM, Martin Schulte wrote: Before reading the source I would never have thought that read sets variables although it returns FAILURE. Think of them as orthogonal conditions. `read' reads until a newline, or EOF or other error condition (ignoring timeouts or reading N characte

Re: Option for read to handle incomplete last line

2021-10-24 Thread Martin Schulte
Hi Greg! > > 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 "

Re: Option for read to handle incomplete last line

2021-10-24 Thread Greg Wooledge
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=${!#

Re: Option for read to handle incomplete last line

2021-10-24 Thread Martin Schulte
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 solu

Re: Option for read to handle incomplete last line

2021-10-24 Thread Greg Wooledge
On Sun, Oct 24, 2021 at 06:41:59PM +0900, Koichi Murase wrote: > > my apologies if there's a much easier solution for the following > > problem - in this case please let me know! > > We can always define a shell function (which would work in all the > POSIX shells): > > read_line() { read line ||

Re: Option for read to handle incomplete last line

2021-10-24 Thread Oğuz
On Sun, Oct 24, 2021 at 12:42 PM Koichi Murase wrote: > and I think this is the behavior implied by the POSIX standard: The spec also says: > STDIN > The standard input shall be a text file. A text file is either empty, or ends with a newline. The behavior of `read' is undefined when the input i

Re: Option for read to handle incomplete last line

2021-10-24 Thread Koichi Murase
> my apologies if there's a much easier solution for the following > problem - in this case please let me know! We can always define a shell function (which would work in all the POSIX shells): read_line() { read line || test -n "$line"; } printf '%s' "$input" | while read_line; do printf ' %s\n

Option for read to handle incomplete last line

2021-10-24 Thread Martin Schulte
Hello, my apologies if there's a much easier solution for the following problem - in this case please let me know! >From time to time a run into troubles when reading a file with a while-read >loop where the last "line" is not terminated with a newline. I found an ugly looking solution (probab