php / php/php-src

Running a shell command

オープン
#12,520 コメント 2 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

Feature Status: Needs Triage
主要言語
C
スター
40.4k
フォーク
8.2k
平均マージ
2日 15時間
マージ済み PR(30日)
103

説明

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? 🙂

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

調査の方向性

exec と shell_exec、system、pcntl_exec、popen、proc_open、passthru、backticks の動作とドキュメントを確認し、ここで説明されている Windows のコマンドウィンドウの動作に特に注意してください。Windows と Linux における既存の出力および終了ステータスの要件を比較してください。提案された exec の動作と、その互換性への影響が明確に解決されれば完了です。

索引モデルが issue の本文から書いたものです。

評価

技術スタック
php
領域
cli
issue の種類
機能追加
難易度
5/5
見積もり時間
1週間以上
活発さ
停滞
明瞭さ
おおむね明確
初心者へのやさしさ
25/100

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。