在bash中隐藏错误消息

编程入门 行业动态 更新时间:2024-10-28 00:29:41
本文介绍了在bash中隐藏错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在以下情况下,我无法从shell命令隐藏错误消息.

I have problem to hide error message from shell command as the following case.

firs_line=$(head -n 1 file) > /dev/null 2>&1

我希望错误消息将被隐藏,但实际上不会. head命令成功执行后如何获取输出,但失败时隐藏错误消息?

I expect that the error message will be hidden but actually it doesn't. How to get output while head command is executed successfully but hide error message when it fails?

谢谢.

推荐答案

错误消息是否来自head程序(例如,找不到文件)?

Is the error message coming from the head program (like, file not found)?

在这种情况下,您必须从内部解析器重定向输出:

In this case you have to redirect the output from inside parens:

firs_line=$(head -n 1 file 2>/dev/null)

此外,您只需要重定向标准错误(而不是重定向应该由$()捕获并存储在firs_line

Moreover, you only have to redirect standard error (and not standard output which is supposed to be catched by $() to be stored in firs_line

更多推荐

在bash中隐藏错误消息

本文发布于:2023-10-09 10:22:25,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1475475.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   消息   bash

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!