Make a pipe conditional on non-empty return
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Over a Mysterious Island Looping
--
Chapters
00:00 Make A Pipe Conditional On Non-Empty Return
00:13 Answer 1 Score 29
00:37 Answer 2 Score 16
00:52 Accepted Answer Score 12
02:38 Answer 4 Score 5
02:59 Answer 5 Score 3
03:24 Thank you
--
Full question
https://superuser.com/questions/210054/m...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#bash #pipe
#avk47
ANSWER 1
Score 16
ifne(1) from moreutils does exactly that. Moreutils is available as a package at least in Debian and Ubuntu, probably in other distros as well.
ACCEPTED ANSWER
Score 13
The following ifnotempty
function pipes its input to the command passed as an argument, except that it does nothing if the input is empty. Use it to pipe source --foo
into sink --bar
by writing source --foo | pipe_if_not_empty sink --bar
.
pipe_if_not_empty () {
head=$(dd bs=1 count=1 2>/dev/null; echo a)
head=${head%a}
if [ "x$head" != x"" ]; then
{ printf %s "$head"; cat; } | "$@"
fi
}
Design notes:
- I would expect this implementation to work on all POSIX/Unix platforms, though strictly speaking it is not standards-compliant: it relies on
dd
not reading more than the one byte it's told to read on its standard input. - I think
head -c 1
would be a suitable replacement fordd bs=1 count=1 2>/dev/null
on Linux. - On the other hand,
head -n 1
would not be suitable becausehead
typically buffers its input and may read more than the one line it outputs — and since it's reading from a pipe, the extra bytes are just lost. read -r head
and evenread -r -n 1 head
are not suitable here because if the first character is a newline,head
would be set to the empty string, making it impossible to distinguish between empty input and input starting with a blank line.- We can't just write
head=$(head -c 1)
because if the first character is a newline, command substitution would strip the final newline, making it impossible to distinguish between empty input and input starting with a blank line. - In bash, ksh or zsh, you can replace
cat
by</dev/stdin
for a microscopic performance gain.
If you don't mind storing the whole intermediate data in memory, here is a very slightly simpler implementation of pipe_if_not_empty
.
pipe_if_not_empty () {
input=$(cat; echo a);
if [ "x$input" != x"a" ]; then
{ printf %s "${input%a}"; } | "$@"
fi
}
Here is a slightly simpler implementation with the following caveats:
- The data produced by the source is considered empty if and only if it consists solely of newline characters. (This may in fact be desirable.)
- The data fed into the sink ends with exactly one newline character, no matter how many newlines the data produced by the source ends with. (This is could be a problem.)
Again, the whole data is stored in memory.
pipe_if_not_empty () {
input=$(cat);
if [ "x$input" != x"" ]; then
{ printf '%s\n' "${input}"; } | "$@"
fi
}
ANSWER 3
Score 5
The function below tries to read the 1st byte, and if successfull echos that byte and cats the rest. Should be efficient and 100% portable.
if_read() {
IFS="" read -rN 1 BYTE && { echo -nE "$BYTE"; cat; } | "$@";
}
Test cases:
$ echo -n | if_read wc -c
$ echo | if_read wc -c
1
$ echo -en "\nX" | if_read wc -c
2
$
ANSWER 4
Score 3
At least something like this works:
yourcommand | if [ $(wc -c) -gt "0" ]; then yourothercommand; fi
Please note that the above will consider line feeds and other special characters as output, so an empty line passed to that if statement will be considered as an output. Just raise the -gt limit if your output should usually be higher than 1 byte :)