format.sh (1231B)
1 #!/bin/sh 2 ## 3 # Usage: format.sh TEMPLATE [SELEXP] 4 # 5 # This runs recfmt w/ template file TEMPLATE, taking input from stdin, 6 # and writing output to stdout. 7 # 8 # Optional arg SELEXP is an expression passed to ‘recsel -e’. If specified, 9 # stdin is first processed by recsel and its output is then piped to recfmt 10 # for formatting. If recsel exits failurefully (e.g., given invalid SELEXP), 11 # no output is written and format.sh exits failurefully as well. 12 ## 13 me=$(basename $0) 14 15 version='1.4' 16 # 1.4 -- create $TMPDIR if it does not exist 17 # 1.3 -- add support for optional arg SELEXP 18 # 1.2 -- add check for required arg TEMPLATE 19 # 1.1 -- add --help/--version support 20 # 1.0 -- initial release 21 22 if [ x"$1" = x--help ] ; then 23 sed '/^##/,/^##/!d;/^##/d;s/^# //g;s/^#$//g' $0 24 exit 0 25 fi 26 27 if [ x"$1" = x--version ] ; then 28 echo $me '(gana)' $version 29 exit 0 30 fi 31 32 if [ x"$1" = x ] ; then 33 echo >&2 "$me: ERROR: missing arg TEMPLATE (try --help)" 34 exit 1 35 fi 36 37 template="$1" 38 39 if [ x"$2" = x ] ; then : ; else 40 selexp="$2" 41 fi 42 43 if [ "$selexp" ] ; then 44 t=$(mktemp) 45 trap "rm -f $t" EXIT 46 recsel -e "$selexp" > $t && 47 recfmt -f "$template" < $t 48 else 49 exec recfmt -f "$template" 50 fi 51 52 # format.sh ends here