-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinput-validation.lib.sh
executable file
·85 lines (73 loc) · 2.63 KB
/
input-validation.lib.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
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
################################################################################
# input-validation.lib.sh - Bash library functions related to input validation
################################################################################
#
# Copyright (C) 2015 stepping stone GmbH
# Bern, Switzerland
# http://www.stepping-stone.ch
#
# Authors:
# Christian Affolter <[email protected]>
#
# Licensed under the EUPL, Version 1.1.
#
# You may not use this work except in compliance with the
# Licence.
# You may obtain a copy of the Licence at:
#
# http://www.osor.eu/eupl
#
# Unless required by applicable law or agreed to in
# writing, software distributed under the Licence is
# distributed on an "AS IS" basis,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
# express or implied.
# See the Licence for the specific language governing
# permissions and limitations under the Licence.
#
################################################################################
# The path to the lib directory.
# The default value only works if not sourced or executed from within $PATH
LIB_DIR=${LIB_DIR:="$(readlink -f ${0%/*})"}
source "${LIB_DIR}/input-output.lib.sh"
source "${LIB_DIR}/validation.lib.sh"
# Reads user input and validates it agains a given regular expression.
#
# See ioReadInputVar and validationRegex
#
# inputValidationRegex \
# <VARIABLE-NAME> <PROMPT-PREFIX> <REGEX> [<ERROR-MESSAGE> [<DEFAULT-VALUE>]]
function inputValidationRegex() {
local varName="${1}"
local inputPrompt="${2}"
local regex="${3}"
local errorMessage="${4:-"${inputPrompt} is invalid"}"
local defaultValue="${5}"
# Read input until the regex matches.
while test -z "${!varName}"; do
ioReadInputVar "${varName}" "${inputPrompt}" "${defaultValue}"
if ! validationRegex "${!varName}" "${regex}" "${errorMessage}"; then
unset $varName
fi
done
}
# Reads user input and checks if the user has entered yes or no
#
# Returns 0 on yes input an 1 on no input
#
# inputValidationYes [<PROMPT-PREFIX> [<ERROR-MESSAGE> [<DEFAULT-VALUE>]]]
function inputValidationYes() {
local inputPrompt="${1:-"yes or no?"}"
local errorMessage="${2:-"Please enter yes or no"}"
local defaultValue="${3}"
local yesRegex='(yes|YES|Yes|y|Y)'
local noRegex='(no|NO|No|n|N)'
local regex="^(${yesRegex}|${noRegex})$"
local input=""
inputValidationRegex \
"input" "${inputPrompt}" "${regex}" "${errorMessage}" "${defaultValue}"
validationRegex "${input}" "${yesRegex}"
return $?
}