Networker

Re: [Networker] Still can't do this in Perl

2004-11-24 10:11:08
Subject: Re: [Networker] Still can't do this in Perl
From: George Sinclair <George.Sinclair AT NOAA DOT GOV>
To: NETWORKER AT LISTMAIL.TEMPLE DOT EDU
Date: Wed, 24 Nov 2004 10:13:35 -0500
"Maarten Boot (CWEU-USERS/CWNL)" wrote:
>
> Is it going any better ?
>
> Maarten

Thanks for asking. Well, here's a very short draft of a script I came up
with that avoids using temporary files or more advanced things like
Open2 or Open3 modules. In my testing, I've been invoking the script as
something like: `script.pl client clientname`. There is still one thing
that doesn't work the way I'd like it to, but see NOTES following script
below.

#!/usr/bin/perl

# Begin fix
$result = open(CHILD, "-|");
# End fix

die "Couldn't open pipe to subprocess" unless defined($result);
open (ADM, "|/usr/sbin/nsradmin -s server") or
   die "Couldn't open pipe to nsradmin.\n";

print ADM "print type: NSR $ARGV[0]; name: $ARGV[1]\n";
close ADM;

# Begin fix
while(<CHILD>)
{
  push @list, $_;
}
close(CHILD);

foreach (@list)
{
   print $_;
}
# End fix

NOTES: A couple of things here. What was throwing me off was the fact
that I had "|/usr/sbin/nsradmin -s server -i")
and as a result, it was expecting a file to be passed in. Apparently,
you can get away with that in the Bourne shell, but not in Perl. So, by
removing the '-i', that solved that. However, the next problem I ran
into was that while the output of the script was getting dumped to the
console, I was not able to capture that output or save it so I could
then process it -- something I'd like to be able to do. I resolved that
by adding the lines listed between the "Begin fix" and "End fix"
remarks. At the end, I'm not doing anything but just dumping the same
output, but I could parse it, process it, whatever I want since I now
have the output captured in @list. HOWEVER, the one thing I'd like to be
able to do is to turn off the output to the console that gets created
from the line:

print ADM "print type: NSR $ARGV[0]; name: $ARGV[1]\n";

As a result, the script ends up dumping the same information twice. It
would be nice if there was a way that I could
turn off the console and then turn it back on when I want to print
things out from @list. I have used file handle saving before wherein you
direct STDOUT to /dev/null but you save it to another file handle and
then swap it back later, but that won't work here that I can tell. The
only time I've used that technique before was when I needed to run a
command like 'ping' and I wanted to capture the return value of the
command (0 or 1) but I didn't want to see the results of the ping
command. I then turn the STDOUT back on and then I can print out the
return value ($?).

Any ideas?

George

>
> On Monday 22 November 2004 16:58, George Sinclair wrote:
> > I am still unable to perform the equivalent of the following simple
> > Bourne shell script in Perl:
> >
> > #!/bin/sh
> > /usr/sbin/nsradmin -s orion -i -<<EOF
> > print type: NSR $1; name: $2
> > EOF

--
Note: To sign off this list, send a "signoff networker" command via email
to listserv AT listmail.temple DOT edu or visit the list's Web site at
http://listmail.temple.edu/archives/networker.html where you can
also view and post messages to the list. Questions regarding this list
should be sent to stan AT temple DOT edu
=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=