executing bash comands with pipe

F

Fred

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
 
P

Paul Lalli

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
 
M

Michele Dondi

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
 
J

John W. Krahn

Fred said:
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
 
J

Jürgen Exner

Fred said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top