executing bash comands with pipe

Discussion in 'Perl Misc' started by Fred, Jun 26, 2007.

  1. Fred

    Fred Guest

    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
     
    Fred, Jun 26, 2007
    #1
    1. Advertising

  2. Fred

    Paul Lalli Guest

    On Jun 26, 11:03 am, Fred <> 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
     
    Paul Lalli, Jun 26, 2007
    #2
    1. Advertising

  3. On Tue, 26 Jun 2007 08:03:13 -0700, Fred <> 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,
     
    Michele Dondi, Jun 26, 2007
    #3
  4. 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
     
    John W. Krahn, Jun 26, 2007
    #4
  5. 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
     
    Jürgen Exner, Jun 26, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,320
    William Park
    Jul 14, 2005
  2. j. del
    Replies:
    13
    Views:
    18,780
    Howard
    Mar 28, 2005
  3. Tiago
    Replies:
    7
    Views:
    861
    Michael DOUBEZ
    Feb 4, 2008
  4. hofer
    Replies:
    11
    Views:
    2,676
  5. Tarek Elganainy

    pipe perl output to a bash command error

    Tarek Elganainy, Feb 2, 2009, in forum: Perl Misc
    Replies:
    6
    Views:
    335
Loading...

Share This Page