tags:

views:

327

answers:

7

For jobs that must be done repeatedly and consist of things like moving files around and appending text is bash scripting still the way to go? If I currently have only a small amount of bash skill should I invest time in learning to do it well, or should I use something else like Perl or Python?

A: 

You can even use Qt/C++. QRegExp and QFile make things a lot easier :-)

MadH
+13  A: 

It's always worth having a variety of skills of course, but I appreciate that time is often tight :)

I'd say Python is likely to give a more maintainable solution if it becomes quite a big task, but bash is fine for smallish scripts. I'd start getting nervous at 100 lines, or if you start needing lots of functions. (Of course, bash experts will be able to find their way around far more complex scripts, but scripts are more likely to be read by "casual" bash users than, say, Java is likely to be read by "casual" Java coders.)

Jon Skeet
+1, I like the point about "casual" users...
rmeador
For python versions of the common bash code, see http://stackoverflow.com/questions/209470/can-i-use-python-as-a-bash-replacement
Nickolay
+2  A: 

To no big surprise I'll agree with Jon Skeet. Even though I always miss my list comprehensions, they are just not native to my shell, and for scripts below 10 lines (my threshold is way lower than Skeets, but so are my abilities), bash or any native shell language is my quick/safe route. And I will not take arguments that suggest me moving to ipython as a native shell, seriously ;)

Steen
+1  A: 

If the solution involves lists, hashes, or any other non-trivial data structures, I would go with a dl like ruby, python, or perl.

On the other hand, if what I'm building will be run on multiple platforms without any gaurantee of a standard tool set then I would go with bash, or even sh for that matter. It all depends on the complexity of the solution and where it will run.

ennuikiller
+1  A: 

In my experience use whatever command line tool that you know. I know some python, but about half of my scripts are written in PHP because that is what I know better.

jrglasgow
+1  A: 

I know that bash is the default /bin/sh for most, if not all, linux distributions, so you can't go wrong with learning how to use it. However, I'd take it a step further and try to stick with strict Bourne shell syntax. That way, your library of scripts will be easily portable to other flavors of UNIX. This assumes, like others point out, that you won't be needing more advanced features, like arrays and stuff, in which case a more advanced language would be in order.

In any case, for learning bash, I'd recommend the Advanced Bash-Scripting Guide as an excellent reference.

Geoff Fritz
+1  A: 

I use both bash and python. Simultaneously. bash is good for short scripts (especially zsh, my favorite when I don't care about portability), python is good for longer, more complex scripts... but python with «embedded» bash scripting is even better... os.popen is your friend.

For example, I often write:

for file in os.popen('find ...'):
    ...

instead of doing os.walk manually, because this is way faster to think about and often much shorter. And... this technique is even better when you use pipes...

Of course, do this with care, as portability might be an issue. Also you need to know both tools pretty well, to know which part write in which language.

liori