如何缩短它?:先阅读|| 1号出口; (回声“$first”;猫)| $foo

发布于 2024-10-01 14:39:47 字数 324 浏览 7 评论 0原文

目的是如果没有输入,甚至不启动 $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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无妨# 2024-10-08 14:39:47
[ -f doesnotexist ] && diff doesnotexist exists

我删除了管道,它在您的示例中没有附加值。严格来说,这不会让 diff 失败,只是没有执行。

[ -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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文