Skip site navigation (1)Skip section navigation (2)
Date:      Mon, 22 Oct 2018 16:32:02 +0300
From:      Yuri Pankov <yuripv@yuripv.net>
To:        Eugene Grosbein <eugen@grosbein.net>, Freebsd hackers list <freebsd-hackers@freebsd.org>
Subject:   Re: shell read built-in
Message-ID:  <4cd38621-7387-6302-437e-b8fda3fbe1cd@yuripv.net>
In-Reply-To: <4ad1ea37-4714-a104-89a2-d7aef70d0f89@yuripv.net>
References:  <2fff355e-a729-d5d3-8199-f2bbba80c112@grosbein.net> <4ad1ea37-4714-a104-89a2-d7aef70d0f89@yuripv.net>

next in thread | previous in thread | raw e-mail | index | archive | help
Yuri Pankov wrote:
> Eugene Grosbein wrote:
>> Hi!
>>
>> $ echo test | read line; echo $?
>> 0
>> $ echo -n test | read line; echo $?
>> 1
>>
>> Is it right behavior for /bin/sh?
>> This makes it hard for "while read line ... < file"
>> to process last line if it is not followed by new line character.
> 
> POSIX defines the exit status for read as (and sh(1) says the same):
> 
>     The following exit values shall be returned:
>      0 Successful completion.
>     >0 End-of-file was detected or an error occurred.
> 
> ...and looks like all of /bin/sh, bash, ksh93 seem to agree on the
> behavior here.

BTW, it looks like last line is still parsed despite not having \n, so
you could workaround it using something like (yes, looks ugly):

$ printf "foo bar\n" | (while read a b; do printf "%s %s\n" $a $b; done;
if test -n "$a$b"; then printf "%s %s\n" $a $b; fi)
foo bar
$ printf "foo bar" | (while read a b; do printf "%s %s\n" $a $b; done;
if test -n "$a$b"; then printf "%s %s\n" $a $b; fi)
foo bar



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?4cd38621-7387-6302-437e-b8fda3fbe1cd>