Tutorial :How to code in the unix spirit? (small single task tools)


I have written a little script which retrieves pictures and movies from my camera and renames them based on their date and then copies them on my harddrive, managing conflicts automatically (same name? same size? same md5?)
Works pretty well.

But this is ONE script.
From time to time I need to check if a picture is already hidden somewhere in a volume, so I'd like to apply the "conflict manager" only. I guess if I had properly followed the unix spirit of tiny single-task tools, I could do that.

What are the best resources, best practices and your experience on the subject?

Thank you.

Edit : Although I'd love to read unix books and have a deep understanding of the subject, I am looking for the Great Principles first. Plus I tend to limit myself to online resources.


I've found that most code doesn't start out being reusable, it evolves to be. Take your existing code and factor out the "conflict manager" portion into its own function or program, then call that program instead of having it be a part of your original application. After that you'll be able to reuse that part of your code that you have a need to reuse. Sometimes it's impossible to design software up front for reusability because you simply don't know which parts you'll want to reuse.

As for resources, it seems like the store shelves are packed with books for Linux desktop users and system administrators, but it's hard to find good Linux programming books. A few good ones:

Lastly, Eric Raymond has made The Art of Unix Programming available online for free.


I would look at the book called The Art of Unix Programming.


Check out this book:

The Art of Unix Programming by Eric S. Raymond


Here is his website: http://www.catb.org/~esr/writings/taoup/


Personally, whenever I see that the script I'm planning to write will be longer than a dozen lines, I use python instead of shell script. One neat trick with python scripting is that it's very easy to program in a style where you create both "unix spirit" command-line tools and libraries. E.g. for your "conflict manager", create a file (python module) and put the functionality in functions and/or classes, and then at the end you can put a python "main" function (the usual if __name__=='__main__': dance) where you parse command line options (use the builtin OptionParser module for this, it's very nice!) and use the functionality in the functions/classes.

This way you can use the utility both as a stand-alone command line program, or you can import the module in another python script and use the functionality defined there via functions/classes rather than parsing input.


Start with wikipedia (Dataflow programming)


The book Software Tools (amazon) by Kernighan and Plauger is a classic on this subject. I think it should be required reading for any serious student of software development.


- the art of UNIX programming - is quite a nice book ok "the unix way", in so far as one exists. OTOH if the way is "do as little work as gets your job done", you may already be there. :)


I think some of the keys for good gnu code, are:

  • Handling the system signals properly, like deattaching hard drive files if SIGTERM is received.
  • Proper use of pipes and standard input/output
  • Follwing common command line flag rules

I would also recommend this book. Pretty old, but I think is quite clear explaining the principles of unix.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Next Post »