processing command line arguments with backticks

Discussion in 'Perl Misc' started by miracle_ks, Sep 26, 2004.

  1. miracle_ks

    miracle_ks Guest

    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
    miracle_ks, Sep 26, 2004
    #1
    1. Advertising

  2. 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?
    Brian McCauley, Sep 26, 2004
    #2
    1. Advertising

  3. miracle_ks

    Joe Smith Guest

    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
    Joe Smith, Sep 27, 2004
    #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. Replies:
    4
    Views:
    1,694
    Terry Hancock
    Apr 23, 2005
  2. Gregor H.

    Processing command line arguments

    Gregor H., Apr 21, 2007, in forum: C Programming
    Replies:
    5
    Views:
    499
  3. Tim Hunter
    Replies:
    9
    Views:
    166
    James Edward Gray II
    Jun 28, 2007
  4. Ravi Parimi
    Replies:
    8
    Views:
    106
    Peter Sundstrom
    Jan 26, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    148
    PerlFAQ Server
    Apr 17, 2011
Loading...

Share This Page