php - getcwd() 和 exec ('pwd' ) 总是匹配吗?

标签 php bash

问题

getcwd()exec('pwd') 总是会返回相同的结果吗?如果不是,他们在什么情况下不会?

我尝试了什么

我尝试执行 exec('cd ..') 然后执行 echo exec('pwd') 但返回的路径与如果我没有执行 cd ..。这并不让我感到惊讶,因为 cd .. 可能只在完成它的 exec() 期间存在。

最佳答案

作为“工作目录”的符号链接(symbolic link)至少是 getcwd()exec('pwd') 可以不同的一种情况 - 即使是 bash 内置 pwd/bin/pwd 在这里不同。

# ll /res4/Linux/eldk
lrwxrwxrwx 1 armali ARNGO_res4 9 Oct 20  2008 /res4/Linux/eldk -> eldk3.1.1
# cd /res4/Linux/eldk
# php
<?php
echo getcwd(), "\n";
echo exec('pwd'), "\n";
?>
/res4/Linux/eldk3.1.1
/res4/Linux/eldk
# pwd
/res4/Linux/eldk
# /bin/pwd   
/res4/Linux/eldk3.1.1

pwd 选项:

   -L, --logical
          use PWD from environment, even if it contains symlinks

   -P, --physical
          avoid all symlinks

显然,bash 内置的 pwd 默认为 -L,而 /bin/pwd 默认为 -P。

但对于大多数用例,包括您的用例,两种形式都可以使用,因此这里的区别无关紧要。

关于php - getcwd() 和 exec ('pwd' ) 总是匹配吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16109799/

相关文章:

php - 如何让 xdebug 在 debian squeeze 中处理手动编译的 php 5.2 二进制文件

php - GET 请求时为 "No data received"

linux - 附加到特定行的末尾

bash - 在没有 find 命令的情况下检查文件是否超过 30 天

bash - Cron Jobs 可以使用 Gnome-Open 吗?

php - WP迁移,无法登录WP admin

php - PHP 框架中的 MVC

php - 将 Google API 响应保存在 php 变量中

file - 在 while 或 for 循环中对具有任意扩展名的现有文件进行 Bash 脚本测试

linux - bash 中的 while 循环多个条件有什么问题