Unix commands and perl

Discussion in 'Perl Misc' started by AS, Nov 28, 2004.

  1. AS

    AS Guest

    Hi,

    I tried to run a set of Unix commands (separated by |) using the
    'system' command from a perl script, but only a single command works.
    What is a good way to do this?

    I need to take the output of one command put it into the next one use
    that ooutput in a third command. e.g.,
    system ('id | awk '{print $2}' | cut -d\( -f2 | cut -d\) -f1');

    This command in itself gives me the current user's set primary group
    in Unix (Solaris)
    AS, Nov 28, 2004
    #1
    1. Advertising

  2. AS wrote:

    > I need to take the output of one command put it into the next one use
    > that ooutput in a third command. e.g.,
    > system ('id | awk '{print $2}' | cut -d\( -f2 | cut -d\) -f1');


    Is that your real code? If so, you have the quoting mixed up - that
    doesn't even parse.

    Fix the quoting by escaping the inner set of single quotes like this:
    '... | awk \'{print $2}\' | ...'

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
    Sherm Pendley, Nov 28, 2004
    #2
    1. Advertising

  3. AS

    Guest

    (AS) wrote:
    > Hi,
    >
    > I tried to run a set of Unix commands (separated by |) using the
    > 'system' command from a perl script, but only a single command works.
    > What is a good way to do this?
    >
    > I need to take the output of one command put it into the next one use
    > that ooutput in a third command. e.g.,
    > system ('id | awk '{print $2}' | cut -d\( -f2 | cut -d\) -f1');


    That doesn't compile. It gives up when you juxtapose the string
    'id | awk '
    next to the apparent hash reference
    {print $2}
    with out an operator.

    And system doesn't capture the output of whatever it runs, anyway.
    You want backticks or qx.

    > This command in itself gives me the current user's set primary group
    > in Unix (Solaris)


    Since you are using Perl anyway, why not just use `id` and then parse the
    result in Perl?

    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Nov 29, 2004
    #3
  4. On 2004-11-29, scribbled these
    curious markings:
    > Since you are using Perl anyway, why not just use `id` and then parse the
    > result in Perl?


    Or use one or a couple of the get* functions, which has the benefit of
    not returning tainted data.

    --
    I abhor a system designed for the "user", if that word is a coded
    pejorative meaning "stupid and unsophisticated". -- Ken Thompson
    Linux: "How rebellious ... in a conformist sort of way."
    Unix is user friendly. However, it isn't idiot friendly.
    Christopher Nehren, Nov 29, 2004
    #4
  5. On 28 Nov 2004 14:45:58 -0800, (AS) wrote:

    >I need to take the output of one command put it into the next one use
    >that ooutput in a third command. e.g.,
    >system ('id | awk '{print $2}' | cut -d\( -f2 | cut -d\) -f1');


    Incidentally it's very very poor programming to use clumsy constructs
    calling external programs to do something that perl can do more
    reliably, clearly and fast in the first place.

    In other words, no need and (no gain!) to use perl like shell
    scripting: use shell scripting if you really want that...


    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, Nov 29, 2004
    #5
  6. AS

    AS Guest

    Thanks Xho, Villy, Christopher for your constructive suggestions.

    Sorry, everyone got distracted by my EXAMPLE. That is just one of my
    set of commands that I wanted to use. It works fine from the Unix
    command line and I tried to use it within Perl to try to understand
    how Perl works. I wanted to understand using something that I
    understood already. I will be probably using this in several different
    ways until I understand fully how to use all the various parts of
    Perl.

    My point is -- what do I need to do to run multiple commands taking
    the output from one command into another? I am comparatively new to
    Perl and am just getting back into programming. Michele, I would
    appreciate it if you would add "how to do it" rather than just telling
    me it can be done in Perl better. In this example if someone explains
    to me what to do to make it work, I will be very thankful.

    BTW, you guessed right, I did not know about q// quoting till after I
    saw your response and searched for it. Thanks for a constructive
    suggestion.


    Michele Dondi <> wrote in message news:<>...
    > On 28 Nov 2004 14:45:58 -0800, (AS) wrote:
    >
    > >I need to take the output of one command put it into the next one use
    > >that ooutput in a third command. e.g.,
    > >system ('id | awk '{print $2}' | cut -d\( -f2 | cut -d\) -f1');

    >
    > Incidentally it's very very poor programming to use clumsy constructs
    > calling external programs to do something that perl can do more
    > reliably, clearly and fast in the first place.
    >
    > In other words, no need and (no gain!) to use perl like shell
    > scripting: use shell scripting if you really want that...
    >
    >
    > Michele
    AS, Nov 29, 2004
    #6
  7. On Sun, 28 Nov 2004 14:45:58 -0800, AS wrote:

    > Hi,
    >
    > I tried to run a set of Unix commands (separated by |) using the
    > 'system' command from a perl script, but only a single command works.
    > What is a good way to do this?
    >
    > I need to take the output of one command put it into the next one use
    > that ooutput in a third command. e.g.,
    > system ('id | awk '{print $2}' | cut -d\( -f2 | cut -d\) -f1');
    >
    > This command in itself gives me the current user's set primary group
    > in Unix (Solaris)


    To get the same infomation in a Perl one-liner (note: in Linux, I have my
    own group for my UID; it's not really a wise thing in a production
    environment):

    [jim@localhost jim]$ perl -e 'print [getgrgid($<)]->[0],"\n";'
    jim
    [jim@localhost jim]$

    Type `perldoc User::pwent` or `perldoc User::grent` or `perldoc perlvar`
    at the command line to get documentation explaining what I did.

    HTH

    Jim
    James Willmore, Nov 29, 2004
    #7
  8. AS

    Guest

    (AS) wrote:
    > Thanks Xho, Villy, Christopher for your constructive suggestions.
    >
    > Sorry, everyone got distracted by my EXAMPLE. That is just one of my
    > set of commands that I wanted to use. It works fine from the Unix
    > command line and I tried to use it within Perl to try to understand
    > how Perl works. I wanted to understand using something that I
    > understood already.


    Ah, well then. Calling system (or using backticks) can be particularly
    troublesome when you need to consider shell escaping and shell
    interpolation and shell quoting and Perl escaping and Perl interpolation
    and Perl quoting all together in one messy mess. My second rule of thumb
    is when in doubt, temporarily change the "system" to a "print", and make
    sure that what is getting printed matches what you think the shell is
    supposed to be seeing. (Does not apply to the multi-argument form of
    "system", or course)


    Xho

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Nov 29, 2004
    #8
  9. AS

    Joe Smith Guest

    AS wrote:

    > My point is -- what do I need to do to run multiple commands taking
    > the output from one command into another?


    The example you gave works as long as you use the correct quoting
    characters, and remember to put a backslash in front of the dollar
    signs otherwise perl will do variable substitution.

    my $cmd = "prog1 | prog2 '\$1' | prog3";
    print "Command = $cmd\n";
    (system $cmd) == 0 or die 'Command failed';

    -Joe
    Joe Smith, Nov 29, 2004
    #9
  10. On Mon, 29 Nov 2004 12:37:19 -0500, James Willmore
    <> wrote:

    >[jim@localhost jim]$ perl -e 'print [getgrgid($<)]->[0],"\n";'


    Not that I find it disturbing or incorrect, but what is this business
    of referencing and dereferencing?

    perl -le 'print +(getgrgid $<)[0]'


    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, Nov 29, 2004
    #10
  11. On 29 Nov 2004 08:47:50 -0800, (AS) wrote:

    >My point is -- what do I need to do to run multiple commands taking
    >the output from one command into another? I am comparatively new to
    >Perl and am just getting back into programming. Michele, I would


    You don't need anything particular. I think you just want to read

    perldoc -q 'output of a command'

    >appreciate it if you would add "how to do it" rather than just telling
    >me it can be done in Perl better. In this example if someone explains
    >to me what to do to make it work, I will be very thankful.


    Well, any introductory reading on perl, including what you can find in
    the docs would help you to find "how to do it". However, let me see...
    in your post you had:

    : system ('id | awk '{print $2}' | cut -d\( -f2 | cut -d\) -f1');

    then incorrect quoting apart and notwithstanding the fact that another
    user already suggested a "pure perl" solution, let's suppose that we
    want to parse the output of id anyway: one of the many possible ways
    could be (untested)

    print do {
    local $_=qx/id/;
    (split)[0] =~ /\((\w+)\)/g;
    }

    or

    {
    local $_=qx/id/;
    print +(split)[0] =~ /\((\w+)\)/g;
    }

    or...


    HTH,
    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, Nov 29, 2004
    #11
  12. On Mon, 29 Nov 2004 22:18:38 +0100, Michele Dondi wrote:

    > On Mon, 29 Nov 2004 12:37:19 -0500, James Willmore
    > <> wrote:
    >
    >>[jim@localhost jim]$ perl -e 'print [getgrgid($<)]->[0],"\n";'

    >
    > Not that I find it disturbing or incorrect, but what is this business
    > of referencing and dereferencing?
    >
    > perl -le 'print +(getgrgid $<)[0]'


    That's better. I always have to work a little bit harder than I need to :)

    Jim
    James Willmore, Nov 30, 2004
    #12
  13. AS

    Anno Siegel Guest

    James Willmore <> wrote in comp.lang.perl.misc:
    > On Mon, 29 Nov 2004 22:18:38 +0100, Michele Dondi wrote:
    >
    > > On Mon, 29 Nov 2004 12:37:19 -0500, James Willmore
    > > <> wrote:
    > >
    > >>[jim@localhost jim]$ perl -e 'print [getgrgid($<)]->[0],"\n";'

    > >
    > > Not that I find it disturbing or incorrect, but what is this business
    > > of referencing and dereferencing?
    > >
    > > perl -le 'print +(getgrgid $<)[0]'

    >
    > That's better. I always have to work a little bit harder than I need to :)


    That's what getgrgid() returns in scalar context anyway:

    perl -le 'print scalar getgrgid $<'

    is a little longer, but conceptually simpler.

    Anno
    Anno Siegel, Nov 30, 2004
    #13
  14. AS

    Ben Morrow Guest

    Quoth -berlin.de (Anno Siegel):
    > James Willmore <> wrote in comp.lang.perl.misc:
    > > On Mon, 29 Nov 2004 22:18:38 +0100, Michele Dondi wrote:
    > >
    > > > On Mon, 29 Nov 2004 12:37:19 -0500, James Willmore
    > > > <> wrote:
    > > >
    > > >>[jim@localhost jim]$ perl -e 'print [getgrgid($<)]->[0],"\n";'
    > > >
    > > > Not that I find it disturbing or incorrect, but what is this business
    > > > of referencing and dereferencing?
    > > >
    > > > perl -le 'print +(getgrgid $<)[0]'

    > >
    > > That's better. I always have to work a little bit harder than I need to :)

    >
    > That's what getgrgid() returns in scalar context anyway:
    >
    > perl -le 'print scalar getgrgid $<'
    >
    > is a little longer, but conceptually simpler.


    Or, even clearer IMHO

    use User::grent;

    print getgr($<)->name;

    Ben

    --
    Every twenty-four hours about 34k children die from the effects of poverty.
    Meanwhile, the latest estimate is that 2800 people died on 9/11, so it's like
    that image, that ghastly, grey-billowing, double-barrelled fall, repeated
    twelve times every day. Full of children. [Iain Banks]
    Ben Morrow, Nov 30, 2004
    #14
    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. dpackwood
    Replies:
    3
    Views:
    1,768
  2. Ben Pfaff

    Re: man pages for C commands (GCC commands)

    Ben Pfaff, Jun 24, 2003, in forum: C Programming
    Replies:
    4
    Views:
    3,952
    Thomas Stegen
    Jun 28, 2003
  3. Tim Stanka
    Replies:
    1
    Views:
    785
    Jeff Epler
    Aug 2, 2004
  4. timdoyle05

    Python and Unix Commands

    timdoyle05, Sep 19, 2005, in forum: Python
    Replies:
    2
    Views:
    432
    Piet van Oostrum
    Sep 19, 2005
  5. Robert Wallace

    my own perl "dos->unix"/"unix->dos"

    Robert Wallace, Jan 21, 2004, in forum: Perl Misc
    Replies:
    7
    Views:
    267
    Michele Dondi
    Jan 22, 2004
Loading...

Share This Page