blob: b3ecec27afc98f716c46f00efca80a78fb2e940a [file] [log] [blame]
kate.ward695ddde2008-06-26 22:22:26 +00001#!/bin/sh
kate.ward120b5042008-06-27 08:58:03 +00002#
kate.ward120b5042008-06-27 08:58:03 +00003# This script does the very simple job of echoing some text. If a '-d' (or
4# '--debug') flag is given, additinal "debug" output is enabled.
5#
6# This script demonstrates the use of a boolean flag to enable custom
7# functionality in a script.
kate.wardc5210682009-03-30 18:54:36 +00008#
9# Try running these:
10# $ ./debug_output.sh speak
11# $ ./debug_output.sh sing
12# $ ./debug_output.sh --debug sing
kate.ward695ddde2008-06-26 22:22:26 +000013
14# source shflags
15. ../src/shflags
16
kate.ward695ddde2008-06-26 22:22:26 +000017# define flags
18DEFINE_boolean 'debug' false 'enable debug mode' 'd'
kate.wardc5210682009-03-30 18:54:36 +000019FLAGS_HELP=`cat <<EOF
20commands:
21 speak: say something
22 sing: sing something
23EOF`
24
25
26debug()
27{
28 [ ${FLAGS_debug} -eq ${FLAGS_TRUE} ] || return
29 echo "DEBUG: $@" >&2
30}
31
32die() {
33 [ $# -gt 0 ] && echo "error: $@" >&2
34 flags_help
35 exit 1
36}
37
kate.ward695ddde2008-06-26 22:22:26 +000038
39# parse the command-line
kate.wardc5210682009-03-30 18:54:36 +000040FLAGS "$@" || exit 1
41eval set -- "${FLAGS_ARGV}"
kate.ward695ddde2008-06-26 22:22:26 +000042
kate.wardc5210682009-03-30 18:54:36 +000043command=$1
44case ${command} in
45 '') die ;;
46
47 speak)
48 debug "I'm getting ready to say something..."
49 echo 'The answer to the question "What is the meaning of life?" is "42".'
50 ;;
51
52 sing)
53 debug "I'm getting ready to sing something..."
54 echo 'I love to sing! La diddy da dum!'
55 ;;
56
57 *) die "unrecognized command (${command})" ;;
58esac