#!/bin/bash -ex
shell 命令 set -ex,
稍有常识的人都能看出,这是 set
命令加上了 -e
和 -x
两个参数 (废话么这不是)。那么,我就把这两个参数拆开,分别说一下它在脚本中的用处。
set -e
这个参数的含义是,当命令发生错误的时候,停止脚本的执行
#!/bin/bash
echo 1 && rm non-existent-file && echo 2
等同
#!/bin/bash
set -e
echo 1
rm non-existent-file # which will fail
echo 2
set -x
-x
参数的作用,是把将要运行的命令用一个 +
标记之后显示出来
#!/bin/bash
set -ex
echo 1
rm non-existent-file # which will fail
echo 2
输出
+ echo 1
1
+ rm non-existent-file
rm: non-existent-file: No such file or directory