For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > June 2007 > executing bash comands with pipe









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author executing bash comands with pipe
Fred

2007-06-26, 10:03 pm

Hi everybody,

I am trying to convert some bash scripts to perl. How can I execute
the bash sequence below:

vxdmpadm listctlr all | grep GENESIS | awk '/ENABLE/ {print $1}' |
sort -t c -k 2,3n | tr "\n" " "

This command give me the following result:

root@fj50in># vxdmpadm listctlr all | grep GENESIS | awk '/ENABLE/
{print $1}' | sort -t c -k 2,3n | tr "\n" " "
c4 c5

The vxdmpadm command result:

root@fj50in># vxdmpadm listctlr all
CTLR-NAME ENCLR-TYPE STATE ENCLR-NAME
========================================
=============
c0 Disk ENABLED Disk
c3 EMC_CLARiiON ENABLED EMC_CLARiiON0
c2 EMC_CLARiiON ENABLED EMC_CLARiiON0
c5 GENESIS ENABLED GENESIS0
c4 GENESIS ENABLED GENESIS0

I tried using chop, but the "awk" command part appears not working:

chop (my $status = `(((vxdmpadm listctlr all) | grep GENESIS) | awk
'{print $1}')`);
print "\n\nmake exit status:\n$status\n";

Results:

make exit status:
c5 GENESIS ENABLED GENESIS0
c4 GENESIS ENABLED GENESIS0

TIA,

Fred

Paul Lalli

2007-06-26, 10:03 pm

On Jun 26, 11:03 am, Fred <fbc1...@gmail.com> wrote:

> I tried using chop, but the "awk" command part appears not working:
>
> chop (my $status = `(((vxdmpadm listctlr all) | grep GENESIS) | awk
> '{print $1}')`);
> print "\n\nmake exit status:\n$status\n";


$1 is a Perl variable. You don't want to use the $1 Perl variable.
You want to pass the literal string '$1' to the awk part of your
command. You need to backslash the $ so Perl knows you're saying the
string dollarsign-one, rather than the variable $1.

Paul Lalli

John W. Krahn

2007-06-26, 10:03 pm

Fred wrote:
>
> I am trying to convert some bash scripts to perl. How can I execute
> the bash sequence below:
>
> vxdmpadm listctlr all | grep GENESIS | awk '/ENABLE/ {print $1}' |
> sort -t c -k 2,3n | tr "\n" " "
>
> This command give me the following result:
>
> root@fj50in># vxdmpadm listctlr all | grep GENESIS | awk '/ENABLE/
> {print $1}' | sort -t c -k 2,3n | tr "\n" " "
> c4 c5
>
> The vxdmpadm command result:
>
> root@fj50in># vxdmpadm listctlr all
> CTLR-NAME ENCLR-TYPE STATE ENCLR-NAME
> ========================================
=============
> c0 Disk ENABLED Disk
> c3 EMC_CLARiiON ENABLED EMC_CLARiiON0
> c2 EMC_CLARiiON ENABLED EMC_CLARiiON0
> c5 GENESIS ENABLED GENESIS0
> c4 GENESIS ENABLED GENESIS0
>
> I tried using chop, but the "awk" command part appears not working:
>
> chop (my $status = `(((vxdmpadm listctlr all) | grep GENESIS) | awk
> '{print $1}')`);
> print "\n\nmake exit status:\n$status\n";
>
> Results:
>
> make exit status:
> c5 GENESIS ENABLED GENESIS0
> c4 GENESIS ENABLED GENESIS0



open my $fh, '-|', 'vxdmpadm', 'listctlr', 'all'
or die "Cannot open pipe from 'vxdmpadm' $!";

my @data;

while ( <$fh> ) {
next unless /GENESIS/ and /ENABLE/;
push @data, [ /^(\D+(\d+))/ ];
}

close $fh or warn $! ? "Error closing 'vxdmpadm' pipe: $!"
: "Exit status $? from 'vxdmpadm'";

print join( ' ',
map $_->[ 0 ],
sort { $a->[ 1 ] <=> $b->[ 1 ] }
@data ),
"\n";




John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Michele Dondi

2007-06-26, 10:03 pm

On Tue, 26 Jun 2007 08:03:13 -0700, Fred <fbc1007@gmail.com> wrote:

>I tried using chop, but the "awk" command part appears not working:
>
>chop (my $status = `(((vxdmpadm listctlr all) | grep GENESIS) | awk
>'{print $1}')`);
>print "\n\nmake exit status:\n$status\n";


You would still be using the perl interpreter as a shell which is not
that useful. You should instead

open my $cmd, '-|', qw/vxdmpadm listctlr all/ or die "D'Oh!\n";

and then do stuff while (<$fh> ). What stuff to do depends on the rest,
but I don't know hawk so I can't help you. As far as the grep is
concerned, it can be just as simple as a

next unless /GENESIS/;


Michele
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{po
p^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com