views:

1150

answers:

23

For writing scripts for process automisation in Linux platform, which scripting language will be better? Shell script, Perl or Python or is there anything else? I am new to all of them. So, am just thinking to which one to go for?

+1  A: 

I have been getting Python recommended all the time. It's supposed to let you do anything. For the small tasks i use shell scripts though.

Vargen
+1  A: 

I'd recommend bash, awk, and sed.

bash - http://tldp.org/LDP/abs/html/

awk - http://www.uga.edu/~ucns/wsg/unix/awk/

sed - http://www.ce.berkeley.edu/~kayyum/unix_tips/sedtips.html

Just some ideas.

Sev
A: 

On Linux? Choose your poison, basically. I like Python, others Ruby, still others Perl. Pick one and go for it. :-)

Bernard
+3  A: 

I think it depends on how complex the tasks are you want to automate. Personally, I've always gone with shell-scripts, which enables you to call on awk, sed, grep, find, ls, cat, etc. which can be combined together to do pretty much anything you can achieve using perl or python. On the other hand, if the processes you want to automate are complex (e.g., not just a linear sequence of steps) then you'll probably find that writing the scripts in perl or python (or even ruby!) is much quicker and makes them easier to maintain.

Ben
A: 

Perl of course.

Daud
A: 

I would usually say the one you know best which can achieve the results you want. Like all religious wars, and after learning a large number of languages, you realise that you can do most things in most languages (Note I did say most). I use Perl. It is maybe not as up to date as Python or Ruby, but it does have massive library support from CPAN. And I have not found anything I can't do in it yet. When I do I will look at other languages to find out which one can fill that gap.

If I was starting today, maybe I would pick Python or Ruby, but I don't know enough about them to make a judgement call. Do any of your friends/colleagues know scripting languages. This could help you massively as the support when learning a new language is very important.

Good luck

Xetius
A: 

That depends on which type of automation you are doing like if it is testing autoamtion Perl is suggested because Perl is much powerful extension modules via CPAN, an online Perl module inventory. If you only need a handy tool to complete a simple source file, awk is very convenient. If you are planning to use the scripts to automate a big project, Perl is a better choice with more features. Again Python was designed from the start as an object-oriented language. Perl 5 has some o-o features added on, but it looks to me like an awkward retrofit. Python has well-implemented o-o features for multiple inheritance, polymorphism, and encapsulation.n summary, it seems to me that Python dominates Perl in most applications except for fairly short shell-script sorts of applications, and there they are roughly comparable.

GustlyWind
+3  A: 

Go for all three of them, start with bash/awk/sed plus fileutils (grep, find, and so on) and then move up the abstraction hierarchy with perl and python.

That way you will be able to decide for yourself which one fits your needs best. I say start with bash and friends because they are ubiquitous, some machines will not have perl or python installed and you'll feel helpless there, especially in traditional unix land (ie, not linux)

Vinko Vrsalovic
+1  A: 

Depends on the complexity and problem domain of the task at hand.

Bash scripts are quick and dirty for simple system automation tasks. For more complex things than moving files around and running commands, I'd personally say Perl is next in line as the defacto sys-admin goto automation tool. For more focus on code reuse and readability/maintainability I'd want to step it up it up to Python or Ruby.

PHP can also be used to automate tasks, however it is not widely accepted for this purpose in my experience.

It really comes down to what language you are most interested in learning, most can be used for automation, in addition to many other things.

ctcherry
A: 

Well , it's like this :

Perl is not the most user friendly scripting language , but it has CPAN ( Comprehensive Perl Archive Newtork ) , which contains thousands of libraries that implement almost anything you may think of , and Perl is really powerful , when it comes to text processing. The disadvantage would be that perl code is kinda hard to maintain ( if you don't know it very well ).

Python is a scripting language that is becoming more and more popular among scripters . It doesn't have a community like CPAN ( yet ) , but it's more readable , and it's easier to maintain . It's as fast as perl .

Ruby is the newest trend in scripting languages . Ruby is full OOP , which means that everything is an object . It's advantage is that the code is very readable , and it's pretty easy to learn , if you are a beginner . The main disadvantage is it's execution speed , which kinda s*x.

Vhaerun
+8  A: 

The answer is: Whatever best fits the job!

My rule of thumb;

Bash - for a short script that might need a for loop to do something repetitively.

Perl - anything to do with some kind of text processing or file processing, especially if it's a one off. Just do a dirty nasty perl script and be done with it

Python - If it's something you might want to do again or something very like it. Then at least you have a chance of being able to reuse the script.

Pev
+1  A: 

I prefer shell scripts only for very small tasks. Writing robust shell scripts requires a lot of knowledge about possible pitfalls, which you only learn by doing. But learning even the basics will increase your productivity a lot!

If I need to have complex logic, I usually use Python. By complex I mean anything that has more than two if -statements =)

