I am trying to write a function that should convert a timestamp of the form hr: min: sec, ms (i.e. 15: 41: 47,757) into milliseconds. The function is as follows:
#!/bin/sh
mili () {
hr=$(echo "$1" | cut -c1-2)
echo "hr is: " $hr
min=$(echo "$1" | cut -c4-5)
echo "min is: " $min
sec=$(echo "$1" | cut -c7-8)
echo "sec is: " $sec
ms=$(echo "$1" | cut -c10-12)
echo "ms is: " $ms
total=$(($hr \* 3600 + $min \* 60 + $sec) \* 1000 + $ms)
return "$total"
}
mili $1
However, when I run it:
./mili.sh "15: 41: 47,757"
I get the following message:
./mili.sh: command substitution: line 15: syntax error near unexpected token
`\*'
./mili.sh: command substitution: line 15: `($hr \* 3600 + $min \* 60 + $sec)
\* 1000 + $ms'
./mili.sh: line 17: return: : numeric argument required
I tried expr options with and without single quotes, double quotes, and writebacks, but could never get it to calculate arithmetic. I can confirm a simple command how it works: expr 2 * 3, but when I try to use something like this in my script, it fails.
How can I make it just evaluate my expression?
source
share