Bash – How to sum time using bash

arithmeticbashtime

I want to know the total amount of time that a series of processes would take in my computer to decide if I should running there or in a stronger computer. So, i am forecasting the running time of each command. The output looks like:

process1    00:03:34
process2    00:00:35
process3    00:12:34

How can I sum the second column to obtain a total running time? I could try pipping each line through

awk '{sum += $2 } END { print sum }

but this makes no sense as the values are not natural numbers.

Best Answer

#!/bin/sh

EPOCH='jan 1 1970'
sum=0

for i in 00:03:34 00:00:35 00:12:34
do
  sum="$(date -u -d "$EPOCH $i" +%s) + $sum"
done
echo $sum|bc

date -u -d "jan 1 1970" +%s gives 0. So date -u -d "jan 1 1970 00:03:34" +%s gives 214 secs.