SHELL 下的set命令

最近需要看一些简单的脚本,里面总会遇到一些set -x,set +x之类的命令,这几个命令在之前学习shell的时候已经遇到过了,可以用来调试脚本。可是像set -o piplefail这样的命令以前就没有见过了。奇怪的在于百度,谷歌也都搜不出有用的信息来。于是硬着头皮man了一下,算是对set命令做一个总结吧。


set的可选参数很多,这里就先说说遇到的这几个情况吧。

1.set [--abefhkmnptuvxBCHP] [-o option] [arg ...]
              Without  options,  the  name  and  value of each shell variable are displayed in a format that can be reused as input.  The output is sorted
              according to the current locale.  When options are specified, they set or unset shell attributes.  Any arguments remaining after the options
              are  processed  are  treated as values for the positional parameters and are assigned, in order, to $1, $2, ...  $n.  Options, if specified,
              have the following meanings:

这是shell 里man set的原文。set的用法很明显:set [--abefhkmnptuvxBCHP] [-o option] [arg ...]

如果不带参数的话,光秃秃的set将会将所有shell的变量和其值打印到输出,以一种可以直接作为输入的形式。当指定了某个参数的话,set命令和这个参数将设置或取消shell的某些属性。


2.set -x

打开脚本的调试模式,脚本在运行过程中先输出原来的命令,再输出命令执行后的结果。


3.set +x

“-”与“+”的区别在于一个是开启一个是关闭。这个在很多其他的命令里也是这样的用法。


4.set -o pipefail

man里说:

 pipefail
                              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.

意思很明了,如果-o后面接的是pipefail参数的话,管线的返回值是最右边命令的返回值,命令成功则为0退出,否则为非0。如果管线所有的命令都全部成功执行的话,这个选项默认是不起作用的。



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值