已合并
fix: add shebang and set -e to runtime set_env.sh #3870
sinat_31531339创建于 19 天前
fix: add shebang and set -e to runtime set_env.sh #3870
已合并
共 1 个文件变更+14-0
| @@ -1,3 +1,4 @@ | |||
| 1 | #!/bin/bash | ||
| 1 | # ----------------------------------------------------------------------------------------------------------- | 2 | # ----------------------------------------------------------------------------------------------------------- |
| 2 | # Copyright (c) 2025 Huawei Technologies Co., Ltd. | 3 | # Copyright (c) 2025 Huawei Technologies Co., Ltd. |
| 3 | # This program is free software, you can redistribute it and/or modify it under the terms and conditions of | 4 | # This program is free software, you can redistribute it and/or modify it under the terms and conditions of |
| @@ -8,6 +9,13 @@ | |||
| 8 | # See LICENSE in the root of the software repository for the full text of the License. | 9 | # See LICENSE in the root of the software repository for the full text of the License. |
| 9 | # ----------------------------------------------------------------------------------------------------------- | 10 | # ----------------------------------------------------------------------------------------------------------- |
| 10 | 11 | ||
| 12 | # 本脚本通常以 source 方式加载,记录调用方原始 errexit 状态,避免 set -e 泄漏到调用方 shell。 | ||
| 13 | case "$-" in | ||
| 14 | *e*) __setenv_errexit_was_set="y" ;; | ||
| 15 | *) __setenv_errexit_was_set="n" ;; | ||
| 16 | esac | ||
| 17 | set -e | ||
| 18 | |||
| 11 | append_env() { | 19 | append_env() { |
| 12 | local name="$1" | 20 | local name="$1" |
| 13 | local value="$2" | 21 | local value="$2" |
| @@ -134,3 +142,9 @@ setenv_main() { | |||
| 134 | } | 142 | } |
| 135 | 143 | ||
| 136 | setenv_main "$@" | 144 | setenv_main "$@" |
| 145 | |||
| 146 | # 恢复调用方原始 errexit 状态,避免污染 source 本脚本的 shell。 | ||
| 147 | if [ "$__setenv_errexit_was_set" = "n" ]; then | ||
| 148 | set +e | ||
| 149 | fi | ||
| 150 | unset __setenv_errexit_was_set | ||
🟡 Medium Priority
变更在第 12 行新增了
set -e。该脚本的主要使用方式是通过source引入(README 及所有 example 均使用source <path>/set_env.sh),而非直接执行。问题链: 新增
set -e→ 脚本被 source 到当前 shell →set -e在当前 shell 中生效 → 脚本结束后set -e未恢复 → 当前 shell 后续任何命令返回非零都会导致 shell 退出。触发条件:用户在交互式终端执行
source /usr/local/Ascend/cann/set_env.sh后,输入任何会失败的命令(如ls /nonexistent、grep pattern nofile),shell 会立即退出,可能丢失未保存的工作状态。说明:脚本内的业务逻辑本身对
set -e是安全的(函数内的if条件分支、|| true保护、$()命令替换等均在set -e豁免范围内)。问题仅在于未在脚本结束前恢复-e选项的原始状态。