bash - 如何在条件命令中立即退出?
问题描述
首先,如果任何命令失败,我们可以使用set -e
立即退出 shell 脚本(以非零代码退出)。
但是,我怎样才能在条件语句中立即退出呢?例如:
set -xe
_if_error() {
'run a bad command' # should exit immediately for this command does not exist
echo -e "\033[31mUnexpected execution\033[0m"
}
if ! _if_error; then
echo 'ok'
else
echo 'bad'
fi
我希望脚本回显ok
,不要回显红色Unexpected execution
。
解决方案
推荐阅读
- sql - 如何使用 ssisdb 在运行过程中获取 ssis 包执行信息
- vue.js - 在 webpackEmptyContext 中找不到模块“../assets/logo.png”(在 ./src/component
- css - Bootstrap的scss文件在scss文件架构中的正确位置
- html - 如何设置页面上最后一个 Angular 组件的样式?
- wordpress - 查找 Wordpress 302 重定向
- python - 如何过滤掉具有特定数字的数字?
- ibeacon - Android 信标库 - 仅检测特定的 UUID
- uwp - 在 Xbox one 中运行的 C# UWP GamePad 类中需要帮助
- python - 是否有一种灵活的方法可以通过从 shapefile 读取或使用一组顶点创建的多边形来裁剪栅格?
- sas - SAS如何在特定字符串之前提取单个字符串?