无法使用变量执行 shell_exec()

unable to execute shell_exec() with variable

我正在尝试 运行 shell_exec() 使用从客户端通过 AJAX 传递的变量。

此代码导致错误(输入文件不存在!):

$searched_image = escapeshellarg("/home/XXX/XXX/XXX/XXX/XXX/sp_dom1.jpg");
$old_path = getcwd();
chdir('../elevation/source_code/altitudes_system/');
$altitudes_system_result = shell_exec('./predict_altitude.sh -i "{$searched_image}" -p basic -o 0');
chdir($old_path);

但是当我用 /home/XXX/XXX/XXX/XXX/XXX/sp_dom1.jpg 代码替换 shell_exec(...) 中的“{$searched_image}”时,代码运行良好:

$old_path = getcwd();
chdir('../elevation/source_code/altitudes_system/');
$altitudes_system_result = shell_exec('./predict_altitude.sh -i /home/XXX/XXX/XXX/XXX/XXX/sp_dom1.jpg -p basic -o 0');
chdir($old_path);

你不知道为什么会这样吗?

你写:

'./predict_altitude.sh -i "{$searched_image}" -p basic -o 0'

不计算单引号字符串内的变量。

您可以改用这个:

"./predict_altitude.sh -i '{$searched_image}' -p basic -o 0"

或者 - 为了避免不可预测的评估 - 这个:

$cmd = './predict_altitude.sh -i \''.$searched_image.'\' -p basic -o 0';
shell_exec( $cmd );