如何保持记录并在线检查?(How to keep make log and check it online?)

我想重定向make的日志,看看make上做了什么。 这是脚本

make |& tee make.log # bash syntax # make 2>&1 | tee make.log # or, sh syntax [ $? -ne 0 ] && echo "Error: stopped" && exit 1 echo "Done"

我发现当make失败时它不会执行Error退出。 我想这是由管道引起的,但是如何优化构建脚本呢?

I want to redirect make's log and see what's doing on make. Here is the script

make |& tee make.log # bash syntax # make 2>&1 | tee make.log # or, sh syntax [ $? -ne 0 ] && echo "Error: stopped" && exit 1 echo "Done"

I found it won't execute the Error exit when make failed. I guess it is caused by the pipe, but how to refine the build script?

最满意答案

由于你已经限制自己使用bash,而不是便携式POSIX sh,你可以使用bash的pipefail shell选项; 运行set -o pipefail 。 bash的手册页说:

如果启用了pipefail,则管道的返回状态是以非零状态退出的最后一个(最右边)命令的值,如果所有命令都成功退出,则返回零。

例如。

#!/bin/bash set -o pipefail if make |& tee make.log ; then echo "Done" else echo "Error: stopped" exit 1 fi

Since you're already restricting yourself to using bash, not portable POSIX sh, you can just use bash's pipefail shell option; run set -o pipefail. The man page for bash says:

If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.

eg.

#!/bin/bash set -o pipefail if make |& tee make.log ; then echo "Done" else echo "Error: stopped" exit 1 fi如何保持记录并在线检查?(How to keep make log and check it online?)

我想重定向make的日志,看看make上做了什么。 这是脚本

make |& tee make.log # bash syntax # make 2>&1 | tee make.log # or, sh syntax [ $? -ne 0 ] && echo "Error: stopped" && exit 1 echo "Done"

我发现当make失败时它不会执行Error退出。 我想这是由管道引起的,但是如何优化构建脚本呢?

I want to redirect make's log and see what's doing on make. Here is the script

make |& tee make.log # bash syntax # make 2>&1 | tee make.log # or, sh syntax [ $? -ne 0 ] && echo "Error: stopped" && exit 1 echo "Done"

I found it won't execute the Error exit when make failed. I guess it is caused by the pipe, but how to refine the build script?

最满意答案

由于你已经限制自己使用bash,而不是便携式POSIX sh,你可以使用bash的pipefail shell选项; 运行set -o pipefail 。 bash的手册页说:

如果启用了pipefail,则管道的返回状态是以非零状态退出的最后一个(最右边)命令的值,如果所有命令都成功退出,则返回零。

例如。

#!/bin/bash set -o pipefail if make |& tee make.log ; then echo "Done" else echo "Error: stopped" exit 1 fi

Since you're already restricting yourself to using bash, not portable POSIX sh, you can just use bash's pipefail shell option; run set -o pipefail. The man page for bash says:

If pipefail is enabled, the pipeline's return status is the value of the last (rightmost) command to exit with a non-zero status, or zero if all commands exit successfully.

eg.

#!/bin/bash set -o pipefail if make |& tee make.log ; then echo "Done" else echo "Error: stopped" exit 1 fi