Issue
Consider next snippet.
generate_build_specs(){
echo ubuntu gcc9 debug
echo macos clang debug
echo macos gcc10 release
echo macos clang release
}
generate_build_specs | awk -v IGNORECASE=1 '/macos/ && /release/' |
if input_data_available ;then
echo "FIXME. Cannot build 'macos release' until it is fixed. Buildspec dropped: $(cat)"
fi
My idea is to echo message with contents of input stream only if data in stream available. Something like peek(char)
in stream, not taking it from there.
Of course, I know a workaround. i.e
var="$(cat)"; if test -n "$var"; echo "blah:: $var" ; fi
Solution
During the discussion, I realized that
there is no way to check if a stream is ended without reading at least one char from it.
input_data_available
could not be implemented in Bash. It is a conceptual question on streams.
So the current solution is to store input to variable, then work on it.
var="$(cat)"; if test -n "$var"; then echo "::ERROR:: $var" ; fi
A better (i.e. laconic) way to do the same is:
sed -zE 's,^.+$,::ERROR:: \0\n,'
It means prepend string and output only if input is not empty. Regexp ^.+$
checks if there is at least one symbol. But I think as all oneliners this one is harder to read.
Thank you everybody for participation.
Answered By - kyb