Hey,
I ecountered another problem in my project, i have set recordings of mettings and upon command i get the length of the file int he following format:
xhr ymn zs (i.e 10hr 5mn 6s)
(which would then be converted into 10:05:06)
The main issues im having are that:
1) it is not always the format xhr ymn zs, i.e if it was only 20mins 30secs long it will only be "20mn 30s" and so the output would need to be 00:20:30
2) extracting the numbers
3) if they are single digit, add a 0 to them.
I have attempted to try and figure this out myself, in regards to issue 1, the only thing i could think of was to have 3 if statements to check if "hr" was in there first, if not then "mn" and lastly "s" - however im not sure what the equivelant operator for an "is in" type thing would be, as im only aware of == !== and the numerical operators.
In regards to the second, i think this would be an easy job with regex, however im not familiar, and my attempt only returned the first value (x), which was:
echo `expr match "16hr 5mn 3s" '\([0-9][0-9]*[0-9][0-9]*[0-9][0-9]\)'`
(note: i just hardcoded the string whilst i was experimenting)
For the third issue, i think this would be simple, as following from 2, if it were possible to set the hr min and sec ammounts in variables, i could simply do something like:
Code:
hourlength="${#hour}"
if [ "$hourlength" -lt 2 ]; then
hour=0"$hour"
fi
(and the same for mins and secs)
Im not sure if there is perhaps a better way then what i was attempting to do, if so im open to suggestions.
I am trying to learn bash scripting, so though i appreciate it alot if you could provide the code, it would help me out alot if you could just comment the script, or explain what is going on, so i can have a better understanding, though i do understand not everyone has time to explain, especially if it is a complicated concept.
Id appreciate any assistance that could be given.
Thankyou.