views:

199

answers:

9

I'm new in using scripting language. I come across 2 scripting languages called Perl and Bash shell script.

I would like to know what kind of things can be done using Perl script but cannot be done using Bash shell script?

Thanks.

A: 

Practically Bash is much less flexible than Perl. Bash has no algorithms or data structures except (indexed or associative) arrays, and no ways to structure or modularize your source code except functions. In addition (and in contradiction to its inflexibility), Bash is quite complicated and unintuitive and it's extremely easy to introduce bugs that are hard to spot. So my advice is: avoid Bash whenever possible, many other languages are better for usual programming tasks. Perl is fine, but if you're a beginner it might be too complicated and distracting—there are always many ways to do something, and it's up to you to choose which is best. I'd recommend Python instead.

If you want/have to learn Bash, be prepared that lots of information or example code is naive or plain wrong (a lot of scripts already break when given file names containing spaces!). My recommendation for Bash programming is the extensive documentation on Greg Wooledge's wiki, e.g. the Bash guide, the Bash FAQ and the Bash pitfalls.

For possible downvoters: I'm only talking about Bash when used as a scripting language, not when used as interactive shell, in which case it's fine. I you don't think that Bash sucks, read the various threads about Bash here, or read the well-known Bash Pitfalls: most of the problems in Bash arise from its inability to use data structures correctly and on its reliance on external programs to do basic data processing. I'd estimate that 95% of all problems that Bash programmers face simply cannot arise in languages with a sane syntax and real data structures and algorithms.

Philipp
"nothing" is a very strong word. Maybe when you graduate you'll see both are used quite often in the real world. But you're right that python has largely supplanted perl. And "nearly every other language is infinitely much better" is just wrong, depending on your task at hand.
Stephen
I'm writing Bash scripts all day and use a version control system that is written in two languages that I dislike (C and Bash), nevertheless I think that Bash is one of the worst programming languages in existence. Often I start with a simple Bash script, thinking it will be easier to write than a similar Python script, but in nearly all cases this proves wrong.
Philipp
But you're right, the wording was too strong, I edited it a bit. Still I'd say that if the choice is whether to learn Bash or to learn Perl, Perl is the sane choice.
Philipp
Bash does have functions: http://tldp.org/HOWTO/Bash-Prog-Intro-HOWTO-8.html
GreenMatt
Nearly all imperative languages, including MASM and early versions of FØRTRAN, have some sort of functions/subroutines. But Perl and Python also have classes, modules, namespaces etc.
Philipp
@Philipp: My statement that Bash has functions was in response to "Bash has ... no ways to structure or modularize your source code.", if your comment about other languages having functions/subroutines was a response to my earlier comment. I consider functions a significant and important way to modularize code.
GreenMatt
You don't know or understand shell. There are plenty of tasks that it can accomplish *much* more easily and elegantly than other languages. Yes, its abilities for heavy-duty programming are strictly limited, but the shell isn't supposed to do heavy-duty work; it's supposed to get a bunch of *other* stuff to do heavy-duty work while serving as the glue between all of the pieces. It should be *added* to your list of languages, not used in isolation.
hobbs
+5  A: 

Bash is a shell and its not meant to be a full fledge programming language, like Perl/Python. It doesn't have things like modules or packages etc. However, bash shell comes with programming facilities such as arrays, flow control eg if/else, loops (for , while) etc that allows you to do simple scripting. To make the bash shell almost equivalent to the power of Python/Perl, you should use it together with unix tools like sed/awk, etc. With these combined, bash scripting/programming can be as powerful.

I don't agree with poster Phillip about avoiding Bash because some things are better done with the shell and *nix tools than in other languages.

In conclusion, you should try to learn bash, as well as Perl or Python. Your knowledge of them will come in handy next time.

ghostdog74
I agree that you should learn Bash because usually you have to read and maintain one of the million Bash scripts out there. But I find it hard to find any problem where Bash programming would be as flexible, fast and extensible as, say, Python programming. My wording was a bit strong, and I don't say "never write or learn Bash", but if a beginner wants a recommendation, I can't do anything but tell him to stay away from Bash as far as possible.
Philipp
+1  A: 

Basically if all I want to do is set up for and execute a program or run a series of "built-in" commands like sed, grep or awk, I'd use Bash. If I had to do any calculation, data manipulation or looping, I'd use perl. And I've been doing Bourne shell scripting and perl for 20+ years. If I was just starting out, I'd use Python instead of perl.

Paul Tomblin
+5  A: 

Perl and Bash where made with two different goals. Perl is a general purpose language and bash is just a interface to the operating system with some programming capabilities.

Ed
+1  A: 

I'm new in using scripting language. I come across 2 scripting languages called Perl and Bash shell script.

