使用PHP exec()的ls -ltr [英] ls -ltr using PHP exec()
问题描述
作为问题状态. 当我做
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
看看区别.因此,在第一个输出中,我通常的awk'{print $ 8}'失败了. 我在使用cron时遇到了同样的问题.但是通过调用
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
脚本中的
.但是不会发生使用php.如果我能以某种方式将php从通常的环境讲"到"exec".任何帮助,将不胜感激.
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.
推荐答案
我想您只对文件名感兴趣,并且想按相反的时间排序. 试试这个:
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
ls -tr1 > output.txt 2>&1
您将获得仅包含文件名的列表,因此根本不需要awk.
You'll get a list with only the file names, so you don't need awk at all.
另一种解决方案是使用"--time-style iso"指定时间格式.看看手册页
Another solution is to specify the time format with "--time-style iso". Have a look at the man page
这篇关于使用PHP exec()的ls -ltr的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!