[Top][All Lists]

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

Re: Option for read to handle incomplete last line

From: Koichi Murase
Subject: Re: Option for read to handle incomplete last line
Date: Sun, 24 Oct 2021 18:41:59 +0900

> 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'; done

> [...] (probably relying on undocumented features)

All the POSIX shells (bash, zsh, ksh, dash, etc.) behave in this way
and I think this is the behavior implied by the POSIX standard:

> XBD 3.206 
> (https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap03.html#tag_03_206)
> 3.206 Line
>   A sequence of zero or more non- <newline> characters plus a terminating 
> <newline> character.
> XCU 4 read 
> (https://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html)
>   read - read from standard input into shell variables
> [...]
>   The read utility shall read a single logical line from standard input into 
> one or more shell variables.
> [...]
>   The following exit values shall be returned:
>   0
>     Successful completion.
>   >0
>     End-of-file was detected or an error occurred.

reply via email to

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