ls -ltr 使用 PHP exec()

发布于 2024-09-08 04:15:11 字数 813 浏览 1 评论 0原文

正如问题所述.. 当我这样做时,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

最美的太阳 2024-09-15 04:15:11

在您的登录 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 to ls to ensure that it prints the date in the expected format when using PHP.

睡美人的小仙女 2024-09-15 04:15:11

我猜您只对文件名感兴趣,并且想按反向时间排序。
试试这个:

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

谁的新欢旧爱 2024-09-15 04:15:11

这不是额外的输出,而是日期格式的差异。显然你在 PHP 和 bash 中设置了不同的区域设置(“命令提示符”)。

(在 bash 中,运行 export LANG=Cexport 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 or export LANG=en_US gives the result with three-letter month name)

不忘初心 2024-09-15 04:15:11

ls 的输出在很大程度上取决于环境(例如,LANG 是这里的重要变量)。为什么不使用 scandir 的组合、 statkrsort

function ls($dir_name) {
  $finfo = array();
  foreach (scandir($dir_name) as $file_name) {
    $s = stat(join('/', array($dir_name,$file_name)));
    $finfo[$file_name] = $s['mtime'];
  }
  krsort($finfo);
  return array_keys($finfo);
}

这比使用 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 of scandir, stat, and krsort?

function ls($dir_name) {
  $finfo = array();
  foreach (scandir($dir_name) as $file_name) {
    $s = stat(join('/', array($dir_name,$file_name)));
    $finfo[$file_name] = $s['mtime'];
  }
  krsort($finfo);
  return array_keys($finfo);
}

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 an exec.

BTW: I am by no means a PHP expert, so the above snippet is likely to be incredibly unsafe and full of errors.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文