Why would you prefer to keep from using bash commands via exec()
in php?
I do not consider portability issue (I definitely will not port it to run on Windows). That's just a matter of a good way of writing scripts.
On the one hand:
- I need to write much more lines in php then in bash to accomplish the same task.
For example, when I need to filter some lines in a file, I just can't imaging using something instead of
cat file | grep string > new_file
. This would take much more time and effort to do in php. - I do not want to analyze all situations when something might go wrong. I will just show bash command output to the user, so he would know what exactly happened.
- I do not need to write another wrapper around filesystem functions and use it. It is much more efficient to leverage the OS for file searching, manipulation etc.
On the other hand:
- Calling unix command with
exec()
might be inefficient in most cases. It is quite expensive to spawn a separate process. Not talking about scripts running under apache, which is even much less efficient than spawning from command line scripts. - Sometimes it turns out to be 'black magic-like' and perl-like scripting. Though it can be avoided via detailed comments.
- Maybe I'm just trying to use two different tools together when they are not supposed to. Each tool has its own application and should not be mixed together.
- Even though I'm sure users will not try to run script will malicious purposes, using
exec()
is a potential security threat. In most cases user data can be escaped withescapeshellarg()
, but it is still an issue to take into account.