Recently in unixisms Category

More filtering

| | Comments (0) | TrackBacks (0)

Here's an easy way to turn a two argument bash script into a filter. It needs to be invoked like io_assignment IN $1 $2. The DEFAULT option is for setting what the default behavior is with only one argument. Sticking this into a library is probably recommended (hrm, this may be harder than it sounds as bash doesn't appear to respect variables set in a function in an included file.)

io_assignment () {
    DEFAULT="$1"
    shift
    case "$#" in
      0) IN_FILE="/dev/stdin"
         OUT_FILE="/dev/stdout"
         ;;
      1) case DEFAULT in
             IN) IN_FILE="$1"
                 OUT_FILE="/dev/stdout"
                 ;;
             OUT) IN_FILE="/dev/stdin"
                  OUT_FILE="$1"
                 ;;
         esac
         ;;
      2) IN_FILE="$1"
         OUT_FILE="$2"
         ;;
    esac
}

Also shell seems to be capable of allowing you to write code that needs a million error checks which I have not done.


Filters and Code Repetition

| | Comments (0) | TrackBacks (0)

Some notes for my own, and possibly your, reference:

If you write code that takes an input file and an output file on the command line, you should make it act as a filter taking either - as an input or output file, or just operating in the abscence of these command line arguments. (Obviously some things, I'm thinking cp here, are silly to have as a filter.)

Also if you're using a Makefile for something you shouldn't try to autogenerate whatever the output file name is in your make invoked code. Let make do this since it generally has to anyway to find a target file name.


About this Archive

This page is a archive of recent entries in the unixisms category.

meta is the previous category.

work is the next category.

Find recent content on the main index or look in the archives to find all content.

April 2009

Sun Mon Tue Wed Thu Fri Sat
      1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30    

September 2005: Monthly Archives

Pages

Powered by Movable Type 4.25