0

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
2
  • 1
    I think the problem is that {} is expanded by the shell, not by the ls command. PHP's exec doesn't go through a shell, or at least not the same kind of shell, hence doesn't expand the shorthand. Commented Feb 25, 2011 at 1:58
  • so ... anyway to solve it ? ;) Commented Feb 25, 2011 at 2:03

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.