2016-09-30 7 views
2

基本的には、コマンドが成功したかどうかを確認するには、shell_execを使用してください。shell_execコマンドが正常に実行されたかどうかの確認

シンプルな機能:

public static function foo() 
{ 
    $command = "blabla"; 
    shell_exec($command); 
} 

編集、私はこのようなミスターMの提案を試みた:

foreach($commands as $key => $value) 
    { 
     shell_exec($value, $output, $return); 
    } 

をそして、私はこのエラーを取得する:

Undefined variable: output

答えて

0

あなたがこれを行うことができます:

$output = shell_exec('ls -lart'); 
echo "<pre>$output</pre>"; 

と出力を検証します。

しかし、マニュアルに従って:

Note: This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.

http://php.net/shell_exec

0

それはあなたのコマンドは、任意の出力を返すかどうかによって異なります。

shell_exec — Execute command via shell and return the complete output as a string

と::PHP docsによると、あなたのコマンドで出力が表示されることを期待することができますのであれば

Return Values

The output from the executed command or NULL if an error occurred or the command produces no output.

Note: This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required.

、ちょうどもしくはshell_exec関数の戻り値をキャッチ。

1

コマンドの終了コードだけが必要な場合は、shell_execの代わりにexecを使用する必要があります。

2

exec

exec($command, $output, $return); 

if ($return != 0) { 
    // error occurred 
}else{ 
    // success 
} 
+0

を使用してみてください、私は$出力を定義する必要も$正しく返さないのですか? – utdev

+0

いいえただ上記のコードを使用してください –

+0

私はこのエラーを得ることができません変数$出力 – utdev

関連する問題