in reply to Re: Inconsistent system call from backticks vs system()
in thread Inconsistent system call from backticks vs system()

If the 2>&1 trick works, then he is going through the shell.
  • Comment on Re: Re: Inconsistent system call from backticks vs system()

Replies are listed 'Best First'.
Re^3: Inconsistent system call from backticks vs system() (little-known)
by tye (Sage) on Jul 01, 2003 at 15:13 UTC
    If the 2>&1 trick works, then he is going through the shell.

    Actually, if the only "shell meta characters" in your command are a trailing "2>&1", then Perl will still avoid using the shell and simply redirect STDERR to STDOUT after it forks but before it execs.

    I think this feature was added to Perl fairly recently and I have yet to notice it having been documented.

                    - tye
      I find this very interesting. If anyone finds any more information about this, can they please post a reference to it.

      thanks.

        The Perl 5.006_00 source code contains (in doio.c):

        for (s = cmd; *s; s++) { if (*s != ' ' && !isALPHA(*s) && strchr("$&*(){}[]'\";\\|?<>~`\n", +*s)) { if (*s == '\n' && !s[1]) { *s = '\0'; break; } /* handle the 2>&1 construct at the end */ if (*s == '>' && s[1] == '&' && s[2] == '1' && s > cmd + 1 && s[-1] == '2' && isSPACE(s[-2]) && (!s[3] || isSPACE(s[3]))) { char *t = s + 3; while (*t && isSPACE(*t)) ++t; if (!*t && (dup2(1,2) != -1)) { s[-2] = '\0'; break; } } doshell: PerlProc_execl(PL_sh_path, "sh", "-c", cmd, (char*)0); return FALSE; } }
        So, when it is checking for shell meta characters, if it finds " 2>&1" at the end (without any shell meta characters before that) and if dup2(1,2) != -1, then it strips the " 2>&1" and avoids the shell.

                        - tye