[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: |
Koichi Murase |
Subject: |
Re: How to test if stdin is empty, if not empty print to stdout, otherwise do something else. |
Date: |
Wed, 25 Mar 2020 13:19:41 +0900 |
2020/3/25 9:22 Peng Yu
> `empty` means as if it is read from /dev/null.
> I end up with something like this. Is it robust? I am not sure if can
> cover all the corner cases.
If `empty' means /dev/null like behavior, your function fails with the
following case.
$ printf '\0' | f
No input from stdin
Also, `-r' option is missing.
$ echo '\a' | f
a
I don't know if this is the best solution, but this is an improved version:
f2() {
local REPLY
if read -r -d '' -n 1; then
if [[ $REPLY ]]; then
printf '%s' "$REPLY"
else
printf '\0'
fi
cat
else
echo 'No input from stdin'
fi
}
2020/3/25 9:43 João Eiras
> https://www.gnu.org/software/bash/manual/html_node/Bash-Builtins.html
>
> if read -t 0 -u 0; then # -u 0 is optional and just here as example
$ { sleep 1; echo A; } | { read -t 0 || echo No input; }
If `empty' means /dev/null, `read -t 0' doesn't work with the above
case. One needs to wait until EOF when there is no input.
- 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, 2020/03/24
- 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 <=
- 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