Perl is okay for its original purpose, but be warned that many of the perlisms you learn are not applicable anywhere else.

Python and Ruby are roughly equivalent. I'd recommend you learn one of them well and check out a tutorial on the other. I prefer Python but it really comes down to personal preference.

To summarize: Learn basics of shell scripts. Learn at least Python or Ruby well.

Antti Rasinen
A: 

Depends on what you want to do, I regularly use all of them:

  • Shell for simple batching of commands with perhaps a loop or an if-statement.
  • Perl when I'm munching files and do some text replacement and souch things.
  • Python when need more logic.

Under *nix you should use the right tool for the right work, which can be hard for the beginner since it's so many things to learn (after some 15 years as a *nix user I still find new things). My recommendation is to look at all the languages quickly to see what they can do, and then start with using shell for everything, when your scripts gets clunky move them to something else.

Mr Shark
+1  A: 

When choosing a scripting language to help automate your linux / unix environment, the most important thing in my opinion is... your replacement :-)

By which I mean the next / other sysadmins who may have to maintain your scripts. I am currently working in an environment where the lead Unix guy is a real script head, but he has mainly restrained himself to using bash, with some perl and windows vbscript thrown in for good luck. At least it has forced me to brush up my perl.

While agreeing with the other comments here, my suggestion would be to master bash - where possible do as much as possible in bash, as most people know it, and can maintain / debug it. And it will be most portable. Use with sed & awk is particularly powerful.

When you have that mastered, you can come back here and ask "What scripting language should I learn after bash?" :-)

JB

Jonathan Bourke
A: 

I'd say Python - it has a very high readability, it is simple( no curly brackets, key words as close to english as possible etc.) and you can do almost everything in it, from simple to very complex things. It is also popular and fun to code.

Oko
A: 

This may sound a little odd, I had been using bash for over 10 years. I have started using PHP5 and it was difficult at first, but now I have a much better reusable code base.

I wouldn't recommend it as a starting point though!

Uresu
A: 

Just write your commands one after the other, put it in a file and run this file with

promp> bash file

and you have your first automation. Then learn about bash variables, loops and control structures.

stephanea
A: 

I second Python - powerful, simple, performant, and... actually quite fun, compared to perl or bash. Also if you know it, you'll find other uses, it's used in a lot of projects. And not just as a "classic" scripting language, take for example the twisted project. That's true for Perl too I guess, but I like Python better order of magnitudes myself

Bottom line though is like has been said beofre, make sure you have the right tool for the job...

Robse
A: 

If I had to pick one, it would have to be AWK. It's lightweight, has a small learning curve and has many useful functions like index and substr.

dogbane
+1  A: 

I use Perl for anything beyond extremely simple scripts.

I also 'use warnings', 'use strict', avoid backticks, call system as 'system($command, @and_args)'. And because I like it to be maintainable: IPC::Run (for pipes), File::Fu (for filenames, tempfiles, etc), YAML (for configs or misc data), and Getopt::Helpful (so I can remember what the options were.)

Eric Wilhelm
A: 

If you aim at having a simple script program "controlling" another (command-line, of course) program, then you should review Tcl/Tk, especially its dialect expect - they're simple and oriented towards that goal - it's very easy to create a script that controls ftp and even does a su with them!

Awk's very nice to process text files - not as powerful as perl, yet much more simple and straightforward (and without the horrible syntax).

Of course, your mileage may vary, so I guess the best answer would be to ask you: what do you want to write scripts for? And then: Are you familiar with any language script? The answers to these questions will point you to the scripting language you should use, according to the pros/cons of each one and their main target.

Joe Pineda
A: 

If you aim at having a simple script program "controlling" another (command-line, of course) program, then you should review Tcl/Tk, especially its dialect expect - they're simple and oriented towards that goal - it's very easy to create a script that controls ftp and even does a su with them!

Awk's very nice to process text files - not as powerful as perl, yet much more simple and straightforward (and without the horrible syntax).

Of course, your mileage may vary, so I guess the best answer would be to ask you: what do you want to write scripts for? And then: Are you familiar with any language script? The answers to these questions will point you to the scripting language you should use, according to the pros/cons of each one and their main target.

Joe Pineda
A: 

If you want minimalistic, compact and fast solution (faster than Python/Ruby) then -> go for LUA scripting language :-) However Lua speed & code compactness is achieved by relativelly small Lua language core, so if you want "batteries included" (aka. very big "standard" libraries) then Lua is not for you. Otherwise, guys who come from C/C++ world very enjoys Lua speed :-)

p.s.

Lua vs Ruby 1.9 benchmark (you can look also Lua Vs Python 3):

http://shootout.alioth.debian.org/u32/benchmark.php?test=all&lang=lua&lang2=yarv

0x69