duanlvxing7707
2015-03-19 06:02
浏览 90
已采纳

更改shell_exec中的目录不起作用

I have this routine for running BASH-scripts:

// $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;
}

The problem is that it doesn't change directory and $work_file is never found

图片转代码服务由CSDN问答提供 功能建议

我有运行BASH脚本的例程:

   // $ cmd类似于'/var/tmp/script.sh'
nfunctionhell($ cmd)
 {
 $ work_dir = dirname($ cmd); 
 $ work_file = basename($ cmd); \  n $ message = shell_exec(“cd”。$ work_dir。“&&”。$ work_file。“2>& 1”); 
返回$ message; 
} 
   
 
 

问题是它不会更改目录并且永远找不到 $ work_file

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doubao12345 2015-03-19 06:09
    已采纳

    Solution1

    Use full path to file

    $message = shell_exec($work_dir . "/" . $work_file . " 2>&1");
    

    Solution2

    Also you can use chdir() for this. It will change current working directory of the php script.

    chdir($work_dir);
    $message = shell_exec($work_file . " 2>&1");
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题