有时候可能需要计算 shell 脚本 执行了多久, 怎么统计这个脚本 执行多长时间呢?


直接看 代码:

#!/bin/bash
start_time=`date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"`

#this is your shell script 
sleep 18

##############
finish_time=`date --date='0 days ago' "+%Y-%m-%d %H:%M:%S"`

duration=$(($(($(date +%s -d "$finish_time")-$(date +%s -d "$start_time")))))

echo "this shell script execution duration: $duration"



其中

date  +%s        自UTC 时间 1970-01-01 00:00:00 以来所经过的秒数
date  -d        -d,--date=字符串       显示指定字符串所描述的时间,而非当前时间


这里把 指定的时间 ,转化为以s 单位.
date +%s -d "$start_time"