shell 脚本中 set -e选项的作用

 

set -e选项保证程序的每一步都执行正确,如果前面一部程序执行错误,则直接退出程序

001、 不加 set -e的情况

(base) [root@PC1 test2]# ls
test.sh
(base) [root@PC1 test2]# cat test.sh
#!/bin/bash

xxxx

echo step2
(base) [root@PC1 test2]# bash test.sh
test.sh: line 3: xxxx: command not found
step2

 

 

002、添加set -e

(base) [root@PC1 test2]# ls
test.sh
(base) [root@PC1 test2]# cat test.sh
#!/bin/bash

set -e

xxxx

echo step2
(base) [root@PC1 test2]# bash test.sh
test.sh: line 5: xxxx: command not found

 

posted @ 2022-10-06 12:10  小鲨鱼2018  阅读(1089)  评论(0编辑  收藏  举报