-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathverbose-mode.sh
executable file
·76 lines (62 loc) · 3.24 KB
/
verbose-mode.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env bash
# -------------------------------------------------------------------------------- #
# Description #
# -------------------------------------------------------------------------------- #
# This is a simple script to display some text in the center of the screen. #
# -------------------------------------------------------------------------------- #
VERBOSE=false
# -------------------------------------------------------------------------------- #
# Set Verbose Mode #
# -------------------------------------------------------------------------------- #
# This is a simple function that will enable verbose mode based on a global #
# variable. It does this by cloning stdout and stderr and then redirecting the #
# original file descriptors to /dev/null. #
# -------------------------------------------------------------------------------- #
function set_verbose_mode()
{
exec 3>&1
exec 4>&2
if [[ "${VERBOSE}" = true ]]; then
echo "Verbose output enabled"
else
exec 1>/dev/null
exec 2>/dev/null
fi
}
# -------------------------------------------------------------------------------- #
# Output Wrapper #
# -------------------------------------------------------------------------------- #
# A wrapper to use instead of echo which will handle the forcing of messages to be #
# displayed even when verbose mode is off. #
# -------------------------------------------------------------------------------- #
function output()
{
if [[ -n $1 ]]; then
if [[ -n $2 ]] && [[ "${2}" = forced ]]; then
echo "$1" 1>&3 2>&4
else
echo "$1"
fi
fi
}
# -------------------------------------------------------------------------------- #
# Run the Tests #
# -------------------------------------------------------------------------------- #
# -------------------------------------------------------------------------------- #
function run_tests()
{
set_verbose_mode
output "I won't be visible when verbose=false"
output "I will be visible no matter what" forced
}
# -------------------------------------------------------------------------------- #
# Main() #
# -------------------------------------------------------------------------------- #
# This is the actual 'script' and the functions/sub routines are called in order. #
# -------------------------------------------------------------------------------- #
run_tests
# -------------------------------------------------------------------------------- #
# End of Script #
# -------------------------------------------------------------------------------- #
# This is the end - nothing more to see here. #
# -------------------------------------------------------------------------------- #