Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   processing command line arguments with backticks (http://www.velocityreviews.com/forums/t888191-processing-command-line-arguments-with-backticks.html)

miracle_ks 09-26-2004 05:59 AM

processing command line arguments with backticks
 
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

Brian McCauley 09-26-2004 08:25 AM

Re: processing command line arguments with backticks
 


miracle_ks wrote:

> 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?


Joe Smith 09-27-2004 10:14 AM

Re: processing command line arguments with backticks
 
miracle_ks wrote:

> $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


All times are GMT. The time now is 09:41 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.