Docs/shell/shell中的set.md

24 lines
842 B
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

**1.shell遇到错误的时候不会停止接着执行下面的shell命令的时候有时候是一种非预期的行为可能会产生破坏性所以我们写的总是小心翼翼。**
**2.shell的调试比较困难尤其是当我们在linux服务器上使用vim来进行shell的编写的时候更会存在这种情况只能靠不断的加一些echo命令来尝试看看发生了什么**
set可以避免以上问题。
有以下几个常用命令
```shell
# +-表示模式的开关,其中-为开启,+为关闭
# 遇到错误后脚本会直接退出,不会继续往下执行
set -e
# 关闭以上模式
set +e
# set -e无法处理管道错误set -o主要处理管道异常问题
set -o pipefail
set +o pipefail
# 若变量不存在,脚本退出(默认忽略)
set -u
set +u
# 增加调试
set -x
set +x
```