错误退出+回声不能同时使用 [英] exit on error + echo doesnt work together

查看:81
本文介绍了错误退出+回声不能同时使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试向脚本中的错误添加有意义的错误消息,然后在第一个出口退出.

I'm trying to add a meaningful error message to errors in my script, and exit at the 1st exit.

我尝试了<*errornous_command_returning_non_zero_value*> || (err=$? && echo $LINENO && exit $err)

由于某种原因,我不明白,该行被回显,但是退出未执行,脚本继续执行

For some reason that I don't understand, the line is echoed, but the exit is not executed and the script continues

推荐答案

您只能退出子外壳( ),而不是实际的外壳.

You only exit the subshell ( ) but not the actual shell.

您可以使用{ }代替子外壳( )来对命令进行分组.示例:

You can use { } instead of a subshell ( ) to group your commands. Example:

false || { err=$?; echo msg; exit $err; }

这篇关于错误退出+回声不能同时使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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