[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to test if stdin is empty, if not empty print to stdout, otherwi
From: |
Eli Schwartz |
Subject: |
Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else. |
Date: |
Tue, 24 Mar 2020 20:04:00 -0400 |
User-agent: |
Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.5.0 |
On 3/24/20 7:43 PM, Peng Yu wrote:
> Hi,
>
> ifne from moreutils can work similar to this goal. But it is an
> external program.
>
> I'd like to test if stdin is not empty, then print to stdout of the input.
>
> But the following code is not robust for this goal. Is there a robust
> way to do so in bash? Thanks.
>
> $ function f {
> if read -n1; then
> echo "$REPLY"
> cat
> else
> echo 'No input from stdin'
> fi
> }
>
> $ { echo; seq 3; } | f
>
> 1
> 2
> 3
> $ seq 10 13 | f
> 1
> 0
> 11
> 12
> 13
> $ f < /dev/null
> No input from stdin
The problem you are having here is, how do you define "empty"?
$ printf '%s\n' '' | { read; echo ret=$?; declare -p REPLY; }
ret=0
declare -- REPLY=""
$ printf '%s' '' | { read; echo ret=$?; declare -p REPLY; }
ret=1
declare -- REPLY=""
$ help read
[...]
Exit Status:
The return code is zero, unless end-of-file is encountered, read
times out
(in which case it's greater than 128), a variable assignment error
occurs,
or an invalid file descriptor is supplied as the argument to -u.
$
And the thing is, that you're currently using the return value of read
to determine that you *do* have input, that input is of length zero, and
that input is terminated by a newline of length 1 (before reaching an EOF).
If you wanted to check whether you have "interesting" input, then don't
check the return value of `read`, instead, analyze $REPLY to see whether
you consider it interesting, for example, if [ -n "$REPLY" ] to see if
it is zero-length. If you don't consider whitespace to be interesting,
then you can do similar checks if $REPLY contains characters other than
[:space:]
--
Eli Schwartz
Arch Linux Bug Wrangler and Trusted User
signature.asc
Description: OpenPGP digital signature
- How to test if stdin is empty, if not empty print to stdout, otherwise do something else., Peng Yu, 2020/03/24
- Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else.,
Eli Schwartz <=
- Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else., Peng Yu, 2020/03/24
- Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else., João Eiras, 2020/03/24
- Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else., Koichi Murase, 2020/03/25
- Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else., Greg Wooledge, 2020/03/25
- Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else., Peng Yu, 2020/03/25
- Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else., Eli Schwartz, 2020/03/25
- Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else., Peng Yu, 2020/03/25