如果特定命令失败,请不要中止脚本 [英] Do not abort script if a specific command fails

查看:59
本文介绍了如果特定命令失败,请不要中止脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下脚本运行脚本:

I am running my scripts with:

#!/bin/bash -eu

每当出现问题时,都会根据需要中止脚本.但是有时候我希望其中一个命令最终会失败,并且我想告诉 bash 忽略失败条件.在 make 中,您可以方便地忽略一个命令的状态:

Which aborts the script whenever a problem occurs, as wanted. But sometimes I expect one of the commands to eventually fail, and I would like to tell bash to ignore the fail condition. In make you can ignore the status of one command with the handy:

-command

中是否有类似内容?唯一想到的是丑陋的:

Is there something similar in bash? The only thing that comes to mind is the ugly:

set +e
command
set -e

推荐答案

您可以在命令失败时不执行操作或将明确的真实条件设置为

You could just do a no-op on the command failure or set an explicit true condition as

command || true

或不操作时

command || :

这样做会强制命令列表(甚至是管道)在失败时返回退出状态0.见

Doing so forces the command-list (even a pipeline) to return an exit status of 0 on failure. See

true | false
echo $?
1
true | false || true
echo $?
0
true | false || :
echo $?
0

这篇关于如果特定命令失败,请不要中止脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