gpt4 book ai didi

unix - 管道命令的输出,但前提是输出非空

转载 作者:行者123 更新时间:2023-12-05 03:14:05 24 4
gpt4 key购买 nike

在 Unix 下有没有一种方法可以运行命令并将其输出通过管道传递给另一个命令,但前提是输出非空?有点像

command --some-arg | something_magical | mail -s "Don't send this e-mail if it would be empty" foo@bar.com

我可以为 something_magical 添加一些简单的东西吗?

最佳答案

我想你可以在这个 link 中找到答案.

反正你可以把这个函数放在神奇的部分:

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
}

关于unix - 管道命令的输出,但前提是输出非空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27074298/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com