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.


0 TrackBacks

Listed below are links to blogs that reference this entry: More filtering.

TrackBack URL for this entry: http://blog.sf.dasbistro.com/mt-tb.cgi/13

Leave a comment

About this Entry

This page contains a single entry by Sam Phillips published on September 12, 2005 3:22 PM.

Filters and Code Repetition was the previous entry in this blog.

Engaged is the next entry in this blog.

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

February 2008

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  

Pages

Powered by Movable Type 4.1