blob: d54635f168a97794fe6d1d1499b267a3b34d9785 [file] [log] [blame]
kate.ward695ddde2008-06-26 22:22:26 +00001#!/bin/sh
kate.ward120b5042008-06-27 08:58:03 +00002#
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.wardc5210682009-03-30 18:54:36 +000013#
14# Try running these:
15# $ ./debug_output.sh speak
16# $ ./debug_output.sh sing
17# $ ./debug_output.sh --debug sing
kate.ward695ddde2008-06-26 22:22:26 +000018
19# source shflags
20. ../src/shflags
21
kate.ward695ddde2008-06-26 22:22:26 +000022# define flags
23DEFINE_boolean 'debug' false 'enable debug mode' 'd'
kate.wardc5210682009-03-30 18:54:36 +000024FLAGS_HELP=`cat <<EOF
25commands:
26 speak: say something
27 sing: sing something
28EOF`
29
30
31debug()
32{
33 [ ${FLAGS_debug} -eq ${FLAGS_TRUE} ] || return
34 echo "DEBUG: $@" >&2
35}
36
37die() {
38 [ $# -gt 0 ] && echo "error: $@" >&2
39 flags_help
40 exit 1
41}
42
kate.ward695ddde2008-06-26 22:22:26 +000043
44# parse the command-line
kate.wardc5210682009-03-30 18:54:36 +000045FLAGS "$@" || exit 1
46eval set -- "${FLAGS_ARGV}"
kate.ward695ddde2008-06-26 22:22:26 +000047
kate.wardc5210682009-03-30 18:54:36 +000048command=$1
49case ${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})" ;;
63esac