php exec 命令不允许使用 { } 定义多个文件

发布于 2024-10-19 09:56:53 字数 1333 浏览 2 评论 0原文

我需要执行一个适用于多个文件的 exec 命令,但它不起作用。我将创建一个示例来展示这一点。显然,我不需要这个目录的任何内容,并且我使用“ls”命令执行示例,尽管与任何其他命令一起发生:

exec("ls -al /etc/security/", $output);
print_r($output);

Array
(
    [0] => total 40
    [1] => drwxr-xr-x  2 root root 4096 Jan 27  2010 .
    [2] => drwxr-xr-x 83 root root 4096 Feb 24 12:38 ..
    [3] => -rw-r--r--  1 root root 4266 Apr  9  2008 access.conf
    [4] => -rw-r--r--  1 root root 3551 Apr  9  2008 group.conf
    [5] => -rw-r--r--  1 root root 1911 Apr  9  2008 limits.conf
    [6] => -rw-r--r--  1 root root 1507 Apr  9  2008 namespace.conf
    [7] => -rwxr-xr-x  1 root root  977 Apr  9  2008 namespace.init
    [8] => -rw-------  1 root root    0 Jan 27  2010 opasswd
    [9] => -rw-r--r--  1 root root 2980 Apr  9  2008 pam_env.conf
    [10] => -rw-r--r--  1 root root 2180 Apr  9  2008 time.conf
)

但是尝试使用更多文件......

exec("ls -al /etc/security/{access.conf,group.conf}", $output);
print_r($output);

ls: cannot access /etc/security/{access.conf,group.conf}: No such file or directory

当然这在控制台中有效:

$ ls -al /etc/security/{access.conf,group.conf}
-rw-r--r-- 1 root root 4266 2008-04-09 15:25 /etc/security/access.conf
-rw-r--r-- 1 root root 3551 2008-04-09 15:25 /etc/security/group.conf

I need to do an exec command that applies to several files, but it's not working. I'm going to create an EXAMPLE to show this. Obviously, I don't need anything of this directory and I do the example with "ls" command, although happens with any other command:

exec("ls -al /etc/security/", $output);
print_r($output);

Array
(
    [0] => total 40
    [1] => drwxr-xr-x  2 root root 4096 Jan 27  2010 .
    [2] => drwxr-xr-x 83 root root 4096 Feb 24 12:38 ..
    [3] => -rw-r--r--  1 root root 4266 Apr  9  2008 access.conf
    [4] => -rw-r--r--  1 root root 3551 Apr  9  2008 group.conf
    [5] => -rw-r--r--  1 root root 1911 Apr  9  2008 limits.conf
    [6] => -rw-r--r--  1 root root 1507 Apr  9  2008 namespace.conf
    [7] => -rwxr-xr-x  1 root root  977 Apr  9  2008 namespace.init
    [8] => -rw-------  1 root root    0 Jan 27  2010 opasswd
    [9] => -rw-r--r--  1 root root 2980 Apr  9  2008 pam_env.conf
    [10] => -rw-r--r--  1 root root 2180 Apr  9  2008 time.conf
)

But trying with more files ...

exec("ls -al /etc/security/{access.conf,group.conf}", $output);
print_r($output);

ls: cannot access /etc/security/{access.conf,group.conf}: No such file or directory

Of course this works in console:

$ ls -al /etc/security/{access.conf,group.conf}
-rw-r--r-- 1 root root 4266 2008-04-09 15:25 /etc/security/access.conf
-rw-r--r-- 1 root root 3551 2008-04-09 15:25 /etc/security/group.conf

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

凉墨 2024-10-26 09:56:53

正如评论中提到的,这是 shell 扩展 {} 简写的问题,在 PHP 环境中不会发生这种情况。您必须单独提供完整路径:

exec('ls -al /etc/security/access.conf /etc/security/group.conf', $output);

As mentioned in the comments, this is a problem of the shell expanding the {} shorthand, which does not happen in a PHP environment. You'll have to supply the full paths separately:

exec('ls -al /etc/security/access.conf /etc/security/group.conf', $output);
默嘫て 2024-10-26 09:56:53

如果您有权这样做,那么这可能只是让 bash 成为 /bin/sh 的提供者的问题。

If you have the rights to do so, this is probably simply a matter of making bash the provider for /bin/sh.

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