TurnKey Linux Virtual Appliance Library

Friends don't let friends program in shell script

Lately I've been going over a hellish patch-work of old shell scripts we wrote to automate some internal processes and I realized something: friends shouldn't let friends program in shell script.

Why?

  • Shell script is ugly.
  • Shell script is unreliable.
  • Shell script doesn't have exceptions.
  • Shell script is hard to debug.
  • Shell script is hard to test.
  • Shell script isn't object oriented.
  • Shell script is full of idiosyncratic cruft.
  • Shell script doesn't have any decent data structures.
  • Shell script doesn't scale.
  • Shell script encourages code duplication.

The problem with shell script is that it's so deceptively easy to get started. Once you've started adding each bit of extra complexity seems easier than trashing the whole thing and starting from scratch in a decent language (e.g., Python) with a decent design.

Speaking from personal experience, nearly every time I have used shell script in recent times for something that isn't completely trivial, I have come to regret it. Ugh.

Sometimes you don't have a choice. For example, one time I had to use shell script to create a transparent command line hijacking function for a utility I was working on, and just look at the abomination I am ashamed to have been forced to write:

hijack_command() {
    CALLBACK=$1
    COMMAND=$2
    NEW_COMMAND=$3

    ORIG_COMMAND=$(which $COMMAND 2>/dev/null)

    eval "

        function $COMMAND() {
            args=()
            for arg; do
                args[\${#args[*]}]=\"'\$arg'\"
            done
            if $CALLBACK; then
                eval $NEW_COMMAND \${args[*]}
            else
                if [ \"$ORIG_COMMAND\" ]; then
                    eval $ORIG_COMMAND \${args[*]}
                fi
            fi
        }"
}

When a language forces you to do nested metaprogramming something as trivial as this, run and don't look back!

Have you ever regretted using shell script? Share your thoughts!

You can get future posts delivered by email or good old-fashioned RSS.
TurnKey also has a presence on Google+, Twitter and Facebook.

Comments

What do you recommend instead?

Hi Liraz

So, lets say a Shell script is ugly; what is shiny?

What is the alternative?

Scripting languages like Perl/Python will to the job?

 

Thanks

Beyond a certain complexity - use a real programming language

Python is nice and neat. It's easy to program in, easy to read, and has an extensive standard library which comes with the kitchen sink so you can get many common tasks done with just a couple of lines of code.

I used to program in Perl and I don't recommend it, unless job security is your primary motivator in which case use Perl, go nuts, and pass it all through Liraz's Perl obfuscation service too: http://liraz.org/obfus.html

I use Java

For anything beyond trivial, I use Java.  It is a very structured language so the compiler catches most errors.

http://UnmeteredVPS.net/tkl
Try our TurnKey Linux appliance hosting FREE!

Daniel Serodio's picture

Java is no good for task automation

Java intentionally hides the Operating System from the programmer, and these kind of automation tasks are very OS-specific. Most *nix sysadmins know and like Perl, which is fine for quick hacks, but I think Python is much more elegant, and therefore, maintainable.

Besides, programming in Python is a real joy, and having a REPL shell is great for prototyping.

Java can do system automation

Java has system calls for common OS-specific tasks.  It also allows you to call any arbitrary system command using an exec call.

Python/perl may be better if you are doing a lot of system automation tasks, but since I program in Java for higher level stuff, it is better for me to be able to stick to the same environment for low level stuff too.

http://UnmeteredVPS.net/tkl
Try our TurnKey Linux appliance hosting FREE!

Liraz Siri's picture

Use the best tool for the job

My take on this is that a programming language is just a tool. Use the right tool for the job. Depending on what you want to accomplish different languages may be better suited. I use Python for most of my programming these days. It's not a speed demon though there are several very serious efforts to bring it's performance up to par with languages such as Java.

Note that for most applications Python's performance is not an issue at all. In the past when I've needed to squeeze out more performance out of a specific bottleneck in my code I've implemented that part in C and then created bindings to Python.

But heck it's not a religious thing. To this day I still use Perl when I need to write a dirty one-liner.

Post new comment

The content of this field is kept private and will not be shown publicly. If you have a Gravatar account, used to display your avatar.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <p> <span> <div> <h1> <h2> <h3> <h4> <h5> <h6> <img> <map> <area> <hr> <br> <br /> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <em> <b> <u> <i> <strong> <font> <del> <ins> <sub> <sup> <quote> <blockquote> <pre> <address> <code> <cite> <strike> <caption>

More information about formatting options

Leave this field empty. It's part of a security mechanism.
(Dear spammers: moderators are notified of all new posts. Spam is deleted immediately)