4 ms·
There is also BASH_REMATCH[0] used in conjunction with the regex comparison "=~" to capture subpatern Here is a function to convert HH:MM:SS to seconds: tos
by zouhair 7y ago
There is also BASH_REMATCH[0] used in conjunction with the regex comparison "=~" to capture subpatern
Here is a function to convert HH:MM:SS to seconds:
toseconds() {
# https://stackoverflow.com/a/12986612/1469043
Time="$1"
while :; do
if [[ $Time =~ ^([0-9]{2}):([0-9]{2}):([0-9]{2})$ ]]; then
if [[ ! BASH_REMATCH[3] < 60 ]]\
&& [[ ! BASH_REMATCH[2] < 60 ]]\
&& [[ ! BASH_REMATCH[1] < 24 ]]; then
break
fi
fi
read -p "Wrong format. Please use the HH:MM:SS format: " -e Time
done
echo "$Time" | awk -F: '{ print ($1 * 3600) + ($2 * 60) + $3 }'
}
[0]: https://www.gnu.org/software/bash/manual/html_node/Bash-Variables.html https://www.gnu.org/software/bash/manual/html_node/Bash-Vari...