2013-11-01 59 views
0

我有一個PHP腳本生成動態命令字符串(調用一個perl腳本),然後執行命令,就像這樣:PHP了shell_exec()插值

$cmd_string = "perl $pushFile"; 
    foreach($cmd_args AS $argName => $arg){ 
     $cmd_string .= ' --' . $argName . '="' . $arg . '"'; 
    } 
    $output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . $cmd_string . ' 2>&1'); 

我得到,我認爲正在失敗由一些參數的插值引起的。例如,其中一個參數是'246 + 8GT> - ',它變成了'246 8GT',並且該字符串沒有終止。但是,如果我將print_r $ cmd_string添加到屏幕並通過命令行執行它,或者將它複製/粘貼到$ cmd_string變量中,它會正確執行。我很難過。我如何確保這些參數正確傳遞?我試過這個:

$output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . escapeshellcmd($cmd_string) . ' 2>&1'); 

但得到相同的結果。幫幫我?

回答

0

構建完成後,您正在轉義comamand字符串。

試試這個:

$cmd_string = "perl $pushFile"; 
foreach($cmd_args AS $argName => $arg){ 
    $cmd_string .= ' --' . $argName . '="' . escapeshellarg($arg) . '"'; 
} 
$output = shell_exec('export PERL5LIB=/mnt/path/to/custom:$PERL5LIB && ' . $cmd_string . ' 2>&1');