Beefy Boxes and Bandwidth Generously Provided by pair Networks chromatic writing perl on a camel
Welcome to the Monastery
 
PerlMonks  

File Handle qwerk?

by mbayer (Novice)
on Apr 10, 2007 at 14:00 UTC ( #609146=perlquestion: print w/ replies, xml ) Need Help??
mbayer has asked for the wisdom of the Perl Monks concerning the following question:

I am attempting to grep the total system memory of a Solaris box with the (prtconf | grep Memory) command, and then user Reg expressions to gather the RAM amount but I received the error: Could not open pipe for prtconf. This should work, Any help would be appreciated.
#!/usr/local/bin/perl -w use FileHandle; $memcmd=`/usr/sbin/prtconf | grep Memory`; $tmem = 0; $tmem_idle = new FileHandle ("$memcmd") || die "Could not open pipe fo +r prtconf: $!\n"; while (<$tmem_idle>) { if ( $_ =~ m/^.*\s+(\d+)\s+\d+$/ ) { $tmem = $1; } } close $tmem_idle; $tmem_busy = $tmem; # Just print the info! print "The total Memory amount is: $tmem_busy"; exit 0;
O.K. -- syntax kills me again. Many thanks for the help.

Comment on File Handle qwerk?
Download Code
Re: File Handle qwerk?
by chargrill (Parson) on Apr 10, 2007 at 14:06 UTC

    This is likely your problem:

    $memcmd=`/usr/sbin/prtconf | grep Memory`;

    ...which you then later try to use as a new FileHandle.

    Try changing that line* to:

    $memcmd="/usr/sbin/prtconf | grep Memory |";

    The alternative is to capture the full output of qx//:

    @memcmd_output = `/usr/sbin/prtconf | grep Memory`;

    ... and then iterate over that ...

    for my $line( @memcmd_output ){ # ... do your stuff on $line }

    * Updated: ikegami pointed out a missing trailing vertical bar.



    --chargrill
    s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)

      Try changing that line to:

      $memcmd="/usr/sbin/prtconf | grep Memory";

      Shouldn't that be

      $memcmd="/usr/sbin/prtconf | grep Memory |";

        Yep. I admit to not testing it - I'm not used to reading the output of a command that way.

        I've corrected it above.



        --chargrill
        s**lil*; $*=join'',sort split q**; s;.*;grr; &&s+(.(.)).+$2$1+; $; = qq-$_-;s,.*,ahc,;$,.=chop for split q,,,reverse;print for($,,$;,$*,$/)
Re: File Handle qwerk?
by Fletch (Chancellor) on Apr 10, 2007 at 14:18 UTC

    Backtick issue that's been pointed out aside, it's almost pointless to spawn an extra grep process from a shell command run from Perl. Just do the winnowing of the output yourself by judicious use of next inside the while loop processing the lines.

    (And I seem to recall an admonition in Perl Best Practices against using anything but IO::File or three argument open.)

Re: File Handle qwerk?
by johngg (Abbot) on Apr 10, 2007 at 14:29 UTC
    As Fletch points out, you might as well use Perl to pull out the line you want from the prtconf output. It is a good idea to use strict; and three argument opens.

    use strict; use warnings; my $prtconfCmd = q{/usr/sbin/prtconf}; open my $prtconfFH, q{-|}, $prtconfCmd or die qq{fork: $prtconfCmd: $!\n}; my $memVal = q{}; while ( <$prtconfFH> ) { next unless m{^Mem.*?:\s+(\d+\s+\S+)}; $memVal = $1; last; } close $prtconfFH; print qq{Total memory is: $memVal\n};

    I hope this is of use.

    Cheers,

    JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://609146]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (9)
As of 2014-04-19 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (478 votes), past polls