#!/bin/bash
###
# ./str2msec.sh "1 d 11 h 12 m 23 s"
#
# или так
#
# echo "1 d 11 h 12 m 23 s" | xargs -0 ./str2msec.sh
###
argument="$1"
# Варианты аргументов:
#argument="1 d 11 h 12 m 23 s"
# or
#argument="1 d 12 m 23 s"
# or
#argument=" 11 h 23 s"
# or
#argument="11 hm 23"
## Убираем пробелы
data=${argument// /}
# Результат из примеров выше:
#data="1d11h12m23s"
# or
#data="1d12m23s"
# or
#data="11h23s"
# or
#data="11hm23"
echo
echo "data = $data"
echo
## Регуляркой делим на элементы
re='([0-9]+[d])?([0-9]+[h])?([0-9]+[m])?([0-9]+[s])?'
## Ищем совпадения и вычисляем
if [[ ${data} =~ ${re} ]]; then
echo "BASH_REMATCH = ${BASH_REMATCH}"
echo "BASH_REMATCH1 = ${BASH_REMATCH[1]}"
echo "BASH_REMATCH2 = ${BASH_REMATCH[2]}"
echo "BASH_REMATCH3 = ${BASH_REMATCH[3]}"
echo "BASH_REMATCH4 = ${BASH_REMATCH[4]}"
days=$(( $((${BASH_REMATCH[1]%d})) * 24 * 60 * 60 * 1000 ))
hours=$(( $((${BASH_REMATCH[2]%h})) * 60 * 60 * 1000 ))
minutes=$(( $((${BASH_REMATCH[3]%m})) * 60 * 1000 ))
seconds=$(( $((${BASH_REMATCH[4]%s})) * 1000 ))
result=$(( ${days} + ${hours} + ${minutes} + ${seconds} ))
else
echo "not matched"
exit 1
fi
echo
echo "Result = $result msec"
echo