allbery@uunet.UU.NET (Brandon S. Allbery - comp.sources.misc) (08/07/89)
Posting-number: Volume 7, Issue 121 Submitted-by: frew%crseo@hub.ucsb.edu Archive-name: sherror Here are a couple of tools I use a LOT for writing shell scripts that will be around for a while. These tools let shell scripts generate standard usage and error messages just like real programs (should). A sample "production" script is included that demonstrates their usage. #! /bin/sh # This is a shell archive, meaning: # 1. Remove everything above the #! /bin/sh line. # 2. Save the resulting text in a file. # 3. Execute the file with /bin/sh (not csh) to create the files: # README # sherror # usage # xch # This archive created: Sun Aug 6 14:29:31 1989 # By: Jim Frew () export PATH; PATH=/bin:$PATH echo shar: extracting "'README'" '(615 characters)' if test -f 'README' then echo shar: will not over-write existing file "'README'" else sed 's/^X//' << \SHAR_EOF > 'README' X06-Aug-1989 X XHere are a couple of trivial utilities that make writing shell scripts Xa lot easier. X X"usage" writes a usage message to the standard error output. X X"sherror" writes an error message to the standard error output. If you Xmake a link to sherror named "shbug", then the message says "BUG" Xinstead of "ERROR". X XA simple script "xch" is included that demonstrates both usage and Xsherror. X X#-----------------+-----------------------+----------------------------- X# James Frew | frew@crseo.ucsb.edu | Computer Systems Lab., UCSB X# +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA SHAR_EOF if test 615 -ne "`wc -c < 'README'`" then echo shar: error transmitting "'README'" '(should have been 615 characters)' fi fi # end of overwriting check echo shar: extracting "'sherror'" '(659 characters)' if test -f 'sherror' then echo shar: will not over-write existing file "'sherror'" else sed 's/^X//' << \SHAR_EOF > 'sherror' X#!/bin/sh X X# sh{error,bug} -- print shell script {error,bug} message X Xexec 1>&2 X Xcase $0 in X*bug) severity=BUG X ;; X*) severity=ERROR X ;; Xesac X Xpgm=`basename ${1:-command?}` Xmsg="${2:-message?}" Xfile=${3:-file?} X Xcase $# in X3) if [ -d "$file" ]; then X type=Directory X else X type=File X fi X echo " X$pgm: $severity: X $type "\"$file\"": $msg X" X ;; X*) echo " X$pgm: $severity: X $msg X" X ;; Xesac X Xexit 1 X X# 06-Aug-1989: placed in the public domain X#-----------------+-----------------------+----------------------------- X# James Frew | frew@crseo.ucsb.edu | Computer Systems Lab., UCSB X# +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA SHAR_EOF if test 659 -ne "`wc -c < 'sherror'`" then echo shar: error transmitting "'sherror'" '(should have been 659 characters)' fi chmod +x 'sherror' fi # end of overwriting check echo shar: extracting "'usage'" '(515 characters)' if test -f 'usage' then echo shar: will not over-write existing file "'usage'" else sed 's/^X//' << \SHAR_EOF > 'usage' X#!/bin/sh X X# usage -- standard usage message for shell scripts X Xexec 1>&2 X Xpgm=`basename ${1:-command?}` Xargs="${2-args?}" # note "-" not ":-", in case no args Xdescription="${3:-description?}" X Xecho " X$pgm -- $description X XUsage: $pgm $args X" X Xexit 1 X X# 06-Aug-1989: placed in the public domain X#-----------------+-----------------------+----------------------------- X# James Frew | frew@crseo.ucsb.edu | Computer Systems Lab., UCSB X# +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA SHAR_EOF if test 515 -ne "`wc -c < 'usage'`" then echo shar: error transmitting "'usage'" '(should have been 515 characters)' fi chmod +x 'usage' fi # end of overwriting check echo shar: extracting "'xch'" '(802 characters)' if test -f 'xch' then echo shar: will not over-write existing file "'xch'" else sed 's/^X//' << \SHAR_EOF > 'xch' X#!/bin/sh X X[ $# = 2 ] || exec usage $0 'name1 name2' 'exchange files or directories' X Xfor check do X [ -d $check -o -f $check ] || X exec sherror $0 'No such file or directory' $check X checkdir=`dirname $check` X [ -w $checkdir ] || exec sherror $0 'No write permission' $checkdir Xdone X Xtmp=,`basename $0`$$ X[ -d $tmp -o -f $tmp ] && exec sherror $0 'temporary name already exists' $tmp X[ -w . ] || exec sherror $0 'No write permission (for temporary name)' . X Xset -x X Xmv $1 $tmp || exit 1 Xmv $2 $1 || exit 1 Xmv $tmp $2 || exit 1 X Xrm -f $tmp X X# 06-Aug-1989: placed in the public domain X#-----------------+-----------------------+----------------------------- X# James Frew | frew@crseo.ucsb.edu | Computer Systems Lab., UCSB X# +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA SHAR_EOF if test 802 -ne "`wc -c < 'xch'`" then echo shar: error transmitting "'xch'" '(should have been 802 characters)' fi chmod +x 'xch' fi # end of overwriting check # End of shell archive exit 0 #-----------------+-----------------------+----------------------------- # James Frew | frew@crseo.ucsb.edu | Computer Systems Lab., UCSB # +1 805 961 8413 | frew@ucsbuxa (BITNET) | Santa Barbara, CA 93106, USA