Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > executing bash comands with pipe

Reply
Thread Tools

executing bash comands with pipe

 
 
Fred
Guest
Posts: n/a
 
      06-26-2007
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

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      06-26-2007
On Jun 26, 11:03 am, Fred <(E-Mail Removed)> 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

 
Reply With Quote
 
 
 
 
Michele Dondi
Guest
Posts: n/a
 
      06-26-2007
On Tue, 26 Jun 2007 08:03:13 -0700, Fred <(E-Mail Removed)> 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{pop^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,
 
Reply With Quote
 
John W. Krahn
Guest
Posts: n/a
 
      06-26-2007
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
 
Reply With Quote
 
Jürgen Exner
Guest
Posts: n/a
 
      06-26-2007
Fred wrote:
> I am trying to convert some bash scripts to perl. How can I execute
> the bash sequence below:


That would not be a particularly good idea because then you would still be
using Perl as a (poor) shell substitute.
I suggest to write a genuine Perl program instead.

> vxdmpadm listctlr all


No idea what vxdmpadm does, you may have to call that one as an external
program using backticks such that you can capture its output.

| grep GENESIS
Perl has its own buildin grep() function, no need to launch an external
process

| awk '/ENABLE/ {print $1}' |
No idea what this awk command does in detail, but it's pretty simply to
replicate the functionality in native Perl.

> sort -t c -k 2,3n

Perl has its own buildin sort() function, no need to launch an external
process

| tr "\n" " "
Perl has its own buildin tr() function, no need to launch an external
process

jue


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pipe perl output to a bash command error Tarek Elganainy Perl Misc 6 02-03-2009 09:41 AM
converting a sed / grep / awk / . . . bash pipe line into python hofer Python 11 09-03-2008 09:56 PM
Comands C/C++ for GPRS Analyser Tiago C++ 7 02-04-2008 12:47 PM
(patch for Bash) GTK+2 + Bash William Park XML 3 07-14-2005 12:09 AM
How to read data from a bash pipe j. del C++ 13 03-28-2005 04:00 PM



Advertisments