在以下情况下,我无法从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中隐藏错误消息
发布评论