Skip to content

February 3, 2009

Guidelines on integrating complex unix command lines into programs

by Joe Kuan

unixNeedless to say, the power of combining unix commands together can break any complicated tasks into challenging tricks. These commands are a great help in programming to offer great short cuts. Less code means less bugs. However, I have seen some code embedded with complex unix command lines used slightly to the extreme.

That would be fine if that program is maintained solely by the author or a team of equal strength unix developers. Otherwise, there are drawbacks:

  • If problems arise in the middle of the complex command lines, it is difficult to interpret into meaningful error messages to the end users
  • Less obvious to the less experienced maintainers; it takes them much longer time to understand & fix the problem. They may even rewrite the whole thing out of frustration.

In general, I would keep the embedded unix command lines to no more than a sequence of 3 commands.

If it has to be a very complex command line, decouple it by

  1. breaking the long command into a well structured sequence of commands and storing them in a separate shell script
  2. keeping the information/debug messages to stdout, error messages to stderr
  3. making sure to exit with as many different values as possible to signal various stages of errors
  4. calling the script with popen (or anything similar) and pclose to get the exit status

This gives the benefits of :

  • extensible, easy to maintain
  • commenting the difficult parts more directly
  • providing unit tests for the script

I work for iTrinegy and here are my other technical blogs

Advertisements
Read more from Unix

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Note: HTML is allowed. Your email address will never be published.

Subscribe to comments

%d bloggers like this: