Problem with "system" call - multiple invocations (Newbie)

Discussion in 'Perl Misc' started by Tony, Jun 24, 2003.

  1. Tony

    Tony Guest

    Newbie question,

    Hi guys,

    I need to invoke a perl script from another perlscript
    with different parms passed to the second perl script.

    Code snippet
    ==============
    my ($no1,$no2) = (0,1)
    my $num = "single digit var from other part of program";
    my $file = "/some/path/file_"
    my $perl_script = "/some/path/perl_program";

    if ($num == 1) {
    system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once
    exit(0);
    if ($num == 2) {
    system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times
    system "$perl_script" , "$file$no2" , "&"; # with different file name.
    exit(0);

    # My "$perl_script" program takes the
    # file name and processes it in a process that never
    # will return, that is why I use "&" so the invoking
    # perl script can start the next invocation when there are
    # 2 different file names.

    The above does not work as expected. With $num = 2 the first
    invocation of "$perl_script" runs but not the second one. Killing
    the first one using kill PID allows the second one to start, in other
    words the "&" does not seem to do what it is supposed to do, let
    the calling script continue with the next item on the list.

    What am I doing wrong here?

    I have tried various combinations of `` '' "" but nothing gives.

    Tony

    --
    --------------------------------------------------------------
    To reply directly send to: anthony AT movielink DOT net DOT au
    Replace AT and DOT with @ and . and mail will get through.
    Tony, Jun 24, 2003
    #1
    1. Advertising

  2. Tony

    Tony Guest

    On Tue, 24 Jun 2003 11:20:28 +1000, Tony wrote:

    >
    > Newbie question,
    >


    Replying to my own post...:))

    > Hi guys,
    >
    > I need to invoke a perl script from another perlscript
    > with different parms passed to the second perl script.
    >
    > Code snippet
    > ==============
    > my ($no1,$no2) = (0,1)
    > my $num = "single digit var from other part of program";
    > my $file = "/some/path/file_"
    > my $perl_script = "/some/path/perl_program";
    >
    > if ($num == 1) {
    > system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once
    > exit(0);
    > if ($num == 2) {
    > system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times
    > system "$perl_script" , "$file$no2" , "&"; # with different file name.
    > exit(0);


    After searching even more "igh and low" it would seem that what
    I was trying is simply not possible!

    Instead I inserted fork statements before every required
    invocation of the $perl_script with an "exec" call in every
    child process while placing an exit in the last parent process.

    That seems to have acheived what I wanted, not pretty but works...

    Any other coments on this approach?

    Tny
    >
    > # My "$perl_script" program takes the
    > # file name and processes it in a process that never
    > # will return, that is why I use "&" so the invoking
    > # perl script can start the next invocation when there are
    > # 2 different file names.
    >
    > The above does not work as expected. With $num = 2 the first

    [...]

    --
    --------------------------------------------------------------
    To reply directly send to: anthony AT movielink DOT net DOT au
    Replace AT and DOT with @ and . and mail will get through.
    Tony, Jun 24, 2003
    #2
    1. Advertising

  3. Tony <anthony@no_spam.movielink.net.au> wrote:

    T> Newbie question,

    T> Hi guys,

    T> I need to invoke a perl script from another perlscript
    T> with different parms passed to the second perl script.

    T> Code snippet
    T> ==============
    T> my ($no1,$no2) = (0,1)
    T> my $num = "single digit var from other part of program";
    T> my $file = "/some/path/file_"
    T> my $perl_script = "/some/path/perl_program";

    T> if ($num == 1) {
    T> system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once
    T> exit(0);
    T> if ($num == 2) {
    T> system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times
    T> system "$perl_script" , "$file$no2" , "&"; # with different file name.
    T> exit(0);

    The following does what you want. See below for an explanation.

    #!/usr/bin/perl
    $|++;
    use strict;
    use warnings;
    my $program = "someprogram";
    my $file = "somefile";
    my $num = 2;
    for (1..$num) {
    system "$program $file$_ &";
    }
    exit(0);

    T> # My "$perl_script" program takes the
    T> # file name and processes it in a process that never
    T> # will return, that is why I use "&" so the invoking
    T> # perl script can start the next invocation when there are
    T> # 2 different file names.

    T> The above does not work as expected. With $num = 2 the first
    T> invocation of "$perl_script" runs but not the second one. Killing
    T> the first one using kill PID allows the second one to start, in other
    T> words the "&" does not seem to do what it is supposed to do, let
    T> the calling script continue with the next item on the list.

    T> What am I doing wrong here?

    See perldoc -f system:

    If there is more than one
    argument in LIST, or if LIST is an array with more
    than one value, starts the program given by the
    first element of the list with arguments given by
    the rest of the list. If there is only one scalar
    argument, the argument is checked for shell
    metacharacters, and if there are any, the entire
    argument is passed to the system's command shell
    for parsing (this is "/bin/sh -c" on Unix plat-
    forms, but varies on other platforms).

    In other words, your invocation of system() with more than one
    argument results in shell metacharacters, such as the ampersand,
    being passed directly as an argument to the program. Very loosely
    speaking, it's the difference between:

    $ ls '&'

    and

    $ ls &

    Regards,

    Nicholas

    --
    "Why shouldn't I top-post?" http://www.aglami.com/tpfaq.html
    "Meanings are another story." http://www.ifas.org/wa/glossolalia.html
    Nicholas Dronen, Jun 24, 2003
    #3
  4. "Tony" <anthony@no_spam.movielink.net.au> wrote in message news:<pan.2003.06.24.01.20.28.773936@no_spam.movielink.net.au>...
    > Newbie question,
    >
    > Hi guys,
    >
    > I need to invoke a perl script from another perlscript
    > with different parms passed to the second perl script.
    >
    > Code snippet
    > ==============
    > my ($no1,$no2) = (0,1)
    > my $num = "single digit var from other part of program";
    > my $file = "/some/path/file_"
    > my $perl_script = "/some/path/perl_program";
    >
    > if ($num == 1) {
    > system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script once
    > exit(0);
    > if ($num == 2) {



    > system "$perl_script" , "$file$no1" , "&"; # Invoke $perl_script 2 times

    system "$perl_script $file$no1 &"; # (See below)


    > system "$perl_script" , "$file$no2" , "&"; # with different file name.

    system "$perl_script $file$no2 &"; # (See below)

    > exit(0);
    >
    > # My "$perl_script" program takes the
    > # file name and processes it in a process that never
    > # will return, that is why I use "&" so the invoking
    > # perl script can start the next invocation when there are
    > # 2 different file names.
    >
    > The above does not work as expected. With $num = 2 the first
    > invocation of "$perl_script" runs but not the second one. Killing
    > the first one using kill PID allows the second one to start, in other
    > words the "&" does not seem to do what it is supposed to do, let
    > the calling script continue with the next item on the list.


    Read the docs for "system". If you had called it with one string as a command
    line, it would have been parsed for shell meta-characters. The & symbol is
    something understood by the shell, it doesn't really mean anything to perl.
    I would bet that $ARGV[2] would be equal to '&' in your 2nd script.



    >
    > What am I doing wrong here?
    >
    > I have tried various combinations of `` '' "" but nothing gives.
    >
    > Tony
    Bryan Castillo, Jun 24, 2003
    #4
    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. NOBODY
    Replies:
    2
    Views:
    376
    NOBODY
    Oct 30, 2004
  2. Replies:
    1
    Views:
    588
    Chris Smith
    Dec 5, 2005
  3. Replies:
    11
    Views:
    6,956
  4. Stefan Ram
    Replies:
    20
    Views:
    823
    Roedy Green
    May 2, 2006
  5. Ingo Weiss
    Replies:
    0
    Views:
    120
    Ingo Weiss
    Mar 1, 2010
Loading...

Share This Page