linux - 在 bash 中,如何仅在出现错误时显示命令的输出?

标签 linux bash terminal

我有一个构建脚本,有时我会在其中调用 make -j4。不过,我不想每次都看到它的全部输出;除非出现错误。那么,如果返回非零结果,我该如何缓冲 make 的输出并打印它呢?

最佳答案

将输出保存到一个临时文件

tmp=$(mktemp)
make -j4 &> "$tmp"

只有在出错时才显示

if (( $? )) ; then
    cat "$tmp"
fi
rm "$tmp"

关于linux - 在 bash 中,如何仅在出现错误时显示命令的输出?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39184743/

相关文章:

linux - Bash Grep 和发送

linux - 拔下 USB 内存棒后重启的 bash 脚本

linux - 从已编译的 bash 脚本中检索纯文本脚本

macos - 使用终端在文本文件中查找和替换。如何转义所有字符?

linux - 将数据包注入(inject) linux 内核以模拟以太网数据包到达的最佳方法

c++ - Linux 配置文件库

linux - 在下一行提示后面打印 bash 脚本结果

bash - 为什么 bash 脚本变量不保存?

linux - Linux 中的 Spring Data GemFire 服务器 java.net.BindException

基于 Linux 内核头文件中功能的条件编译