Foil 06 - Utility subroutine to output error messages

 1 #*********************************************************************
 2 # Subroutime to output error messages to STDERR.  Quoted strings     *
 3 # are passed on the command and output as separate lines.            *
 4 #                                                                    *
 5 # The bell or other signal is activiated first.  1st line preceeded  *
 6 # with the program name, others are not.  The ^G is actually Cntl-G. *
 7 #*********************************************************************
 8 emsg()
 9 {
10 
11 echo "^G\c" 1>&2 # Ring the bell if output is to a terminal, no NL
12 
13 if [ $# -gt 0 ]
14   then
15     echo "$progname: $1" 1>&2
16     shift 1
17     while [ $# -gt 0 ]
18       do
19         echo "$1" 1>&2
20         shift 1
21       done
22   fi
23 
24 } # End of emsg subroutine                                         #06

This is a sample of standard building blocks than can be used in more complex applications. They can be written as separate commands, but are used as embeded subroutines here so they can't be lost when someone copies the command and forgets the support library.

  • Lines 11, 15, and 19 redirect their output to standard error.
  • Line 11 uses Control-G to alert the user and uses \c to prevent outputing a blank line.
  • Lines 13 and 17 check the number of operands remaining on the command line (in this case the subroutine call).
  • Lines 16 and 20 cycle through the operands.

    The progname variable would be set elsewhere with something like progname=`/bin/basename "$0"`

    Previous   Next   Index