PHP vs BASH for CLI scripting? [closed]
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
开发者_C百科 Improve this questionI have never used PHP with CLI, but I have seen scripts running with PHP code.
I was wondering:
- Why should we use BASH, when PHP is so popular and is able to run in CLI?
- What are the pros and cons with each one?
- Should I use PHP for all CLI scripting in the future?
It is still much easier to perform many common tasks in bash. Consider writing PHP equivalents for the following bash one-liners:
# remove a folder full of rubbish
rm -rf oldTrashFolder
# create an archive of files containing the word 'localhost'
tar cf backup.tar $(grep -rlw localhost ~/my-source-code)
# make a copy of a mysql database
{ echo "create database NEWDATABASE; "; mysqldump OLDDATABASE; } | mysql NEWDATABASE
Bash is more common for this type of application, simply by virtue of it being around forever. But when it comes to choosing one over the other. The language you already know is usually the better choice. Just because you will be more productive.
I guess the main thing is: bash is always there on a recent unix system (its predecessors really are always there, even on ancient unixen). Bash uses every little utility on the system, which can include PHP, Python, Ruby, AWK, Perl (those last two especially).
Consider: how do you get PHP installed? Bunch of bash scripts and a makefile, right? Or your OS package manager, which these days is probably Python, but used to be a bash script as well.
You can't administer unix without knowing the shell really well, nor can you write or use makefiles. It may not be the right answer every time, but for a scripting job I always try to figure out if it can be done in bash first.
Preference mainly.
Higher level languages (like php) have better data structures easily accessible (among other things). However, it's pretty easy to hack together a working bash script.
So really, it's up to you.
PHP is only better in the web directory. Bash is better suited for handling command line inputs and general shell semantics, IMO.
I think python is better than php for CLI.
If you have never used PHP, I recommend Python, because PHP was originally designed for web development to produce dynamic web pages, and for Python, It's more generous and emphasize code readability.
For general purpose task scripting i would suggest going Bash all the way.
Bash is probably most widely spread shell and your script will be able to run on most *nix systems. By contrast PHP will not always be readily available. Of course if you don't intend to use those scripts over unfamiliar system you'll be fine with both.
That said, most standard shell commands are easier to write in bash then in php. In php you would open a subprocesses, execute shell commands through some wrapper functions or what not while in bash you would simply record your bash session in a file an made it executable. Take listing of a directory for example, in bash you simply state ls
and in php you'd probably be using scandir which isn't fun as it sounds. Also i would imagine scandir has lot less sorting options. grep -Ri something .
anyone?
Most of the general purpose languages can be used for shell scripting, but that doesn't mean they should be and at the end of the day it comes down to getting the job done and a matter of subjective preference.
I was looking into this myself as I'm current doing a upgrade a on large LMS having come across this link and another which has some result to a proformance test I thought I would add this link to this post. while it task is nota real world example it does show some interesting stats.
精彩评论