################################################################################
# AMiRo-OS is an operating system designed for the Autonomous Mini Robot #
# (AMiRo) platform. #
# Copyright (C) 2016..2019 Thomas Schöpping et al. #
# #
# This program is free software: you can redistribute it and/or modify #
# it under the terms of the GNU General Public License as published by #
# the Free Software Foundation, either version 3 of the License, or #
# (at your option) any later version. #
# #
# This program is distributed in the hope that it will be useful, #
# but WITHOUT ANY WARRANTY; without even the implied warranty of #
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
# GNU General Public License for more details. #
# #
# You should have received a copy of the GNU General Public License #
# along with this program. If not, see . #
# #
# This research/work was supported by the Cluster of Excellence Cognitive #
# Interaction Technology 'CITEC' (EXC 277) at Bielefeld University, which is #
# funded by the German Research Foundation (DFG). #
################################################################################
#!/bin/bash
################################################################################
# GENERIC FUNCTIONS #
################################################################################
### print an error message #####################################################
# Prints a error to standard output.
#If variable 'LOG_FILE' is specified, the message is also appended to the given file.
#
# usage: printError
# arguments:
# Message string to print.
# return: n/a
#
function printError {
local string="ERROR: $1"
# if a log file is specified
if [ -n "$LOG_FILE" ]; then
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE
fi
printf "$(tput setaf 1)>>> $string$(tput sgr 0)" 1>&2
}
### print a warning message ####################################################
# Prints a warning to standard output.
#If variable 'LOG_FILE' is specified, the message is also appended to the given file.
#
# usage: printMessage
# arguments:
# Message string to print.
# return: n/a
#
function printWarning {
local string="WARNING: $1"
# if a log file is specified
if [ -n "$LOG_FILE" ]; then
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE
fi
printf "$(tput setaf 3)>>> $string$(tput sgr 0)"
}
### print an information message ###############################################
# Prints an information to standard output.
#If variable 'LOG_FILE' is specified, the message is also appended to the given file.
#
# usage: printInfo
# arguments:
# Message string to print.
# return: n/a
#
function printInfo {
local string="INFO: $1"
# if a log file is specified
if [ -n "$LOG_FILE" ]; then
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE
fi
printf "$(tput setaf 2)>>> $string$(tput sgr 0)"
}
### print a message to file ####################################################
# Appends a to a log file, specified by the variable 'LOG_FILE'.
#
# usage printLog
# arguments:
# Message string to print.
# return: n/a
#
function printLog {
local string="LOG: $1"
# if a log file is specified
if [ -n "$LOG_FILE" ]; then
printf "[$(date '+%Y-%m-%d %H:%M:%S')] $string" >> $LOG_FILE
fi
}
### exit the script normally ###################################################
# Prints a delimiter and exits the script normally (returns 0).
#
# usage: quitScript
# arguments: n/a
# return: 0
# No error or warning occurred.
#
function quitScript {
printInfo "exiting $(realpath ${BASH_SOURCE[0]})\n"
printf "\n"
printf "######################################################################\n"
exit 0
}
### read a user input ##########################################################
# Reads a single character user input from a set up and stores it in
# a given variable.
#
# usage: readUserInput
# arguments:
# String definiing the set of valid characters.
# If the string is empty, the user can input any character.
#
# Variable to store the selected character to.
# return: n/a
#
function readUserInput {
local input=""
# read user input
while [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); do
read -p "your selection: " -n 1 -e input
if [ -z $input ] || ( [ -n "$1" ] && [[ ! $input =~ ^[$1]$ ]] ); then
printWarning "[$input] is no valid action\n"
fi
done
printLog "[$input] has been selected\n"
eval $2="$input"
}
### check whether argument is an option ########################################
# Checks a whether it is an option.
# Options are defined to either start with '--' followed by any string, or
# to start with a single '-' followed by a single character, or
# to start with a single '-' followed by a single character, a '=' and any string.
# Examples: '--option', '--option=arg', '-o', '-o=arg', '--'
#
# usage: parseIsOption
# arguments:
# A string to check whether it is an option.
# return: 0
# is an option.
# -1
# is not an option.
#
function parseIsOption {
if [[ "$1" =~ ^-(.$|.=.*) ]] || [[ "$1" =~ ^--.* ]]; then
return 0
else
return -1
fi
}
### set the log file ###########################################################
# Sets a specified as log file and checks whether it already exists.
# If so, the log may either be appended to the file, its content can be cleared,
# or no log is generated at all.
# The resulting path is stored in .
#
# usage: setLogFile [--option=