如下脚本所示:

start_time=`date "+%Y-%m-%d %H:%M:%S"`

sleep 3s

end_time=`date "+%Y-%m-%d %H:%M:%S"`

duration=`echo $(($(date +%s -d "${end_time}") - $(date +%s -d "${start_time}"))) | awk '{t=split("60 s 60 m 24 h 999 d",a);for(n=1;n<t;n+=2){if($1==0)s="0s";break;s=$1%a[n]a[n+1]s;$1=int($1/a[n])}print s}'`

echo "开始时间: $start_time"
echo "结束时间: $end_time"
echo "累计耗时: $duration"

(END)