help-bash
[Top][All Lists]
Advanced

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

How to test if stdin is empty, if not empty print to stdout, otherwise d


From: Peng Yu
Subject: How to test if stdin is empty, if not empty print to stdout, otherwise do something else.
Date: Tue, 24 Mar 2020 18:43:49 -0500

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


-- 
Regards,
Peng



reply via email to

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