更改 shell_exec 中的目录不起作用
Changing directory in shell_exec does not work
我有 运行 BASH-脚本的例程:
// $cmd is something like '/var/tmp/script.sh'
function runshell($cmd)
{
$work_dir = dirname($cmd);
$work_file = basename($cmd);
$message = shell_exec("cd " . $work_dir . " && " . $work_file . " 2>&1");
return $message;
}
问题是它不改变目录并且 $work_file
永远找不到
解决方案1
使用文件的完整路径
$message = shell_exec($work_dir . "/" . $work_file . " 2>&1");
解决方案2
您也可以为此使用 chdir()
。它将更改 php 脚本的当前工作目录。
chdir($work_dir);
$message = shell_exec($work_file . " 2>&1");
我有 运行 BASH-脚本的例程:
// $cmd is something like '/var/tmp/script.sh'
function runshell($cmd)
{
$work_dir = dirname($cmd);
$work_file = basename($cmd);
$message = shell_exec("cd " . $work_dir . " && " . $work_file . " 2>&1");
return $message;
}
问题是它不改变目录并且 $work_file
永远找不到
解决方案1
使用文件的完整路径
$message = shell_exec($work_dir . "/" . $work_file . " 2>&1");
解决方案2
您也可以为此使用 chdir()
。它将更改 php 脚本的当前工作目录。
chdir($work_dir);
$message = shell_exec($work_file . " 2>&1");