• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/bin/sh
2#
3# 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.
8#
9# Try running these:
10# $ ./debug_output.sh speak
11# $ ./debug_output.sh sing
12# $ ./debug_output.sh --debug sing
13
14# source shflags
15. ../src/shflags
16
17# define flags
18DEFINE_boolean 'debug' false 'enable debug mode' 'd'
19FLAGS_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
38
39# parse the command-line
40FLAGS "$@" || exit 1
41eval set -- "${FLAGS_ARGV}"
42
43command=$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
59