a little madness

A man needs a little madness, or else he never dares cut the rope and be free -Nikos Kazantzakis

Zutubi

Your Next Programming Language

Many people talk about how, as software developers, we should learn new programming languages frequently. I couldn’t agree more: the broader perspective improves your skills and opens your eyes to the dark corners of the language you are currently using. It strikes me, however, that many developers are missing out on a class of languages that are extremely useful every day. People learn high-level languages like Java and C++, and often a scripting language or two like Perl or Python. Maybe they will even dabble in a functional language to get a really different take on the world. But for me, the single programming language I use most frequently day-to-day, alongside my primary language, is bash scripting. Yep, plain old hackish shell scripts.

Why? Because like most programmers, I’m lazy. I don’t like to do anything I can make a computer do for me, and there are a whole raft of such things that are easily achieved via a shell script. Often it will just be a one-liner to perform a batch operation on a bunch of files. A find/exec/sed sure beats the pants off changing 200 files by hand, and is even quicker than writing a Perl script. Shell scripting is also a boon for project automation. Is packaging your project a headache? Need to pull in a bunch of resources, munge a few files, run some tests and squeeze it all together? Build tools such as Ant or make may get you part of the way, but they are not designed to write scripts. I often use a script to do all the gathering and munging, and call out to those scripts from my build file.

So, no excuses! Even those of you more inclined to the Windows way of life have easy access to bash (and other shells) via Cygwin. Get a taste and you won’t look back. There’s something quite gratifying about replacing an arduous, multi-step task with a script that you can run without breaking a sweat. You’ll never have to work again!

——–
Into continuous integration? Want to be? Try pulse.

Liked this post? Share it!

10 Responses to “Your Next Programming Language”

  1. May 9th, 2006 at 8:40 pm

    Dieter D'haeyere says:

    Any recommended starting points ? (examples for common tasks)

  2. May 9th, 2006 at 9:09 pm

    Jason says:

    Dieter,

    If you are really just starting out, there is a good set of tutorials at developerworks:

    http://www.ibm.com/developerworks/library/l-bash.html

    Mostly, I have just learnt by doing over time, you can start simple and discover things as you go. Also, bash scripting goes hand in hand with learning the other Unix tools, such as find, grep, sed, awk, cut, paste and so on. A bit of determination and a shell pipeline can get you a long way. Something I do frequently is applying a command to a bunch of files with a name that matches a certain pattern. You can do this with find -exec, for example:

    find . -name ‘*.txt’ -exec cat ‘{}’ \;

    or, to avoid the awkward -exec syntax, you can use a bash loop:

    find . -name ‘*.txt’ | while read filename; do cat $filename; done

    As I discover neat ways to do things in bash, I may post a few tips here. I have found that people always have new and different ways to accomplish things. Searching through newsgroup posts for unix shell scripting can be enlightening.

  3. May 9th, 2006 at 10:36 pm

    Orhan says:

    I agree. Nowadays I am trying to do the same thing with Groovy, since I am more familiar with Java.

  4. May 10th, 2006 at 3:10 am

    Keith Hill says:

    If you are on Windows you really should take a look at the new Windows PowerShell (aka Monad). PowerShell is an interactive shell that provides a C# like scripting language allowing access to the .NET Framework. You can write script at the prompt so it makes for a nice little .NET based REPL. You can download it from:

    http://www.microsoft.com/PowerShell

    And do stuff like this:

    # Same as find example in Jason’s post above
    > Get-ChildItem *.txt -rec | Get-Content

    # Get BIOS info
    > Get-WmiObject Win32_Bios

    SMBIOSBIOSVersion : A07
    Manufacturer : Dell Computer Corporation
    Name : Phoenix ROM BIOS PLUS Version 1.10 A07
    SerialNumber : 60X4W22
    Version : DELL – 8

    # Get stats on my music collection
    > get-childitem “$home\Music” -rec |
    >> where {!$_.PsIsContainer -and $_.extension -match “mp3|wma”} |
    >> measure-object -property length -sum -min -max -ave
    >>

    Count : 1018
    Average : 2991362.43123772
    Sum : 3045206955
    Max : 102750941
    Min : 89025
    Property : Length

    # Find all files/dirs that have been written to in the past week
    # Note: gci is an alias for Get-ChildItem and ? is an alias for where
    > $lastWeek = (get-date).AddDays(-7)
    > gci “$home\Documents” -rec | ? {$_.LastWriteTime -gt $lastWeek}

    # Tweak the registry
    > cd ‘HKCU:\Software\Microsoft\Command Processor’
    > set-itemproperty . -name CompletionChar -value 9

  5. May 10th, 2006 at 4:01 am

    Paul Brown says:

    Lambda the Ultimate (a.k.a., LtU) (http://lambda-the-ultimate.org/) is a great place (if not THE place) to go next language shopping.

  6. May 10th, 2006 at 10:27 pm

    David Taylor says:

    Sankey,

    If you do have a few minutes, google and download Microsoft PowerShell RC1 (for Windows XP or Server 2003). It is an interesting bridge of functional programming, with a shell and scripting language based on stringly typed objects instead of text. I have heard about this (under the old Monad name) since MS started the project 4-5 years ago, but ignored it even at the PDC in LA last year which I attented (did not go to any Monad sessions). But finally over since Release Candidate 1 came out last week I have been learning it and am completely impressed.

    >>find . -name ‘*.txt’ -exec cat ‘{}’ \;

    # Same as find example in Jason’s post above
    > Get-ChildItem *.txt -rec | Get-Content

    # Note that PowerShell provides Alias for the above so you can just type:
    > dir *.txt -r | type

    This is pasically saying, call the directory command (get-childitem) and pipe the output into the type command (get-content).

    Coolect thing is you can do things like:
    dir *.txt -r | sort Length | select -first 5 Name, Length

    Now cool is that!

    BTW I work for Melbourne IT, so it is nice finding another Aussie blog – but I am working from our London office at the moment.

  7. May 10th, 2006 at 10:42 pm

    timvw says:

    In my experience there is no good definition for ‘scripting language’ and thus i find it ridiculous to talk about them…

    I do agree with the content of the article as in that there will always be room for automation and that some tools are better suited than others for that taks…

  8. May 18th, 2006 at 5:53 am

    NZ says:

    JScript on Microsoft
    Why reinvent or reinstall the wheel ?

    You can use JScript fo do a lot of stuff.
    And you do not have to download or intall anything.
    It works as a batch or shell script (you can even use VB if you prefer buy JS is more powerful)

    Drag files over (to do something on them)

    var text=’ Selected files:’;
    var strArgument;
    for (var i=0;i

  9. May 19th, 2006 at 4:30 pm

    Peter Butler says:

    find . -name ‘*.txt’ | while read filename; do cat $filename; done

    Argh! Do this:

    find -name ‘*.txt’ | xargs -n1 cat

    xargs is fan-fuckin-tastic.

  10. July 10th, 2006 at 1:58 pm

    Technical Related Notes » Blog Archive » links for 2006-07-07 says:

    [...] a little madness » Blog Archive » Your Next Programming Language (tags: shell unix) [...]

Leave a Reply