Cool. Both are useful technologies, both can be over-extended to be a nightmare to maintain and debug.

I would like to know what kind of things can be done using Perl script but cannot be done using Bash shell script?

Both are turing complete, so you can do anything in either. But that's not a helpful answer :) Both have their merits, bash (or ksh, csh) is often used for configuration scripts or driver scripts. Look in your /etc/init.d directory for a list of scripts of this flavor. Once you get beyond these sorts of scripts, bash tends to become more difficult than it's worth.

Perl is a little more feature-ful with libraries, etc. It's biggest strength (so I've been told, I don't do much perl programming), is it's text processing. It's convenient to (and I think it was originally designed to) parse text files, which make a sysadmin's life easier.

What would you suggest? (you didn't ask this, but I'm pretending you did :) )

If you want to learn a scripting language, learn python. It'll be a more familiar language, it's easy to learn and it's extremely powerful. Knowing bash and python, I've never felt the need to learn perl - but stand in awe of the mangled syntactic mess that spew forth into "programs".

You'll pick up bash along the way, maybe read a tutorial or two, pick up a few bookmarks. You'll mostly learn it from hacking stuff at the command line, or tossing together a few lines of .bashrc or something. Don't worry about "learning" it. You'll just absorb it over time, then peek into the corners when you realize you need it.

Stephen
"the mangled syntactic mess that spew forth into \"programs\"." To the untrained eye, that is.
fengshaun
@fengshaun: definitely! I'm sure if I knew perl it would be poetic :)
Stephen
+1  A: 

To answer at the most basic level, with perl you can access CPAN, which (as far as I know, anyways) you can't do with bash. CPAN contains libraries to do, well, just about anything you'd want to do with a programming language. Probably one of the most common things you'd do with perl that you can't (easily) do with bash is to interface with a database.

chris
A: 

There is no programming language without gotchas!

http://www.yosefk.com/blog/python-teaching-kids-and-biting-bits-dont-mix.html

sig
A: 

If this is for a system administrator related job, you must know both. The very basics is a shell because that is what you use on unix to execute your commands. Perl is important because i find it easier to use for strings processing (although most of those you can do in a shell as well), you can use this to automate log reporting and examination, but also for quickly linking several commands together. Nothing a shell can't do, but these days more and more files are in xml format (for example) and that is easier to process in perl, same with web based interfaces etc.

You will also notice that a lot of tools will have modules available for perl, or for a large part they might be written in perl (for example certain products from symantec).

Regarding python, it is a very nice language and there is high usage mostly on linux platforms (a lot of redhat & ubuntu tools are written in python). While this is not the case on unix platforms (solaris, hpux - python is not even installed by default on these), which still lean more towards perl. could be important, regarding on what OS your main focus will be.

dirk
+3  A: 

Here's the tl;dr version: if you find yourself writing more than a page of bash, stop and rewrite it in a real programming language. I don't care if its Perl or Python or Ruby. Just don't create unmaintainable 500 line shell hairballs.

Now, in addition to the existing answers...

Perl has a debugger.

A lot of people don't use the Perl debugger, but I rely heavily on it. Its a nightmare for me to figure out what a big shell script is doing. I find this cycle: "sprinkle debugging echos around the code, run, look at the debugging output, realize I need to know something else, sprinkle more echos, repeat" to be very slow and frustrating. Running code in a debugger lets you examine the data as you need it, and perform more examinations as you have a better understanding what you need.

Perl is cross platform.

A bash script isn't going to work on Windows without help, but that's not really what I'm talking about. Bash relies on the salad of command line programs that come with a Unix system... maybe. Assuming they exist, these don't always work the same. Things like find and grep. Is it the GNU version? The BSD version? Maybe an old Sun/AIX/ version? Does it support the flags you're throwing at it?

But hey, you only use Fedora or Debian or whatever, who cares? Eventually either your distribution is going to change something incompatibly, or you're going to want to move to a different system and suddenly you find yourself with a huge mess. "Why is that server still running Redhat 7?!" is often because it has some fragile shell scripts doing something important.

Which brings us to...

Perl can declare its dependencies.

If a Perl program needs a certain version of Perl and certain modules it can declare that in a machine readable way by the relatively simple measure of distributing it as a Perl module. Bash has no such mechanism (you could make a dpkg or rpm, which is its own hell) so you have no idea what sort of assumptions your bash script has made.

Perl is easy to test.

I'm sure its possible to decompose and test a bash program, and someone out there writes beautifully architected shell code. Its like skiing uphill, or a dancing bear. Its very impressive, but not very practical. So nobody does it. Whereas Perl ships with a test framework, has easy tricks to make even single-file programs easy to test, and encourages you to decompose large programs into libraries.

Schwern