繁体   English   中英

如何从提示中捕获错误消息-Shell或Perl

[英]How to capture error message from prompt - shell or perl

我正在尝试捕获命令的输出。 如果执行该命令,它将正常工作。 但是,当出现错误时,我无法捕获命令行中显示的内容

例如。

$ out=`/opt/torque/bin/qsub submitscript`
qsub: Unauthorized Request  MSG=group ACL is not satisfied: user abc@xyz.org, queue home
$ echo $out

$

我希望$ out收到消息

谢谢!

错误在stderr上,因此您需要将它们重定向到stdout,以便反引号可以捕获它:

out=`/opt/torque/bin/qsub submitscript 2>&1`
if [ $? -gt 0 ] ; then
    # By convention, this is sent to stderr, but if you need it on
    # stdout, just remove the >&2 redirection
    echo "Error: $out" >&2
else
    echo "Success: $out"
fi

您应该测试命令的退出状态以弄清楚输出代表什么(显示一种方式)。 它与perl类似,当然语法略有不同。

您是否尝试过这样做

$ out=`/opt/torque/bin/qsub submitscript 2>&1 > /dev/null`
$ echo $out

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM