Problem with executing UNIX command in perl script

Discussion in 'Perl Misc' started by sunil, Dec 25, 2003.

  1. sunil

    sunil Guest

    Hi All,
    I am trying to execute a complex piped UNIX command in perl
    script as follows:

    system("nm @ARGV | awk -F\| '$4 == "OBJT " && $5 == "GLOB " {print $8
    " " $3}'| grep '^[^$]' | uniq | sort -r | awk '$1 != prevvar
    {varstr=sprintf("char %s[%d];",$1,$2);print varstr;prevvar = $1}' |
    sort > $ofname");

    and it fails with many errors. The errors are compile time errors and
    more problems than that. awk uses $0,$1 and I noticed that perl may
    substitute its own values for $1....
    I tried to get information from PERL documentation and it says that if
    I pass entire command and argument list as single scalar (which I am
    doing), if there are any shell metacharacters (there are in current
    case) it is passed to shell for parsing. Hence it should be able to
    execute this.
    Any pointers to documentation which can give me more insight into this
    or any help will be greatly appreciated.
    Thanks,
    Sunil.
     
    sunil, Dec 25, 2003
    #1
    1. Advertising

  2. On Wed, 24 Dec 2003 17:47:36 -0800, sunil wrote:

    > Hi All,
    > I am trying to execute a complex piped UNIX command in perl
    > script as follows:
    >
    > system("nm @ARGV | awk -F\| '$4 == "OBJT " && $5 == "GLOB " {print $8 " "
    > $3}'| grep '^[^$]' | uniq | sort -r | awk '$1 != prevvar
    > {varstr=sprintf("char %s[%d];",$1,$2);print varstr;prevvar = $1}' | sort >
    > $ofname");
    >
    > and it fails with many errors. The errors are compile time errors and more
    > problems than that. awk uses $0,$1 and I noticed that perl may substitute
    > its own values for $1....
    > I tried to get information from PERL documentation and it says that if I
    > pass entire command and argument list as single scalar (which I am doing),
    > if there are any shell metacharacters (there are in current case) it is
    > passed to shell for parsing. Hence it should be able to execute this.
    > Any pointers to documentation which can give me more insight into this or
    > any help will be greatly appreciated. Thanks,
    > Sunil.


    You are using a literal " character in a "-delimited string, for one. Also
    note that using a "-delimited string will interpolate Perl variables typed
    in the string literal.

    To combat all of this use something like (untested):

    system q[m @ARGV | awk -F\| '$4 == "OBJT " && $5 == "GLOB " {print $8 " "
    $3}'| grep '^[^$]' | uniq | sort -r | awk '$1 != prevvar
    {varstr=sprintf("char %s[%d];",$1,$2);print varstr;prevvar = $1}' | sort >
    $ofname];

    where the q[ ... ] construct is equivalent to ' ... ' (that is,
    uninterpolated string), but doesn't get confused by your use of '
    characters in the string literal.

    look for qq and q in "perldoc perlop" for more info.

    HTH
    --
    Joost Diepenmaat
    There are rot13's in my address. Remove them and apply one to mail me.
     
    Joost Diepenmaat, Dec 25, 2003
    #2
    1. Advertising

  3. sunil

    Tintin Guest

    "sunil" <> wrote in message
    news:...
    > Hi All,
    > I am trying to execute a complex piped UNIX command in perl
    > script as follows:
    >
    > system("nm @ARGV | awk -F\| '$4 == "OBJT " && $5 == "GLOB " {print $8
    > " " $3}'| grep '^[^$]' | uniq | sort -r | awk '$1 != prevvar
    > {varstr=sprintf("char %s[%d];",$1,$2);print varstr;prevvar = $1}' |
    > sort > $ofname");


    Why not use Perl to process the output from nm? Otherwise, you'd be better
    off just writing it as a shell/awk script.
     
    Tintin, Dec 25, 2003
    #3
    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,873
  2. ruds
    Replies:
    2
    Views:
    335
  3. gil
    Replies:
    3
    Views:
    114
  4. Replies:
    3
    Views:
    212
    Josef Moellers
    Nov 1, 2007
  5. Slickuser

    executing unix command on a diff domain

    Slickuser, Dec 9, 2008, in forum: Perl Misc
    Replies:
    3
    Views:
    123
    Slickuser
    Dec 10, 2008
Loading...

Share This Page