views:

259

answers:

13

What language do you prefer writing scripts for common tasks (backup, sync, etc.) and why? I'm not talking about programing web pages or applications. I've got this question when thinking why bash is still popular. For example python looks more comfortable for me. Do you use just because you know it or for some special reasons?

+2  A: 

Unix has a philosophy of small tools which do one particular job and do it well. Often the easiest way to solve problems is to use a combination of such tools. Shell scripting is the king for this, no questions about that.

Of course, there's also the "when all you have is a hammer" syndrome :)

oggy
A: 

G'day,

Different tasks call for different languages. I tend to use either shell, usually bash, or Perl depending on the task.

Now I'm getting more comfortable with Ruby, for those tasks that might suit an OO approach, I'll use that.

HTH

cheers,

Rob Wells
+3  A: 

This really depends on the type of script. I am starting to use ruby for many sysadmin type tasks however bash is still my first choice for quick and dirty scripts. The advantage of bash, in my eyes, is the interactive nature of it.

To give an example. The other day I was searching for some particular values in approx 200 compressed log files, re-formatting the output and mailing the results.

It was very easy to use bash to do this iteratively, so, zcat one file piping the output to grep, retrying a few times to get the regex correct. Then take that output and reformat the result using awk, again retrying several times to get the format correct.

This process took a couple of minutes after which I wrote the bash commands into a script file, paramaterized it, wrapped a for loop around it, mailed the result and the job was done.

I find this process much simpler in bash just using command editing and retrying the regexes etc than I would in a separate script file where I have to keep editing the script and retrying etc.

Steve Weet
+1  A: 

Perl would be best in handling system administration tasks. I have never come across a *nix system that does not have Perl installed.

Alan Haggai Alavi
+1  A: 

Python for me at the moment, I like using python because it has an interactive terminal that I can use to build up and execute the script as I go along - but I used perl in the past.

Kinlan
A: 

Bash is the preferred scripting language for these kinds of tasks. It's pretty ubiquitous, and it's intended to be a glue language, in the sense that you can glue together a bunch of commands that you would normally do in the terminal pretty much unchanged.

Jeremy Cantrell
+3  A: 

If it's "create this directory, run this command, if that worked do run this"-level, I just use bash shell-scripts..

Anything more complicated, say something that parses the output of a command and acts upon it, becomes a Python script - I find it just as quick to write, mainly because shell scripts are difficult to debug (bash script error messages aren't exactly useful compared to Python's tracebacks..), and the end code becomes much more readable

dbr
A: 

I use Ruby for most of my shell scripting tasks. I can never remember some of the nuanaces/gotchas of Bash scripting.

mipadi
+1  A: 

Bash, or various sh dialects in the broader sense can be assumed to be present on pretty much any unix system. Often, production Unix systems (Solaris, HP/UX, AIX etc.) have a very plain vanilla install; quite often they will not have perl or python installed. There may be company policies restricting this, so getting it installed may not be an option either. If you want something that will work on this type of platform, you will probably be limited to sh/sed/awk.

Bash is quite good for tasks that primarily involve running other commands, so you shouldn't underrate it. However, it rapidly becomes a write-only language at fairly trivial levels of complexity, so Perl or Python might be a better choice if you are programming something with a lot of internal processing.

For scheduling a backup, a bash script run from cron is quite possibly the best way to do the job. For something that involves parsing multiple log files, formatting the output to a summary status file and sending you an email notification if it notices certain types of events you might be better off with perl or python.

ConcernedOfTunbridgeWells
+3  A: 

...why bash is still popular?

Well, because Bourne Shell ( sh , and not necessarily bash ) is pretty much available in any +*n*x installation.

A good command of sh and vi its extremely helpful when connecting to remote servers via telnet/ssh

For local admin ( when you own the server ) you can use python/perl/ruby and customize them at your will. But most certainly, any day you could be asked to "quickly fix" other server where the two defaults are installed: sh+vi

That's why.

OscarRyz
A: 

I use Ruby because I am most comfortable in it. It's one of the few languages in which I find myself struggling with the logic of my problem, rather than the syntax or restrictions of the language. Compare this to C++ or Perl, in which I get frustrated over pointers and sigils. I find recursive directory traversal and running system commands very easy to do in Ruby, e.g. using Ruby to rename files and edit their content.

Sarah Vessels
A: 

I use perl, typically. The module library at CPAN makes many tasks simple. Net::SSH is a great tool for automating system administration tasks.

TJF
A: 

After using different flavors of Linux and BSD, I tend to prototype in Bash or Korn Shell, and convert to Bourne Shell afterward for portability. Never failed me yet.

l0b0