如何缩短它?:先阅读|| 1号出口; (回声“$first”;猫)| $foo
目的是如果没有输入,甚至不启动 $foo。
如果 foo="diff - xyz" 并且输入丢失或为空,这可能很有用。 否则,diff 将输出所有“xyz”作为差异。
最初的问题是“cat doesnotexist | diff - Exists”输出“exists”的内容(前缀为“>”),而我希望整个管道失败。 我当前的解决方案是:
cat doesnotexist | (read first|| exit 1; (echo "$first"; cat) | diff - exists)
,但我想要一个更短的解决方案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我删除了管道,它在您的示例中没有附加值。严格来说,这不会让 diff 失败,只是没有执行。
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.