Categories
Uncategorized

Shell Scripting

I kind of like shell scripting. It’s quirky, frequently ugly, but it’s damned useful. Like Perl, it annoys me when I see things which could be written in a better manner. One persistent example is abusing the if statement.

  if [ "x$machine" = "x" ]
  then
          echo "not in ~/.machines adding"
          echo $1 >> ~/.machines
          source ~/.bash_aliases
  fi

What’s the problem here? That little “x” in the test. It’s not necessary and hasn’t been since the late 80’s. There used to be a parsing bug in shells which meant that an empty argument got removed. So you couldn’t say [ "$machine" = "" ] because you’d end up comparing $machine to ].

But this got fixed, a long time ago. Really. Unless you’re working on an aging System III box, you shouldn’t worry about this, it’s just Cargo cult programming.

So the test can be just [ "$machine" = "" ]. That’s better. But not good enough. Testing for an empty string? The test command (aka [) provides a -z operator for that! So goes down to [ -z "$machine" ].

In this particular case, we’re explicitly targetting bash, though. One of the nice features of modern POSIX shells like bash is that they provide conditional expressions, which have different parsing rules. So you can say [[ -z $machine ]] and not worry about the lack of quotes.

Lastly, I should point out that I missed off the first line of that extract.

  machine=`grep $1 ~/.machines`

With that in place, you can see that we’re not actually using $machine anywhere in the function. It’s purely in place for the if statement. Which means you can simplify this even further. if takes a command as it’s argument. So why not just give it the grep command?

  if grep -q $1 ~/.machines
  then
    echo "not in ~/.machines adding"
    echo $1 >> ~/.machines
    source ~/.bash_aliases
  fi

I had to add the -q flag to shut grep up; we’re only interested in the return code, not the output.

Does this matter? Probably not much. But like all programming languages, it’s a lot easier to read when you use it idiomatically.

Categories
Uncategorized

Sussex Geek Dinner

I went to the Sussex Geek Dinner last night. Tristan Roddis was talking about Plone. I was initially very skeptical, as I had a bad Zope experience several years ago. I’m extremely glad to see that development has come on a long way. Whilst there’s still a very steep learning curve, you can now actually use source control with it, which was still not really done when I last looked (circa 1999). Anyway, it sounds really powerful when you get to grips with it, which Tristan’s company certainly have. I also pointed out Varnish to Tristan as he mentioned using squid in front of a CMS. 🙂

I also found out that he’s working in the same building on the floor above me. Which is really the point of events like these. It’s all about bringing the local tech community together so we get to know each other better. I was really pleased to bump into a few people I hadn’t seen in a while last night. We had a great talk after the main show—mainly about photography.

Just after 10, a few of us wandered over to the farm meet in another pub. I bumped into even more people there and had lots of deliciously geeky talk. It turned out that Sevan had just been interviewed on bsdtalk two hours previously about his work on the Brighton Chilli hotspot project. I was quite shocked to find that interview downloading to my ipod when I got back home. Good work Will Backman!

Categories
Uncategorized

Unicode in Rails

Unicode in Rails takes a step further today, as ActiveSupport::MultiByte is committed to the edge (r5223). More information is available over at fingertips, including a neat demo video. This should really help people who need proper Unicode support. There’s no excuse to not use UTF-8 now!