tags:

views:

249

answers:

7

i have never used PHP with CLI, but i have seen scripts run 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?

+1  A: 

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.

xyld
+1  A: 

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.

Steve Robillard
+1  A: 

PHP is only better in the web directory. Bash is better suited for handling command line inputs and general shell semantics, IMO.

chiggsy
To be honest, I'd pick python,ruby, or perl over php to write shell scripts, for similar reasons that I might pick php over bash to serve up content via HTTP.
chiggsy
+2  A: 

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.

zhongshu
Python is better than PHP because it has `shutil` and `getopt` modules which simplify many CLI tasks. You need to do these things by hand for PHP.
too much php
+3  A: 

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
too much php
+1  A: 

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 any standard *nix system. By contrast PHP will not be available always. Of course if you don't intend to use those scripts over unfamiliar system you'll be fine with both.

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 simple record you 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. At the end it all comes down to getting the job done and a matter of subjective preference.

rebus
good example with ls vs scandir:)
never_had_a_name
+1  A: 

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.

Andrew McGregor