casual_prgmr has asked for the wisdom of the Perl Monks concerning the following question:

perl -e 'system("echo 1; echo 2; echo 3");'

produces the following output on Windows:

1; echo 2; echo 3

The above is consistently produced with ActiveState (have also produced with Oracle's PERL distrib). Only MKS PERL seems to produce the output which I was expecting, i.e.

1 2 3

I also tried:

perl -e 'system("echo 1 && echo 2 && echo 3");'

resulting in:

1 && echo 2 && echo 3

where am I screwing up? Version info:

ActiveState PERL is v5.6.1, Oracle PERL is v5.14.1, MKS PERL is v5.8.5

thanks!

Replies are listed 'Best First'.
Re: Semicolon behaviour in system call
by AnomalousMonk (Archbishop) on Mar 27, 2014 at 17:56 UTC

    Windoze (Win7 in the example below) echo consumes everything on the command line unless it's quoted. Try:

    c:\@Work\Perl\monks>echo "1 & echo 2 & echo 3" & echo two dos & echo t +hree "1 & echo 2 & echo 3" two dos three

    MKS is intended to emulate *nix and is probably running in its *nix-ish shell, hence the *nix-ish behavior.

    Update: Fixed command-line example code: && vice &.

      Indeed... but what's the equivalent in a system call?

      In any case, the testcase using 'echo' was simply to demonstrate the problem; it reproduces in any variety of ways, e.g.

      cd C:\temp perl -e 'system("pwd; pwd");'

      results in

      pwd [-LP] [-d path]

      in other words, a usage error... but *nix/MKS perl produces:

      c:/temp c:/temp

      I guess, in summary, what I'm asking is how a multi-command system call is made with standard Windows PERL distribs

        Indeed... but what's the equivalent in a system call?

        system takes a shell command, so the equivalent of

        echo "1 & echo 2 & echo 3" & echo two dos & echo t

        using system is

        system('echo "1 & echo 2 & echo 3" & echo two dos & echo t');

        Do you realize you are launching a Windows build of Perl (ActivePerl or Strawberry Perl?), and that Windows build of Perl launch cmd rather than (non-existent) /bin/sh? In other words,

        system('pwd; pwd');
        is the same as
        system('cmd', '/x', '/c', 'pwd; pwd');

        Perhaps you are looking for

        # Adjust the path for your system system('c:\\progs\\cygwin\\bin\\sh', '-c', 'pwd; pwd');

        Do you get the same output from

        C:\temp> pwd; pwd

        as you do from

        C:\temp> perl -e 'system("pwd; pwd");'

        Do the same check for:

        C:\temp> echo 1; echo 2; echo 3

        From Perl, you may need to put each command in an array and use "system $_ for @cmds", e.g.

        $ perl -e 'my @cmds = ("echo 1", "echo 2", "echo 3", "pwd", "pwd"); sy +stem $_ for @cmds' 1 2 3 /Users/ken/tmp /Users/ken/tmp

        [I don't have Perl running on any MSWin platform, so I can't test this for you.]

        -- Ken