douzhun5971 2012-10-06 07:52
浏览 168
已采纳

“which”命令在PHP中不起作用

I have two remote servers that need to have a certain codebase in sync. I have it all set up on the first, but while setting it up on the second, I ran into a problem:

They don't have hg installed in the same location. I can't just run hg <command> because it returns the error:

sh: hg: command not found

So I had been using the full path, but that won't work with two separate hg locations.

I thought it would be clever to run which hg and use the path from the response, so here's the new code:

$hg = trim(`which hg`, "
");
$output = `{$hg} pull -u`

But $hg is NULL, so that doesn't work! I can ssh into the 2nd server and see that which hg definitely does work. I even appended 2>> path/to/log to see if there were any errors with the which command, but there was not. I made sure it was writing to the log, so it wasn't related to that.

I am not running in safe mode, and I am definitely allowed to run shell_exec because other commands work.

I know I could just create a symlink so they both had the same path and just hardcode the path, but it's driving me crazy why shell_exec('which hg') isn't working!!!

  • 写回答

2条回答 默认 最新

  • dongqian8265 2012-10-06 07:55
    关注

    which hg will only work if hg is already in your path, otherwise it will return the empty string.

    Obviously this is not what you want, since if it were already in your path, you could just use hg.

    If you're using many machines with different configs, you could just create a bin directory in your home directory, add it to your path and make links to the commands you need in that directory. That will allow you to use the same commands on all machines.

    For PHP, you'd need to put it in a directory accessible to the web server (but preferably outside the document root), besides that, you can use the same technique there by giving an absolute path to the common directory where you created your links.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥15 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)