3

I just want to know that if want to do some mastery over linux and and along the way i need to do some automation of some tasks in linux

Which language should i use for that

1)SHell scripting
2)Python
3)C
4) C++
5)Perl

I think shell scripting does pretty much everything , why i need any other prog language?

1

7 Answers 7

8

Automation should (very rarely) be done in C or C++ - partly because they need compiling, which makes quick changes a bit slower to do, but mostly because they're designed for writing proper programs.

As a general rule, if what you're trying to do is simple enough for shell scripting, use that. If it's not, use the scripting language of your choice. Personally, I like perl, and I've found it is slightly more common to have it installed, but use whatever you know best. Some swear by python, some by ruby, etc.

3
  • 1
    +1 for perl, & it being the Chuck Norris of computer scripting languages.
    – grufftech
    Jun 29, 2010 at 7:27
  • 2
    PHP can also be used as scripting language for the command line, it does a pretty good job and is really close to Perl.
    – Weboide
    Jun 29, 2010 at 10:58
  • Perl is the best scripting language around the world ! ;)
    – aleroot
    Jun 29, 2010 at 11:51
4

A practical example: suppose you have some postfix log and have to create a report of the most often used sender-recipient pairs. Probably the best way to do this is to create a hashtable, containing sender-recipient pairs as keys and the number of occurences as values.

To implement it in bash you lack pretty much everything, you don't have hashtables, you can't easily handle pairs and even to increment a counter you have to use some pretty wierd expression.

However if you have python you have all these structures and can handle every aspect of the problem out-of-the-box. So this problem is much easier to solve in python.

Another example would be to simply check if a specific sender sent an e-mail today. In bash you can just use "grep sender maillog" and you are done. In python you have to create a way longer script to achieve the same. So in this case, bash wins.

Choose the tool that best suits your needs. From the five options, I would focus on bash and python, they pretty much cover everything.

2
  • Bash 4 has associative arrays. In earlier versions you can still store the values as variables, with the prefixed hash as the variable name. It's a bit like Buckley's Mixture. Another option is to create a directory structure in which to store a heirarchical set of values. One of bash's real strong points is its emphasis on teaching you how to do absurd creative things to get around language limitations. This is dangerous, however: after prolonged exposure, one begins to question if this isn't actually the right way to do these things.
    – intuited
    Jun 29, 2010 at 7:36
  • Yepp, you can simulate a hashtable in bash, you can simulate a pair in bash but my point is that it is easier to just use this functionality if it is part of the language. Ages ago I even had a proper stack implementation written in bash - now that's not something that I'm proud of :-D
    – Zizzencs
    Jun 29, 2010 at 8:11
2

You are right, shell programming can do everything :). In fact sh is the 'language' that scores better in the scriptometer. That said, it depends on how serieous you do want to automante and maintain your code and if you plan to reuse it.

I usually do this:

  • I use bash and commandline tools to create simple scripts (as far as I can do them with my knowlegde of bash)
  • I use ruby for everything else

In ruby there are several gems that aid on automating tasks:

  • rake: ruby make
  • sake: system-wide rake
  • rush: shell with ruby syntax
  • capistrano: tool for automating tasks on one or more remote servers

I found this book on system administration with ruby very useful.

1

The theoretical answer would be "use whatever is best-suited to fix your problem".

In practice, you maybe know two or three languages, so stick to them. For example you can do pretty much with bash, but you can also end up in a mess. If you prefer performance, use C or Python.

To sum it up, it depends on what automation you want to do. Is it a modular tool? Most of them are easily extendable.

0

If you want to become a master you are going to have to learn to work with a lot of legacy scripts and code that was created by someone else. This means you probably should learn the basic of most of the languages you identified, and I think you probably need to start by learning how to understand basic shell scripting. Then work on getting at least a basic understanding of perl/python. If you are interested in doing kernel debugging you'll need to learn C.

2
  • It's important to learn shell scripting first, because learning Python makes it unlearnable.
    – intuited
    Jun 29, 2010 at 7:39
  • i know very basic shell scripting , can i proceed to python or after i need to go more advanced in python
    – John
    Jun 30, 2010 at 0:28
0

Firstly, "shell scripting" is more than just:

for (( i = 0; i < 10; i++ )); do
    echo ${i}
done

It's about knowing what tools perform specific tasks, how to use them effectively, and how to find things out. Asking here or using man -k answers the last one. The Advanced Bash-Scripting Guide goes a long way in answering the other two, but ultimately you learn a lot of these things from experience.

Proper programming languages will do things differently and often more efficiently. Just because you can write FORTRAN in any language doesn't mean you should - you need to understand the UNIX philosophy of "do one thing and do it well" that shell scripting utilises, and "use the right tool for the right job" to decide when you need to start doing things in other languages.

The art of programming is something to ask Stack Overflow about.

2
  • Can i use mysql database insertion with shell scripting ,
    – John
    Jun 29, 2010 at 7:58
  • mysql can receive SQL on its standard input, so it that sense you can. It just depends what you are feeding it. mysql db_name < script.sql > output.tab
    – Tobu
    Jun 29, 2010 at 10:31
0

Shell and Perl from heresay , but find the best language by trying out yourself !

You must log in to answer this question.