Guidelines on integrating complex unix command lines into programs
Needless 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
- breaking the long command into a well structured sequence of commands and storing them in a separate shell script
- keeping the information/debug messages to stdout, error messages to stderr
- making sure to exit with as many different values as possible to signal various stages of errors
- 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