kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 1 | #!/bin/sh |
kate.ward | 120b504 | 2008-06-27 08:58:03 +0000 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2008 Kate Ward. All Rights Reserved. |
| 4 | # Released under the LGPL (GNU Lesser General Public License) |
| 5 | # |
| 6 | # Author: kate.ward@forestent.com (Kate Ward) |
| 7 | # |
| 8 | # This script does the very simple job of echoing some text. If a '-d' (or |
| 9 | # '--debug') flag is given, additinal "debug" output is enabled. |
| 10 | # |
| 11 | # This script demonstrates the use of a boolean flag to enable custom |
| 12 | # functionality in a script. |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 13 | # |
| 14 | # Try running these: |
| 15 | # $ ./debug_output.sh speak |
| 16 | # $ ./debug_output.sh sing |
| 17 | # $ ./debug_output.sh --debug sing |
kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 18 | |
| 19 | # source shflags |
| 20 | . ../src/shflags |
| 21 | |
kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 22 | # define flags |
| 23 | DEFINE_boolean 'debug' false 'enable debug mode' 'd' |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 24 | FLAGS_HELP=`cat <<EOF |
| 25 | commands: |
| 26 | speak: say something |
| 27 | sing: sing something |
| 28 | EOF` |
| 29 | |
| 30 | |
| 31 | debug() |
| 32 | { |
| 33 | [ ${FLAGS_debug} -eq ${FLAGS_TRUE} ] || return |
| 34 | echo "DEBUG: $@" >&2 |
| 35 | } |
| 36 | |
| 37 | die() { |
| 38 | [ $# -gt 0 ] && echo "error: $@" >&2 |
| 39 | flags_help |
| 40 | exit 1 |
| 41 | } |
| 42 | |
kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 43 | |
| 44 | # parse the command-line |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 45 | FLAGS "$@" || exit 1 |
| 46 | eval set -- "${FLAGS_ARGV}" |
kate.ward | 695ddde | 2008-06-26 22:22:26 +0000 | [diff] [blame] | 47 | |
kate.ward | c521068 | 2009-03-30 18:54:36 +0000 | [diff] [blame] | 48 | command=$1 |
| 49 | case ${command} in |
| 50 | '') die ;; |
| 51 | |
| 52 | speak) |
| 53 | debug "I'm getting ready to say something..." |
| 54 | echo 'The answer to the question "What is the meaning of life?" is "42".' |
| 55 | ;; |
| 56 | |
| 57 | sing) |
| 58 | debug "I'm getting ready to sing something..." |
| 59 | echo 'I love to sing! La diddy da dum!' |
| 60 | ;; |
| 61 | |
| 62 | *) die "unrecognized command (${command})" ;; |
| 63 | esac |