在管道中使用“ifne”——运行多个命令

Mar*_*ter 3 bash pipe shell-script

在我的脚本中,我使用的ifnemoreutils包中的实用程序。该行可以简化为以下内容:

printf "asdf\n" | ifne cat - && echo "stream not empty"
Run Code Online (Sandbox Code Playgroud)

ifne仅当流非空时才执行。但是如何使第二个命令 ( echo "stream not empty") 也仅在流非空时执行?例如,如何更改以下命令以使其不打印“stream not empty”?

printf "" | ifne cat - && echo "stream not empty"
Run Code Online (Sandbox Code Playgroud)

周围catecho用括号生成语法错误:

printf "" | ifne (cat - && echo "stream not empty")
Run Code Online (Sandbox Code Playgroud)

仅当流非空时如何执行最后一个命令?

Bab*_*aba 9

使用这种格式:

printf ""     | ifne sh -c  "cat - ; echo 'stream not empty' "
Run Code Online (Sandbox Code Playgroud)

输出为无,并且:

printf "bb\n" | ifne sh -c  "cat - ; echo 'stream not empty' "
Run Code Online (Sandbox Code Playgroud)

输出是:

bb
stream not empty
Run Code Online (Sandbox Code Playgroud)