-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshellFunctionSetup.sh
More file actions
executable file
·114 lines (110 loc) · 4.59 KB
/
Copy pathshellFunctionSetup.sh
File metadata and controls
executable file
·114 lines (110 loc) · 4.59 KB
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
SHELL_FUNCTION_SETUP_DOC=$(
cat <<"EOF"
#/ DESCRIPTION:
#/ The purpose of this file is to set up the calling shell by:
#/ - Exporting environment variable(s) used by shell function(s).
#/ - Sourcing all shell function(s).
#/ - Creating alias(es) for shell script(s).
#/
#/ USAGE: eval "$(shellFunctionSetup.sh)"
#/
#/ NOTE(S):
#/ - An executed script runs in a child process, which can't change
#/ the shell that started it. So, when executed, this file only
#/ prints shell code. The caller's `eval` then runs that code in
#/ the caller's own shell.
#/ - The printed code exports `SHELL_FUNCTIONS` (the directory this
#/ file is in, found via `$0`) and then sources this file, which
#/ runs the "Sourced: Set Up the Calling Shell" section.
#/ - File(s) sourced from that section also run in the calling shell,
#/ so they can use `.`, `export`, and `alias` directly.
#/ - Sourcing this file directly isn't supported, as POSIX sh gives a
#/ sourced file no way to learn its own path.
#/ - On error, a message is printed to stderr and nothing is printed
#/ to stdout, so the caller's `eval` does nothing. No failing
#/ return code is returned so that a failure can't cause a user's
#/ shell configuration to exit before the user knows why.
#/
#/ SPECIAL OPTION(S):
#/ None.
#/
#/ ARGUMENT(S):
#/ None.
#/
#/ OPTION(S):
#/ None.
#/
#/ RETURN CODE(S):
#/ - 0: Always returned.
#/
#/ EXAMPLE(S):
#/ eval "$(shellFunctionSetup.sh)"
#/
#/ AUTHOR(S):
#/ - Reed Clanton
#/
#/ TODO(S):
#/ - Test.
EOF
)
##################################################
## Executed: Print Code for the Caller to `eval` ##
##################################################
# This section runs in a child process, so all it can do is print code. That
# code exports `SHELL_FUNCTIONS` and sources this file with
# `_SOURCE_FUNCTIONS_SOURCING` set, which skips this section.
#
# Each exit point uses `return 0 2>/dev/null || exit 0` so that a mistaken
# `. shellFunctionSetup.sh` or `source shellFunctionSetup.sh` returns rather
# than closing the user's shell.
if [ "${_SOURCE_FUNCTIONS_SOURCING-}" != 1 ]; then
## Determine Current Directory ##
# `$0` names this file only when it's executed, and the name has to match
# the file sourced by the printed code.
case "${0##*/}" in
shellFunctionSetup.sh) ;;
*)
echo 'shellFunctionSetup.sh: Execute, do not source. Usage: eval "$(/path/to/shellFunctionSetup.sh)"' >&2
return 0 2>/dev/null || exit 0
;;
esac
currDir=$(CDPATH= cd -P -- "$(dirname -- "$0")" && pwd -P) || {
echo 'shellFunctionSetup.sh: Cannot determine the directory this file is in.' >&2
return 0 2>/dev/null || exit 0
}
## Print Code ##
# Single-quote the directory so the caller reads it back exactly. Each `'`
# inside it becomes `'\''` (end quote, escaped quote, start quote).
quotedCurrDir=$(printf '%s\n' "$currDir" | sed "s/'/'\\\\''/g")
printf '%s\n' \
"export SHELL_FUNCTIONS='$quotedCurrDir'" \
'_SOURCE_FUNCTIONS_SOURCING=1' \
'. "$SHELL_FUNCTIONS/shellFunctionSetup.sh"'
return 0 2>/dev/null || exit 0
fi
#######################################
## Sourced: Set Up the Calling Shell ##
#######################################
# Everything from here on, including the file(s) sourced below, runs in the
# calling shell, so plain `export`, `.`, and `alias` affect it directly.
unset _SOURCE_FUNCTIONS_SOURCING
## Data ##
export VERIFY_INPUT_PROVIDED_SHELL_FUNCTION_HOME="$SHELL_FUNCTIONS/util/verifyInputProvided"
export OUTPUT_SHELL_FUNCTION_HOME="$SHELL_FUNCTIONS/output"
export LOGGING_SHELL_FUNCTION_HOME="$SHELL_FUNCTIONS/logging"
export SHELL_NAME_SHELL_FUNCTION_HOME="$SHELL_FUNCTIONS/util/shellName"
export BACK_UP_SHELL_FUNCTION_HOME="$SHELL_FUNCTIONS/backUp"
## Function(s) ##
# Include shell option checking function used by other function(s) in the same/child directories.
# Note: If location/name of this file changes, update all other *.sh files in the same directory.
. "$VERIFY_INPUT_PROVIDED_SHELL_FUNCTION_HOME/verifyInputProvided.sh"
# Include output producing/formatting function(s) used by other function(s) in the same/child directories.
. "$OUTPUT_SHELL_FUNCTION_HOME/output.sh"
# Include logging function used by other function(s) in the same/child directories.
. "$LOGGING_SHELL_FUNCTION_HOME/logging.sh"
# Alias(es) are single-quoted so the variable expands, quoted, each time the
# alias is used. That keeps path(s) with spaces or quotes intact.
# Include function for returning name of current shell.
alias shellName='"$SHELL_NAME_SHELL_FUNCTION_HOME/shellName.sh"'
# Include function that backs up local home directory to "external" drive.
alias backUp='"$BACK_UP_SHELL_FUNCTION_HOME/backUp.sh"'