set -o pipefail

对于set命令-o参数的pipefail选项,linux是这样解释的:

“If set, the return value of a pipeline is the value of the last (rightmost) command to exit with a non-zero status,or zero if all commands in the pipeline exit successfully. This option is disabled by default.”

设置了这个选项以后,包含管道命令的语句的返回值,会变成最后一个返回非零的管道命令的返回值。听起来比较绕,其实也很简单:

 

 

set命令的-e参数,linux自带的说明如下:

“Exit immediately if a simple command exits with a non-zero status.”

也就是说,在”set -e”之后出现的代码,一旦出现了返回值非零,整个脚本就会立即退出。有的人喜欢使用这个参数,是出于保证代码安全性的考虑。但有的时候,这种美好的初衷,也会导致严重的问题。

 

 

 

set -x 与 set +x命令的作用实际是用于输出详细日志,是Shell脚本中使用echo命令输出的替代方案。更适用于输出大量日志的场景使用
set -x 是开启,set +x是关闭,set -o是查看 (xtrace,追踪一段代码的显示情况)

posted @ 2023-01-30 14:51  hbg-rohens  阅读(774)  评论(0编辑  收藏  举报