ls -ltr 使用 PHP exec()
正如问题所述.. 当我这样做时,
exec("ls -ltr > output.txt 2>&1",$result,$status);
它与正常输出不同。添加一个额外的列。就像
-rw-r--r-- 1 apache apache 211 Jul 1 15:52 withoutsudo.txt
-rw-r--r-- 1 apache apache 0 Jul 1 15:53 withsudo.txt
从命令提示符执行时的 where as 一样,
-rw-r--r-- 1 apache apache 211 2010-07-01 15:52 withoutsudo.txt
-rw-r--r-- 1 apache apache 274 2010-07-01 15:53 withsudo.txt
-rw-r--r-- 1 apache apache 346 2010-07-01 15:55 sudominusu.txt
-rw-r--r-- 1 apache apache 414 2010-07-01 15:58 sudominusu.txt
看看区别。所以在第一个输出中,我通常的 awk '{print $8}' 失败了。 我在 cron 中也遇到了同样的问题。但是通过调用
./$HOME/.bashrc
脚本解决了这个问题。但使用 php 却没有发生。如果以某种方式我可以“告诉”php从通常的环境中“执行”。任何帮助将不胜感激。
as the problem states..
when i do
exec("ls -ltr > output.txt 2>&1",$result,$status);
its different from the normal output. An extra column gets added. something like
-rw-r--r-- 1 apache apache 211 Jul 1 15:52 withoutsudo.txt
-rw-r--r-- 1 apache apache 0 Jul 1 15:53 withsudo.txt
where as when executed from the command prompt its like
-rw-r--r-- 1 apache apache 211 2010-07-01 15:52 withoutsudo.txt
-rw-r--r-- 1 apache apache 274 2010-07-01 15:53 withsudo.txt
-rw-r--r-- 1 apache apache 346 2010-07-01 15:55 sudominusu.txt
-rw-r--r-- 1 apache apache 414 2010-07-01 15:58 sudominusu.txt
See the difference. So in the first output , my usual awk '{print $8}' fails.
I was facing the same problem with cron. But solved it by calling
./$HOME/.bashrc
in the script. But not happening using php. If somehow i can "tell" php to "exec" from the usual environment. Any help would be appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在您的登录 shell 中,
ls
可能是别名,以便它打印另一个日期。这将位于您的 .basrc 或 .bash_profile 中。将
--time-style=
选项显式传递给ls
以确保它在使用 PHP 时以预期格式打印日期。In your login shell,
ls
is probably aliased so that it prints another date. This would be in your .basrc or .bash_profile.Explicitly pass the
--time-style=
option tols
to ensure that it prints the date in the expected format when using PHP.我猜您只对文件名感兴趣,并且想按反向时间排序。
试试这个:
ls -tr1 > output.txt 2>&1
您将得到一个仅包含文件名的列表,因此您根本不需要 awk。
另一种解决方案是使用“--time-style iso”指定时间格式。查看 手册页
I guess you are only interested in the file names and you want to sort with reverse time.
Try this:
ls -tr1 > output.txt 2>&1
You'll get a list with only the file names, so you don't need awk at all.
Another solution is to specify the time format with "--time-style iso". Have a look at the man page
这不是额外的输出,而是日期格式的差异。显然你在 PHP 和 bash 中设置了不同的区域设置(“命令提示符”)。
(在 bash 中,运行
export LANG=C
或export LANG=en_US
会给出包含三个字母的月份名称的结果)That's not an extra output, that's a difference in formatting the date. Apparently you have a different locale set in PHP and in bash ("command prompt").
(in bash, running
export LANG=C
orexport LANG=en_US
gives the result with three-letter month name)ls
的输出在很大程度上取决于环境(例如,LANG
是这里的重要变量)。为什么不使用scandir
的组合、stat
和krsort
?这比使用
ls
更安全、更高效。更不用说您将受益于以在exec
内部难以完成的方式自定义排序和过滤结果。顺便说一句:我绝不是 PHP 专家,因此上面的代码片段可能非常不安全并且充满错误。
The output of
ls
is heavily dependent on the environment (e.g.,LANG
being the important variable here). Why not use a combination ofscandir
,stat
, andkrsort
?This will be safer and a lot more efficient than shelling out to
ls
. Not to mention that you get the benefit of being about to customize the sorting and filter the results in ways that are difficult to do inside of anexec
.BTW: I am by no means a PHP expert, so the above snippet is likely to be incredibly unsafe and full of errors.