• 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. ../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
25debug() {
26  [ ${FLAGS_debug} -eq ${FLAGS_TRUE} ] || return
27  echo "DEBUG: $@" >&2
28}
29
30die() { [ $# -gt 0 ] && echo "error: $@" >&2
31  flags_help
32  exit 1
33}
34
35# Parse the command-line.
36FLAGS "$@" || exit 1
37eval set -- "${FLAGS_ARGV}"
38
39command=$1
40case ${command} in
41  '') die ;;
42
43  speak)
44    debug "I'm getting ready to say something..."
45    echo 'The answer to the question "What is the meaning of life?" is "42".'
46    ;;
47
48  sing)
49    debug "I'm getting ready to sing something..."
50    echo 'I love to sing! La diddy da dum!'
51    ;;
52
53  *) die "unrecognized command (${command})" ;;
54esac
55