php shell_exec()插值

php shell_exec()插值,php,shell-exec,Php,Shell Exec,我有一个php脚本,它构建了一个动态命令字符串(称为perl脚本),然后执行该命令,如下所示: $cmd_string = "perl $pushFile"; foreach($cmd_args AS $argName => $arg){ $cmd_string .= ' --' . $argName . '="' . $arg . '"'; } $output = shell_exec('export PERL5LIB=/mnt/path/

我有一个php脚本,它构建了一个动态命令字符串(称为perl脚本),然后执行该命令,如下所示:

    $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”,并出现字符串未终止的错误。但是,如果我将$cmd_字符串打印到屏幕上并通过命令行执行它,或者将其复制/粘贴到$cmd_字符串变量中,它将正确执行。我被难住了。如何确保正确传递这些参数?我试过这个:

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

但是得到同样的结果。帮助?

在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');