【发布时间】:2020-04-08 22:20:58
【问题描述】:
我有一个主脚本(deploy_test.sh),它使用 find 命令循环文件并执行其他几个 shell 脚本。即使其他 shell 遇到故障,主脚本也不会退出。我在脚本开始时使用了几个选项,但仍然无法退出,脚本一直持续到最后。
deploy_test.sh
#!/usr/bin/env bash
set -euo pipefail
shopt -s execfail
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "Do you want to Continue: [Yes/No]"
read action
if [ $action = "Yes" ]
then
echo "Executing scripts"
find ${SCRIPT_DIR}/folder2 -type f -name '*.sh' -exec bash {} \;
echo $?
echo "This should also not be printed"
else
echo "nothing"
exit 1
fi
我的文件夹 2 有 2 个 .sh 文件(1.sh 和 2.sh)
1.sh(脚本末尾有一些特殊字符)
#!/usr/bin/env bash -eu
echo "hi iam in 1.sh and i have error in this file"
`
2.sh
#!/usr/bin/env bash -eu
echo "hi iam in 2.sh and i have no error in this file"
执行脚本时的输出
(deploy) CSI-0048:test_test smullangi$ ./deploy_test.sh
Do you want to Continue: [Yes/No]
Yes
Executing scripts
hi iam in 1.sh and i have error in this file
/Users/smullangi/Desktop/test_test/folder2/1.sh: line 4: unexpected EOF while looking for matching ``'
/Users/smullangi/Desktop/test_test/folder2/1.sh: line 5: syntax error: unexpected end of file
hi iam in 2.sh and i have no error in this file
0
This should also not be printed
我希望这个脚本在遇到具有特殊字符的 1.sh 文件中的错误后退出。但是无论我尝试什么选项,脚本在遇到错误后都不会退出。
非常感谢任何帮助。我正在使用 bash 版本(3.2.57(1)-release)的macbook(macos catalina v10.15.3)上执行此操作
#UPDATE1:
我也觉得脚本根本没有执行。如果脚本中没有错误,那么它也会退出。简而言之,我觉得我在 folder1/folder2 中的脚本在根据 Phillippe 建议修改代码后没有被执行
#!/usr/bin/env bash
set -euo pipefail
shopt -s execfail
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo "Do you want to Continue: [Yes/No]"
read action
if [ $action = "Yes" ]
then
echo "Executing scripts"
find ${SCRIPT_DIR}/folder2 -type f -name '*.sh' -exec false bash {} +
#find ${SCRIPT_DIR}/folder2 -type f -name '*.sh' -exec bash {} \;
echo $?
echo "This should also not be printed"
else
echo "nothing"
exit 1
fi
输出
(deploy) CSI-0048:test_test smullangi$ ./deploy_test.sh
Do you want to Continue: [Yes/No]
Yes
Executing scripts
【问题讨论】:
标签: linux bash shell error-handling sh