urtware / tools / bash / setuplib.sh @ f6e5368e
History | View | Annotate | Download (9.913 KB)
1 | 1fb06240 | skenneweg | ################################################################################ |
---|---|---|---|
2 | # AMiRo-uRtWare is a collection of applications for the Autonomous Mini Robot # |
||
3 | # (AMiRo) platform. # |
||
4 | # Copyright (C) 2016..2020 Thomas Schöpping et al. # |
||
5 | # # |
||
6 | # This program is free software: you can redistribute it and/or modify # |
||
7 | # it under the terms of the GNU General Public License as published by # |
||
8 | # the Free Software Foundation, either version 3 of the License, or # |
||
9 | # (at your option) any later version. # |
||
10 | # # |
||
11 | # This program is distributed in the hope that it will be useful, # |
||
12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # |
||
13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # |
||
14 | # GNU General Public License for more details. # |
||
15 | # # |
||
16 | # You should have received a copy of the GNU General Public License # |
||
17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # |
||
18 | # # |
||
19 | # This research/work was supported by the Cluster of Excellence Cognitive # |
||
20 | # Interaction Technology 'CITEC' (EXC 277) at Bielefeld University, which is # |
||
21 | # funded by the German Research Foundation (DFG). # |
||
22 | ################################################################################ |
||
23 | |||
24 | ### print an error message ##################################################### |
||
25 | # Prints a error <message> to standard output. |
||
26 | #If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
||
27 | # |
||
28 | # usage: printError <message> |
||
29 | # arguments: <message> |
||
30 | # Message string to print. |
||
31 | # return: n/a |
||
32 | # |
||
33 | function printError { |
||
34 | local string="ERROR: $1" |
||
35 | # if a log file is specified |
||
36 | if [ -n "$LOG_FILE" ]; then |
||
37 | printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
||
38 | fi |
||
39 | printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2 |
||
40 | } |
||
41 | |||
42 | ### print a warning message #################################################### |
||
43 | # Prints a warning <message> to standard output. |
||
44 | #If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
||
45 | # |
||
46 | # usage: printMessage <message> |
||
47 | # arguments: <message> |
||
48 | # Message string to print. |
||
49 | # return: n/a |
||
50 | # |
||
51 | function printWarning { |
||
52 | local string="WARNING: $1" |
||
53 | # if a log file is specified |
||
54 | if [ -n "$LOG_FILE" ]; then |
||
55 | printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
||
56 | fi |
||
57 | printf "$(tput setaf 3)>>> $string$(tput sgr 0)" |
||
58 | } |
||
59 | |||
60 | ### print an information message ############################################### |
||
61 | # Prints an information <message> to standard output. |
||
62 | #If variable 'LOG_FILE' is specified, the message is also appended to the given file. |
||
63 | # |
||
64 | # usage: printInfo <message> |
||
65 | # arguments: <message> |
||
66 | # Message string to print. |
||
67 | # return: n/a |
||
68 | # |
||
69 | function printInfo { |
||
70 | local string="INFO: $1" |
||
71 | # if a log file is specified |
||
72 | if [ -n "$LOG_FILE" ]; then |
||
73 | printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
||
74 | fi |
||
75 | printf "$(tput setaf 2)>>> $string$(tput sgr 0)" |
||
76 | } |
||
77 | |||
78 | ### print a message to file #################################################### |
||
79 | # Appends a <message> to a log file, specified by the variable 'LOG_FILE'. |
||
80 | # |
||
81 | # usage printLog <message> |
||
82 | # arguments: <message> |
||
83 | # Message string to print. |
||
84 | # return: n/a |
||
85 | # |
||
86 | function printLog { |
||
87 | local string="LOG: $1" |
||
88 | # if a log file is specified |
||
89 | if [ -n "$LOG_FILE" ]; then |
||
90 | printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE |
||
91 | fi |
||
92 | } |
||
93 | |||
94 | ### exit the script normally ################################################### |
||
95 | # Prints a delimiter and exits the script normally (returns 0). |
||
96 | # |
||
97 | # usage: quitScript |
||
98 | # arguments: n/a |
||
99 | # return: 0 |
||
100 | # No error or warning occurred. |
||
101 | # |
||
102 | function quitScript { |
||
103 | if [ ! -z ${BASH_SOURCE[1]} ]; then |
||
104 | printInfo "exiting $(realpath --relative-to="${PWD}" "${BASH_SOURCE[1]}")\n" |
||
105 | printf "\n" |
||
106 | fi |
||
107 | printf "######################################################################\n" |
||
108 | exit 0 |
||
109 | } |
||
110 | |||
111 | ### read a user input ########################################################## |
||
112 | # Reads a single character user input from a set up <options> and stores it in |
||
113 | # a given <return> variable. |
||
114 | # |
||
115 | # usage: readUserInput <options> <return> |
||
116 | # arguments: <options> |
||
117 | # String definiing the set of valid characters. |
||
118 | # If the string is empty, the user can input any character. |
||
119 | # <return> |
||
120 | # Variable to store the selected character to. |
||
121 | # return: n/a |
||
122 | # |
||
123 | function readUserInput { |
||
124 | local input="" |
||
125 | # read user input |
||
126 | while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do |
||
127 | read -p "your selection: " -n 1 -e input |
||
128 | if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then |
||
129 | printWarning "[$input] is no valid action\n" |
||
130 | fi |
||
131 | done |
||
132 | printLog "[$input] has been selected\n" |
||
133 | eval $2="$input" |
||
134 | } |
||
135 | |||
136 | ### check whether argument is an option ######################################## |
||
137 | # Checks a <string> whether it is an option. |
||
138 | # Options are defined to either start with '--' followed by any string, or |
||
139 | # to start with a single '-' followed by a single character, or |
||
140 | # to start with a single '-' followed by a single character, a '=' and any string. |
||
141 | # Examples: '--option', '--option=arg', '-o', '-o=arg', '--' |
||
142 | # |
||
143 | # usage: parseIsOption <string> |
||
144 | # arguments: <string> |
||
145 | # A string to check whether it is an option. |
||
146 | # return: 0 |
||
147 | # <string> is an option. |
||
148 | # -1 |
||
149 | # <string> is not an option. |
||
150 | # |
||
151 | function parseIsOption { |
||
152 | if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then |
||
153 | return 0 |
||
154 | else |
||
155 | return -1 |
||
156 | fi |
||
157 | } |
||
158 | |||
159 | ### set the log file ########################################################### |
||
160 | # Sets a specified <infile> as log file and checks whether it already exists. |
||
161 | # If so, the log may either be appended to the file, its content can be cleared, |
||
162 | # or no log is generated at all. |
||
163 | # The resulting path is stored in <outvar>. |
||
164 | # |
||
165 | # usage: setLogFile [--option=<option>] [--quiet] <infile> <outvar> |
||
166 | # arguments: --option=<option> |
||
167 | # Select what to do if <file> already exists. |
||
168 | # Possible values are 'a', 'c', 'r' and 'n'. |
||
169 | # - a: append (starts with a separator) |
||
170 | # - c: continue (does not insert a seperator) |
||
171 | # - r: delete and restart |
||
172 | # - n: no log |
||
173 | # If no option is secified but <file> exists, an interactive selection is provided. |
||
174 | # --quiet |
||
175 | # Suppress all messages. |
||
176 | # <infile> |
||
177 | # Path of the wanted log file. |
||
178 | # <outvar> |
||
179 | # Variable to store the path of the log file to. |
||
180 | # return: 0 |
||
181 | # No error or warning occurred. |
||
182 | # -1 |
||
183 | # Error: invalid input |
||
184 | # |
||
185 | function setLogFile { |
||
186 | local filepath="" |
||
187 | local option="" |
||
188 | local quiet=false |
||
189 | |||
190 | # parse arguments |
||
191 | local otherargs=() |
||
192 | while [ $# -gt 0 ]; do |
||
193 | if ( parseIsOption $1 ); then |
||
194 | case "$1" in |
||
195 | -o=*|--option=*) |
||
196 | option=${1#*=}; shift 1;; |
||
197 | -o*|--option*) |
||
198 | option="$2"; shift 2;; |
||
199 | -q|--quiet) |
||
200 | quiet=true; shift 1;; |
||
201 | *) |
||
202 | printError "invalid option: $1\n"; shift 1;; |
||
203 | esac |
||
204 | else |
||
205 | otherargs+=("$1") |
||
206 | shift 1 |
||
207 | fi |
||
208 | done |
||
209 | filepath=$(realpath ${otherargs[0]}) |
||
210 | |||
211 | # if file already exists |
||
212 | if [ -e $filepath ]; then |
||
213 | # if no option was specified, ask what to do |
||
214 | if [ -z "$option" ]; then |
||
215 | printWarning "log file $filepath already esists\n" |
||
216 | local userinput="" |
||
217 | printf "Select what to do:\n" |
||
218 | printf " [A] - append log\n" |
||
219 | printf " [R] - restart log (delete existing file)\n" |
||
220 | printf " [N] - no log\n" |
||
221 | readUserInput "AaRrNn" userinput |
||
222 | option=${userinput,,} |
||
223 | fi |
||
224 | # evaluate option |
||
225 | case "$option" in |
||
226 | a|c) |
||
227 | if [ $quiet = false ]; then |
||
228 | printInfo "appending log to $filepath\n" |
||
229 | fi |
||
230 | if [ $option != c ]; then |
||
231 | printf "\n" >> $filepath |
||
232 | printf "######################################################################\n" >> $filepath |
||
233 | printf "\n" >> $filepath |
||
234 | fi |
||
235 | ;; |
||
236 | r) |
||
237 | echo -n "" > $filepath |
||
238 | if [ $quiet = false ]; then |
||
239 | printInfo "content of $filepath wiped\n" |
||
240 | fi |
||
241 | ;; |
||
242 | n) |
||
243 | if [ $quiet = false ]; then |
||
244 | printInfo "no log file will be generated\n" |
||
245 | fi |
||
246 | filepath="" |
||
247 | ;; |
||
248 | *) # sanity check (return error) |
||
249 | printError "unexpected argument: $option\n"; return -1;; |
||
250 | esac |
||
251 | else |
||
252 | if [ $quiet = false ]; then |
||
253 | printInfo "log file set to $filepath\n" |
||
254 | fi |
||
255 | fi |
||
256 | |||
257 | eval ${otherargs[1]}="$filepath" |
||
258 | |||
259 | return 0 |
||
260 | } |
||
261 | |||
262 | ### check whether commands are available ####################################### |
||
263 | # Checks whether the specified commands are available and can be executed. |
||
264 | # |
||
265 | # usage: checkCommands [<command> <command> ...] |
||
266 | # arguments: <command> |
||
267 | # Name of the command to check. |
||
268 | # return: 0 |
||
269 | # All requested commands are available. |
||
270 | # >0 |
||
271 | # Number of requested commands that were not found. |
||
272 | # -1 |
||
273 | # No argument given. |
||
274 | # |
||
275 | function checkCommands { |
||
276 | local status=0 |
||
277 | |||
278 | # return if no argument was specified |
||
279 | if [ $# -eq 0 ]; then |
||
280 | return -1 |
||
281 | fi |
||
282 | |||
283 | # check all specified commands |
||
284 | while [ $# -gt 0 ]; do |
||
285 | command -v $1 &>/dev/null |
||
286 | if [ $? -ne 0 ]; then |
||
287 | printWarning "Command '$1' not available.\n" |
||
288 | status=$((status + 1)) |
||
289 | fi |
||
290 | shift 1 |
||
291 | done |
||
292 | |||
293 | return $status |
||
294 | } |