Bash 中是否有 TRY CATCH 命令 [英] Is there a TRY CATCH command in Bash

查看:24
本文介绍了Bash 中是否有 TRY CATCH 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写 shell 脚本,需要检查是否已安装终端应用程序.除非有更简洁的方法,否则我想使用 TRY/CATCH 命令来执行此操作.

I'm writing a shell script and need to check that a terminal app has been installed. I want to use a TRY/CATCH command to do this unless there is a neater way.

推荐答案

Bash 中是否有 TRY CATCH 命令?

Is there a TRY CATCH command in Bash?

没有.

Bash 并没有像许多编程语言中那样多.

Bash doesn't have as many luxuries as one can find in many programming languages.

bash 中没有try/catch;但是,可以使用 &&|| 实现类似的行为.

There is no try/catch in bash; however, one can achieve similar behavior using && or ||.

使用||:

如果 command1 失败,则 command2 运行如下

if command1 fails then command2 runs as follows

command1 || command2

同样,使用&&command2会在command1成功后运行

Similarly, using &&, command2 will run if command1 is successful

try/catch 最接近的近似如下

{ # try

    command1 &&
    #save your output

} || { # catch
    # save log for exception 
}

bash 也包含一些错误处理机制,以及

Also bash contains some error handling mechanisms, as well

set -e

如果任何简单命令失败,它会停止您的脚本.

it stops your script if any simple command fails.

还有为什么不if...else.它是你最好的朋友.

And also why not if...else. It is your best friend.

这篇关于Bash 中是否有 TRY CATCH 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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