如何使用Perl中的Backticks在两个不同的变量中捕获STDOUT和STDERR [英] How to capture both STDOUT and STDERR in two different variables using Backticks in Perl

查看:203
本文介绍了如何使用Perl中的Backticks在两个不同的变量中捕获STDOUT和STDERR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想从脚本中运行一个外部程序,同时我想捕获STDOUT和STDERR,但在两个不同的变量。我该怎么办?对于istance,如果我运行这个脚本...

  my $ cmd =`snmpwalk -v $ version -c $ community $ hostname $ oid`; 

...如果没有错误,一切都正常工作,但如果命令提出错误此错误将在命令行上打印,我不希望发生这种情况。我想捕获错误,以及。无需在屏幕上打印。任何想法?

解决方案

在Perl常见问题中,您有不同的选项,
$ b

http://perldoc.perl.org/perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command%3f


Let's say I want to run an external program from my script with backticks and at the same time I want to capture both STDOUT and STDERR but in two different variables. How can I do that? For istance if I run this script...

my $cmd = `snmpwalk -v $version -c $community $hostname $oid`;

...if there is no error everything works just fine BUT if the command raise an error this error will be printed on the command line and I don't want that to happen. I want to capture the error as well. Nothing has to be printed on the screen. Any ideas?

解决方案

In the Perl FAQ you have different options depending how do you want to proceed:

http://perldoc.perl.org/perlfaq8.html#How-can-I-capture-STDERR-from-an-external-command%3f

这篇关于如何使用Perl中的Backticks在两个不同的变量中捕获STDOUT和STDERR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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