help-bash
[Top][All Lists]
Advanced

[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: Peng Yu
Subject: Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else.
Date: Wed, 25 Mar 2020 09:08:22 -0500

> The problem with this thread, as with all of Peng Yu's threads, is that
> nobody knows what the actual GOAL is.  Peng Yu will never reveal the goal,
> either, no matter how many times you ask.

I have emailed you before. The goal does not matter.

> I can think of at least two possible goals which might drive someone to
> ask how to test whether stdin has input available.

Thanks for your suggestion. However, I only use stdin to take input
data whenever possible. And I don't want to add an additional switch
to tell the program whether the input contains data or not as it is
redundant to add such a switch when the information whether the input
is empty is contained in the input itself.

> (1) "I want to write a utility that can either take data as an argument,
>     or read data from stdin.  I want it to do the correct thing
>     automatically, without having to pass a --stdin option or whatever."
>
>     In this case, the test is simply reversed.  Don't try to test whether
>     stdin has input available.  Test whether an argument was passed.
>
>     thing() {
>       if [[ $1 ]]; then
>         stuff "$1"
>       else
>         local in
>       in=$(cat; printf x)
>       in=${in%x}
>       # Or, in=$(cat) if you want to strip newlines.  Or, read -r in if
>       # you only want one line of input, with newline stripped.
>       stuff "$in"
>       fi
>     }
>
> (2) "I'm writing an keyboard-event-driven user interface that should handle
>     keyboard presses whenever they occur, but should not block to wait for
>     them."

This is not relevant to my goal.

-- 
Regards,
Peng



reply via email to

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