【问题标题】:Unable to exit shell script when using exec command使用 exec 命令时无法退出 shell 脚本
【发布时间】: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


    【解决方案1】:

    find 在运行的命令出错时并不总是以错误代码退出:

    find ${SCRIPT_DIR}/folder1 -type f -exec false {} \;
    

    find 以上命令本身运行成功,即使它运行的每个命令都会出错。

    按照find 给出错误:

    find ${SCRIPT_DIR}/folder1 -type f -exec false {} +
    

    要对每个脚本进行错误处理,您可以这样做

    cd ${SCRIPT_DIR}/folder1
    for script in ./*.sh; do
        $script
    done
    

    【讨论】:

    • 嗨菲利普。感谢你的回答。它正在部分工作。该脚本现在正在退出,但它不会引发任何错误。如何在命令行显示错误然后退出?
    • 如果它现在正在退出,您可能需要粘贴新版本的脚本和输出。
    • 我已根据您的建议将代码块和输出放在问题本身中
    • -exec false bash {} + DOES exit with error, 正如我在回答中所说的那样。
    • 它没有给我带来错误。我已经运行了这个脚本,里面有 50 个文件。他们都没有错误,但它没有给我任何输出。它正在退出一秒钟
    【解决方案2】:

    我没有找到任何使用 exec 的优雅解决方案。所以我在 find 命令中使用了 xargs ,它工作得很好。 Shell 退出并显示相应的错误消息。我以此为参考https://unix.stackexchange.com/questions/571215/force-xargs-to-stop-on-first-command-error

    #!/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' | xargs -I {} sh -c 'bash "$1" || exit 255' sh {}
     echo $?
     echo "This should also not be printed"
    
    
    else
    echo "nothing"
    exit 1
    fi
    

    【讨论】:

      猜你喜欢
      • 2018-05-27
      • 2013-03-01
      • 1970-01-01
      • 2017-03-04
      • 2012-01-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-04
      相关资源
      最近更新 更多