Problem with executing UNIX command in perl script

S

sunil

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.
 
J

Joost Diepenmaat

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
 
T

Tintin

sunil said:
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.
 

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

Forum statistics

Threads
473,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top