processing command line arguments with backticks

M

miracle_ks

Hi All,

I am trying to run a system command in my perl script, such that I
specify some command line options within the script and then take the
next argument from an input file and then the final argument again
within the script:

$command='/bin/somecommand -x -y -z';
$p1=2456;
foreach $item (@in_arr){
print OUT `$command $item $p1`;
}

When i run this, only last item from the input file is run properly
with $p1 argument. For all other items $p1 is not taken into
consideration.

Any help will be appreciated.

Regards
 
B

Brian McCauley

miracle_ks said:
I am trying to run a system command in my perl script, such that I
specify some command line options within the script and then take the
next argument from an input file and then the final argument again
within the script:

$command='/bin/somecommand -x -y -z';
$p1=2456;
foreach $item (@in_arr){
print OUT `$command $item $p1`;
}

When i run this, only last item from the input file is run properly
with $p1 argument. For all other items $p1 is not taken into
consideration.

Any help will be appreciated.

You should try to construct a _minimal_ but _complete_ script to
illustrate your problem.

Random shot in the dark, you forgot to chomp() the data that you read
into @in_arr and that data also lacks a newline at EOF.

However if this were the case you'd expect to see errors about 2345 not
being found. Did you perhaps send STDERR somewhere you don't see it?
 
J

Joe Smith

miracle_ks said:
$command='/bin/somecommand -x -y -z';
$p1=2456;
foreach $item (@in_arr){
print OUT `$command $item $p1`;
}

When i run this, only last item from the input file is run properly
with $p1 argument. For all other items $p1 is not taken into
consideration.

I was able to reproduce your symptoms by using this:
print OUT `$command $item1\n $p1`;
print OUT `$command $item2 $p1`;

The solution is obvious: get rid of the extra \n by using chomp().
-Joe
 

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top