Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

SOS...on system command

0 views
Skip to first unread message

cyrus...@gmail.com

unread,
Jul 17, 2008, 4:35:30 PM7/17/08
to
I'm trying to store out put into the array the search for string in
this case truncating, but when I run the following command it prints
all the output before going to the foreach loop, it was my
understanding that the out put of the of the command will be saved
into @output then it can be searched, can some tell me where I go
wrong on this..thanks in advance...


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";
}

J. Gleixner

unread,
Jul 17, 2008, 4:45:38 PM7/17/08
to
cyrus...@gmail.com wrote:

Next time please post a helpful subject.

^^^^^^.. first, what do you expect that to do?
> print " .................$el\n";
> }

cyrus...@gmail.com

unread,
Jul 17, 2008, 4:57:24 PM7/17/08
to
On Jul 17, 1:45 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
wrote:

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 ;-)

Joost Diepenmaat

unread,
Jul 17, 2008, 5:00:27 PM7/17/08
to
cyrus...@gmail.com writes:

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/

xho...@gmail.com

unread,
Jul 17, 2008, 5:02:58 PM7/17/08
to
cyrus...@gmail.com wrote:
> I'm trying to store out put into the array the search for string in
> this case truncating, but when I run the following command it prints
> all the output before going to the foreach loop, it was my
> understanding that the out put of the of the command will be saved
> into @output then it can be searched, can some tell me where I go
> wrong on this..thanks in advance...
>
> my $sys = "dude";
> my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;

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.

Jens Thoms Toerring

unread,
Jul 17, 2008, 5:12:07 PM7/17/08
to

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

Leon Timmermans

unread,
Jul 17, 2008, 5:18:50 PM7/17/08
to
On Thu, 17 Jul 2008 21:12:07 +0000, Jens Thoms Toerring wrote:
> 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.

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

Jens Thoms Toerring

unread,
Jul 17, 2008, 5:22:59 PM7/17/08
to

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.

Ted Zlatanov

unread,
Jul 18, 2008, 9:52:53 AM7/18/08
to
On Thu, 17 Jul 2008 15:45:38 -0500 "J. Gleixner" <glex_n...@qwest-spam-no.invalid> wrote:

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

Eric Pozharski

unread,
Jul 18, 2008, 3:20:14 PM7/18/08
to
cyrus...@gmail.com wrote:
> On Jul 17, 1:45 pm, "J. Gleixner" <glex_no-s...@qwest-spam-no.invalid>
> wrote:
>> cyrusgre...@gmail.com wrote:
*SKIP*

>> > my @output= `snmpwalk -c public -v 1 $sys .1 |grep \"truncating\"`;
*SKIP*

> 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?

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

Andrew DeFaria

unread,
Jul 19, 2008, 11:04:23 PM7/19/08
to
Eric Pozharski wrote:
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).
That'd be my guess too. Depending on your shell you might try:
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> stderr
Then check what goes to stdout and what goes to stderr.
--
Andrew DeFaria
Puritanism: The haunting fear that someone, somewhere may be happy.
0 new messages