[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Option for read to handle incomplete last line
From: |
Martin Schulte |
Subject: |
Re: Option for read to handle incomplete last line |
Date: |
Sun, 24 Oct 2021 16:26:44 +0200 |
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?
Best regards
Martin