是否可以清除 ${arg?word} 失败产生的消息
当 bash 解释脚本时遇到
${arg?error message}
arg 未设置时,脚本在打印错误消息后终止。不幸的是,bash 在错误消息前面添加了“$0: line $LINENO: arg:”。是否可以抑制附加信息?我希望错误消息完全是“错误消息”。
When bash is interpreting a script and encounters
${arg?error message}
when arg is unset, the script terminates after printing the error message. Unfortunately, bash prepends "$0: line $LINENO: arg:" to the error message. Is it possible to suppress the additional information? I would like the error message to be exactly "error message".
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可能必须避免
${PARAMETER?WORD}
构造并手动执行。例如,类似的东西You probably have to avoid the
${PARAMETER?WORD}
construct and do it manually. For example, something like我猜想,如果您想这样做,您可能无法以 Bash 中最自然的方式实现您的最终目的。像这样的东西应该更好:
不过,更直接地回答问题:
解释:
|&
连接括号中代码块的标准错误(这是工作所必需的,通过的方式)进入sed
(流编辑器),它用正则表达式去除您不需要的消息部分。I'm guessing that if you're wanting to do this, you may not be accomplishing your ultimate purpose in the way that is most natural in Bash. Something like this should be better:
Answering the question more directly, though:
Explanation: The
|&
connects the standard error of the block of code in the parentheses (which are necessary for this to work, by the way) intosed
(stream editor), which strips off the part of the message you don't want with a regular expression.