Shell Script Language - Use Perl, Not Bash

Written by BinnyVA on May 2, 2008 – 11:40 pm -

To me, a shell script is a script that automates repetitive tasks. But that is not the ‘official’ definition. Wikipedia has this definition

A shell script is a script written for the shell, or command line interpreter, of an operating system.

I use a definition that defines the purpose of the script - while the others prefer a definition that defines the technology used. I am not going to claim that my definition is better than the other definition - that’s pointless. Besides, even I think that the ‘other’ definition is the right one. But I will try to show you the advantages of my approach.

The Purpose

I like to automate things(in other words, I’m lazy). So I have a nice little collection of custom shell scripts. But there is a huge barrier to writing shell scripts - the language used. Traditionally shell scripts are written in a language provided by the shell - like bash or tcsh. That is the problem - these languages are Bad - with a capital ‘B’.

The Problem

To people who are accustomed to decent languages, these shell languages will seem clunky - or even evil. But since most shell scripts are small, most people don’t mind the torture.

In bash, the control flow commands seem to be thrown in as a after thought rather than something that’s built into the language. If you don’t believe me, compare the ‘if’ loop of bash and Perl.

Bash

This code checks wether the variable ‘$value’ has the value 0 - if so it prints the message ‘No Value’

if [ $value -eq 0 ] ; then
	echo “No Value”
fi

Perl

The same code in Perl…

if($value == 0) {
	print "No Value";
}

Of course, Perl experts will go for if(!$value) or even unless($value) - but that’s not the point. See how better the code looks in Perl. Yeah, even I am surprised to hear those words - Perl is considered by many to be an ‘ugly’ language. But when compared to shell languages, Perl is a gem(sorry about the pun - couldn’t resist).

The Solution

The solution is simple - don’t use a shell language to write your shell scripts - use a high level scripting language like Perl. Or Python, Ruby, PHP, Tcl or even JavaScript.

I still use bash to write shell scripts - but if the shell script has an if condition(other than a simple argument check), I use a higher language - usually Perl.

Advantages of using a Shell Language

  • Its supported in even the tinest linux distros - even in embedded systems.
  • Its the method preferred by the majority - I hope this will change soon.
  • Command calls look more natural in a shell language.

Advantages of using a High Level Language

  • Better Code
  • Easy to maintain
  • Faster development
  • Libraries provide a lot of functionality
  • Easier to port to different platforms.

So why am I telling you all this? Two reasons…

One, the next time you are going to write a shell script, I want you to choose a high level language rather than using bash.

The second reason is that now that my series on Linux MP3 Players are over, I am going to take a small break from desktop posts and write on more ‘linuxy’ topics. And one of those topic is Shell Scripting. So in the future posts, I am going to share some of my shell scripts with you. So when I publish a Perl script and call it a shell script, I don’t want you to get confused .


del.icio.us | Digg it | Furl | ma.gnolia | reddit | StumbleUpon |

Tags: , , , , ,
Posted in Command Line, Programming, Scripting |

5 Comments to “Shell Script Language - Use Perl, Not Bash”

  1. Will Says:

    In your example, you seem to be using -eq instead of != just to make Bash seem more foreign.

    Unlike perl you generally don’t have to install anything extra on your system for BASH

    Is there an easy way to do the following in PERL? Because in BASH doing operations on multiple files at once is very easy.

    The below will also work with filenames with strange characters in their name; it will figure out the escape characters for you so passing in the filenames to another program is no trouble.

    for x in /home/*; do
    echo $x
    done

    Is there an easy way of telling if a file/directory exists using Perl?

    # use -d for directories
    if [ -e /tmp/file.txt ]; then
    echo “file.txt exists”
    fi

    PERL is a nice language, but there is no reason to use it over BASH for automated tasks.

  2. Murphy Says:

    Of course there are easy ways:

    print “$_\n” for ;

    and

    print “file.txt exists” if -e /tmp/file.txt;

    for easy tasks i also use bash - but with strings, calculations … involved i am much more happy with perl

  3. Murphy Says:

    uups, in the first example my angle brackets disappeared..
    it should read

    print ā€œ$_\nā€ for (/home/*);

    and now replace the round brackets with angle ones ;-)

  4. BinnyVA Says:

    > Is there an easy way to do the following in PERL? Because in BASH doing operations on multiple files at once is very easy.
    > The below will also work with filenames with strange characters in their name; it will figure out the escape characters for you so passing in the filenames to another program is no trouble.
    > for x in /home/*; do
    > echo $x
    > done
    In perl, this can be done by
    chdir(’/home/binnyva’);
    foreach $f (<*>) {
    print $f . “\n”;
    }

    > Is there an easy way of telling if a file/directory exists using Perl?
    > # use -d for directories
    > if [ -e /tmp/file.txt ]; then
    > echo “file.txt exists”
    > fi
    Sure..
    if(-e “/tmp/hello.txt”) {
    print “Its there”;
    }

    > PERL is a nice language, but there is no reason to use it over BASH for automated tasks.
    If you are comfortable with bash, there is very little reason to switch over to perl - but most people are not that comfortable with bash. I, for one, prefer using high level languages over bash. But bash has its advantages - as noted in the article.

  5. Shell Script to Backup Files Locally Using Rsync » LinDesk Says:

    [...] control or other backup systems built into the tools I use. Anyway, in the first post of the shell scripts series, let me introduce you to my Rsync based local backup [...]

Leave a Comment