-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
Copy pathfunctions.bash
141 lines (126 loc) · 4.22 KB
/
functions.bash
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#!/usr/bin/env bash
# __ _ _
# / _|_ _ _ __ ___| |_(_) ___ _ __ ___
# | |_| | | | '_ \ / __| __| |/ _ \| '_ \/ __|
# | _| |_| | | | | (__| |_| | (_) | | | \__ \
# |_| \__,_|_| |_|\___|\__|_|\___/|_| |_|___/
#
# Shell function examples and boilerplate.
#
# The functions here are intended to be included in the interactive shell,
# which is done by defining them in a shell init file like `~/.bashrc`.
#
# Bash Boilerplate: https://github.com/xwmx/bash-boilerplate
#
# Copyright (c) 2016 William Melody • [email protected]
###############################################################################
# Simple shell function with help / usage.
#
# This function provides an example of a simple shell function with help /
# usage information that is displayed with either the `-h` or `--help` flag.
###############################################################################
hello() {
if [ "${1}" = "-h" ] || [ "${1}" = "--help" ]
then
cat <<HEREDOC
Usage:
hello
hello -h | --help
Options:
-h --help Display this usage information.
Description:
Say hello.
HEREDOC
return 0
fi
printf "Hello.\\n"
}
###############################################################################
# Simple shell function with help / usage and option flags.
#
# This function provides an example of a simple shell function with help /
# usage information that is displayed with either the `-h` or `--help` flag.
# This example also demonstrates how to include an option flag to provide
# optional behavior. Only one option can be used at a time and it is expected
# to be in the first position.
###############################################################################
hi() {
if [ "${1}" = "-h" ] || [ "${1}" = "--help" ]
then
cat <<HEREDOC
Usage:
hi
hi --all
hi -h | --help
Options:
--all Say "hi" to everyone.
-h --help Display this usage information.
Description:
Say hi.
HEREDOC
elif [ "${1}" = "--all" ]
then
printf "Hi, everyone!\\n"
else
printf "Hi!\\n"
fi
}
###############################################################################
# Simple wrapper with help / usage and option flags.
#
# This wrapper function provides an example of a simple wrapper for an
# existing command, adding help / usage information that is displayed with
# either the `-h` or `--help` flag. This example also demonstrates how to
# include an option flag to provide optional behavior. Only one option can be
# used at a time and it is expected to be in the first position.
#
# Wrapper functions are useful for adding functionality and options to an
# existing command, or even simply adding usage information to a command that
# doesn't otherwise provide it.
###############################################################################
# Save the existing `yes` exectuable path to a variable so it can be used
# after the name `yes` is redefined.
_YES_COMMAND="$(which yes)"
yes() {
if [ "${1}" = "-h" ] || [ "${1}" = "--help" ]
then
cat <<HEREDOC
Usage:
yes [<expletive>]
yes --quiet
yes -h | --help
Options:
--quiet Suppress output.
-h --help Display this usage information.
Description:
A wrapper for \`yes\`, which outputs <expletive> or, by default, 'y' forever.
For more information, run \`man yes\`.
HEREDOC
elif [ "${1}" = "--quiet" ]
then
"${_YES_COMMAND}" "${@}" 1> /dev/null
else
"${_YES_COMMAND}" "${@}"
fi
}
###############################################################################
# Anonymous Function / Immediately-invoked function expression (IIFE)
#
# A simple anonymous throway function pattern that leverages common conventions
# from JavaScript and other languages. An underscore (`_`) function name
# indicates an anonymous function, which is then immediately invoked after
# the definition.
#
# Anonymous functions can be useful for shell initialization functions that are
# intended to be executed only once.
#
# Source (credit to https://stackoverflow.com/users/10559/jwfearn):
# https://stackoverflow.com/a/24538676
#
# More Information:
# https://en.wikipedia.org/wiki/Immediately-invoked_function_expression
###############################################################################
_() {
printf "Hello World.\\n"
} && _ "$@"
unset -f _