在Shell脚本中捕获失败 [英] Catch failure in shell script

查看:85
本文介绍了在Shell脚本中捕获失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

shell脚本非常新.我正在尝试执行以下操作:

Pretty new to shell scripting. I am trying to do the following:

#!/bin/bash

unzip myfile.zip

#do stuff if unzip successful

我知道我可以将命令与&& 链接在一起,但是有很大一部分,这是很难维护的.

I know that I can just chain the commands together in with && but there is quite a chunk, it would not be terribly maintainable.

推荐答案

您可以在测试中显式使用命令的退出状态:

You can use the exit status of the command explicitly in the test:

if ! unzip myfile.zip &> /dev/null; then
    # handle error
fi

这篇关于在Shell脚本中捕获失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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