my $sys = "dude";
my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;
foreach my $el (@output) {
next if ($el !=m~ ^\truncating\);
print " .................$el\n";
}
Next time please post a helpful subject.
^^^^^^.. first, what do you expect that to do?
> print " .................$el\n";
> }
My questions are:
1. why it print everything right after this "my @output= `snmpwalk -c
public -v 1 $sys .1 |grep \"truncating\"`; " instead of storing it
into @output array
2. Why the @output is empty?
Sorry for bad subject, next time I'll try do better ;-)
Because your check is incorrect. I'm VERY surprised that code even
compiles. Please enable warnings and strict. Anyway, you probably want something like
next if ($el =~ /^truncating/);
> 2. Why the @output is empty?
I'm sure it isn't.
--
Joost Diepenmaat | blog: http://joost.zeekat.nl/ | work: http://zeekat.nl/
I can't reproduce your problem. Things for me work as expected, once I
replace snmpwalk with a command I trust to run on my computer which still
generates output containing "truncating" on some lines.
Are you sure your real code uses backticks, as shown above, and not system,
as your subject line would seem to suggest?
> foreach my $el (@output) {
> next if ($el !=m~ ^\truncating\);
What the heck is that supposed to be? It doesn't compile, whatever it
is.
> print " .................$el\n";
> }
Xho
--
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.
with the backticks you create a new process that starts a shell
to run the command. the output of this command is printed to
the stdout of that shell and is not passed back to your script.
That's what you're seeing when you run your script, the output
of the command goes to the terminal and not your script. All
your script receives is the return value of the last command
that got executed. If you want the output of a command then
don't use backticks but use open with the output of the com-
mand redirected to a file handle that your script can read
from:
open my $f, '|-', 'my_command' or die "Can't execute my_command\n";
Then read in what the command outputs just like it would be a
normal file:
my @output = <$f>;
and finally close this "file":
close $f;
Now '@output' contains all the lines output by the command.
Regards, Jens
--
\ Jens Thoms Toerring ___ j...@toerring.de
\__________________________ http://toerring.de
That's not correct! Read the perlop entry for qx//. You're confusing
backticks with system().
> open my $f, '|-', 'my_command' or die "Can't execute my_command\n";
That creates a process to write to, not to read from!
Leon Timmermans
Sorry, forget about that. Got things mixed up with the system()
function you were refering to in the subject line. Things are
different with backticks.
JG> cyrus...@gmail.com wrote:
>> next if ($el !=m~ ^\truncating\);
JG> ^^^^^^.. first, what do you expect that to do?
Obviously, it inverts a negative hopeful match in rleP :)
I do wish people would post more rleP code.
Ted
In case you are B<absolutely> sure that I<@output> is empty (prove you
are), then (if my crystal globe doesn't joke this time) I would
investigate if B<snmpwalk> uses C<STDOUT> for output (instead of some
other handle).
--
Torvalds' goal for Linux is very simple: World Domination
In case you are B<absolutely> sure that I<@output> is empty (prove you are), then (if my crystal globe doesn't joke this time) I would investigate if B<snmpwalk> uses C<STDOUT> for output (instead of some other handle).
my @output= `snmpwalk -c public -v 1 $sys .1 2>&1 |grep \"truncating\"`;
You could always do the following in the shell:$ snmpwalk -c public -v 1 <sys> .1 1> stdout 2> stderrThen check what goes to stdout and what goes to stderr.