How to shorten this?: read first|| exit 1; (echo "$first"; cat) | $foo
The aim is to not even start $foo if there is no input.
This can be useful if foo=开发者_如何学运维"diff - xyz", and the input is missing or empty. Otherwise, diff would output all of "xyz" as the difference.
The original problem is that "cat doesnotexist | diff - exists" outputs the content of "exists" (prefixed with ">"), while I want the entire pipe to fail. My current solution is:
cat doesnotexist | (read first|| exit 1; (echo "$first"; cat) | diff - exists)
, but I would like a shorter one.
[ -f doesnotexist ] && diff doesnotexist exists
I removed the pipe, it has no added value in your example. Strictly, this doesn't let the diff fail, it's just not executed.
精彩评论