-
Notifications
You must be signed in to change notification settings - Fork 0
/
prompt.bash
154 lines (115 loc) · 3.67 KB
/
prompt.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
142
143
144
145
146
147
148
149
150
151
152
153
154
#!/usr/bin/env bash
: <<'!COMMENT'
GGCOM - Bash - Library - Prompts v201507100702
Louis T. Getterman IV (@LTGIV)
www.GotGetLLC.com | www.opensour.cc/ggcom/bash/lib/prompt
Example usage:
source "prompt.bash"
pauseret
Thanks:
HowTo: Add Pause Prompt In a Shell Script ( bash pause command )
http://www.cyberciti.biz/tips/linux-unix-pause-command.html
Solution by mcoolive
http://stackoverflow.com/questions/12022592/how-can-i-use-long-options-with-the-bash-getopts-builtin
How to read command line arguments in a bash script - a great resource for How To's from Wikia
http://how-to.wikia.com/wiki/How_to_read_command_line_arguments_in_a_bash_script
bash how to pass array as an argument to a function - Stack Overflow
http://stackoverflow.com/questions/16461656/bash-how-to-pass-array-as-an-argument-to-a-function
!COMMENT
function pause(){
read -p "$*";
} # END FUNCTION
function pauseret() {
pause 'Press [Enter] key to continue...';
} # END FUNCTION
function qa_with_def() {
#----- Variables
local question="$1"
local defAnswer="$2"
local inpAnswer=''
local numChar=${3-''}
local timeout=${4-0}
#-----/Variables
if [[ "$numChar" =~ ^[0-9]+$ ]]; then
read -n $numChar -p "$question [$defAnswer] : " inpAnswer
else
read -p "$question [$defAnswer] : " inpAnswer
fi
if [ -z "$inpAnswer" ]; then
inpAnswer="$defAnswer"
fi
echo "$inpAnswer"
} # END FUNCTION: qa_with_def
function notifyalert() {
# Notification
#----- Variables
local notifyapp="$1"
local notifyargs="$2"
local title="$3"
local message="$4"
#-----/Variables
if [ ! -z "$notifyapp" ]; then
eval $notifyapp "$( \
str_replace "\%title\%" "$title" "$( \
str_replace "\%message\%" "$message" "$notifyargs"
)"
)"
fi
} # END FUNCTION: notifyalert
function parseArgs() {
# $# - Number of arguments
# $@ / $* - All arguments
#----- Variables
local rawArgs=("$@")
((last_idx=${#rawArgs[@]} - 1))
local rawTrgt=${rawArgs[last_idx]}
local rawTrgts=${#rawTrgt}
unset rawArgs[last_idx]
rawArgsSize=${#rawArgs}
local boolFlag=False # Flag?
local boolValues=False # Values?
local boolBlobMatch=False # Beginning segment matches?
local value='' # Return
#-----/Variables
# Iterate through all arguments, and find the target
for i in "${rawArgs[@]}"; do
s=${#i} # Size of entire argument (if applicable, with values)
offset=$(( $s - $rawTrgts )) # Offset of target size from current argument size
blob="${i:0:$rawTrgts}" # Potential match (would also match --targetTest if searching for --target)
trail="${i:$rawTrgts}" # Trailing Text after target size
value="${i:$(( rawTrgts+1 ))}" # X
# Is this a boolean flag that we're searching for? If offset is zero, then it is, and our return value is True.
if [ $offset -eq 0 ] && [ "$i" = "$rawTrgt" ]; then
boolFlag=True
value=True
fi
# Blob match?
[[ "$blob" = "$rawTrgt" ]] && boolBlobMatch=True || boolBlobMatch=False
# Do we have values? If character is '=', then it is.
if [ "$boolBlobMatch" = True ] && [ "${trail:0:1}" = '=' ]; then
boolValues=True
else
boolValues=False
fi
# No values are available in this iteration.
if [ "$boolFlag" = False ] && [ "$boolValues" = False ]; then value=''; fi
: <<'!COMMENT'
############################################## DEBUG BLOCK
printf "All arguments: "
for rax in "${rawArgs[@]}"; do
echo -n "$rax "
done
echo;
echo "Size of all arguments: $rawArgsSize"
echo;
#
# (Insert any additional variables needed for debugging.)
##############################################/DEBUG BLOCK
!COMMENT
# Return desired value, and break out of loop.
if [ ! -z "$value" ]; then
printf "$value"
break;
fi
done # END FOR
} # END FUNCTION: parseArgs