php / php/php-src

Running a shell command

Abierto
#12,520 2 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Feature Status: Needs Triage
Lenguaje dominante
C
Estrellas
40.4k
Forks
8.2k
Merge medio
2 d 13 h
PR fusionados (30 d)
96

Descripción

Description

There's a handful of ways to run a command in PHP - I have to look this up every single time, and every time, I end up reaching the conclusion that none of them are exactly good.

The most common, most basic requirement is you just want to run a command, capture the output, and get the exit status code - it appears there is still no simple way to achieve this in PHP.

  • shell_exec can't reliably detect failures, doesn't return the status code, fails for binary output on Windows 👎

  • system only returns the last line of the command output. 👎

  • pcntl_exec is not available on Windows 👎

  • popen requires some hand holding and leads to bugs 👎

  • proc_open requires even more hand holding and is difficult just to get it working at all 👎

  • passthru requires an output buffer, which may be slightly less error prone than other options, but still 👎

  • exec opens a command window (on Windows only?) 👎

Finally, there's the backtick operator, which, having dedicated syntax, seems like the most obvious option - but turns out to be an alias for shell_exec, being one of the worst options overall.

Of all these options, exec probably comes closest to fulfilling the most basic requirements - yet still falls short by opening a command window on Windows; behavior that is (I'm sure, for some historical reason) inconsistent with that of Linux.

Rather than suggesting we add yet another function to run a command, how about fixing this issue with exec?

Yes, that is technically a breaking change, but do we imagine anyone is writing Windows-specific PHP programs that rely on opening command windows, expecting interaction from the user? This seems highly unlikely.

I think it would be really great if this situation could finally be remedied, so that just running a simple command is simple and "feature-complete" with no cross-platform quirks - this should not be something that requires any mental gymnastics, and it should be extremely obvious and easy to find in the manual, e.g. with an opening note on every other manual page for all these other functions.

I'm not going to suggest deprecating any of these functions, as they are most likely all being used for something.

But wouldn't it be great if there was one clear choice going forward that "just works" for the 98% use case? 🙂

Guía de contribución

Abrir la guía de contribución

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Línea de trabajo

Revisa el comportamiento y la documentación de exec junto con shell_exec, system, pcntl_exec, popen, proc_open, passthru y backticks, prestando especial atención al comportamiento de la ventana de comandos de Windows descrito aquí. Compara los requisitos existentes de salida y estado de salida en Windows y Linux; se considera terminado cuando el comportamiento propuesto de exec y su impacto en la compatibilidad estén claramente resueltos.

Escrito por el modelo de indexación a partir del texto del issue.

Evaluación

Stack tecnológico
php
Área
cli
Tipo de issue
Nueva funcionalidad
Dificultad
5/5
Tiempo estimado
Más de una semana
Estado de actividad
Estancado
Claridad
Bastante claro
Aptitud para principiantes
25/100

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.