有一个shell脚本,可以获得shell脚本运行的时间,但是以秒显示的,比如70s,而不是1分钟10秒,想要以后一种形式显示,该如何做?
#!/usr/bin/bash
#统计shell运行时间,date相减
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
#运行的程序
sleep 70s
#sleep 10m
endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]
echo "$startTime ---> $endTime" "Total:$sumTime seconds"
###以下代码不对,如何修改
h=$($sumTime/3600)
$m=$sumTime/60
$s=$sumTime%60
echo "总计:" h "小时" m "分钟" s "秒"
seconds=601
dateF=`eval "echo $(date -ud "@$seconds" +'$((%s/3600/24)) 天 %H 小时 %M 分 %S 秒')"`
echo $dateF
#!/bin/bash
displaytime() {
local T=$1
local D=$((T/60/60/24))
local H=$((T/60/60%24))
local M=$((T/60%60))
local S=$((T%60))
(( $D > 0 )) && printf '%d 天 ' $D
(( $H > 0 )) && printf '%d 小时 ' $H
(( $M > 0 )) && printf '%d 分 ' $M
printf '%d 秒\n' $S
}
displaytime 601
你可以用format直接转化成秒啊
ans="0s"
swap_seconds ()
{
SEC=$1
if [ $SEC -lt 60 ]; then
ans="${SEC}s" elif [ $SEC -ge 60 ] && [ $SEC -lt 3600 ];then
ans="$(( SEC / 60 ))m$(( SEC % 60 ))s"
elif [ $SEC -ge 3600 ]; then
ans="$(( SEC / 3600 ))h$(( (SEC % 3600) / 60 ))m$(( (SEC % 3600) % 60 ))s"
fi
}
swap_seconds 100
echo $ans
#!/usr/bin/bash
#统计shell运行时间,date相减
startTime=`date +%Y%m%d-%H:%M:%S`
startTime_s=`date +%s`
#运行的程序
sleep 3s
#sleep 10m
endTime=`date +%Y%m%d-%H:%M:%S`
endTime_s=`date +%s`
sumTime=$[ $endTime_s - $startTime_s ]
echo "$startTime ---> $endTime" "Total:$sumTime seconds"
###以下代码不对,如何修改
h="$sumTime/3600" | bc
m="$sumTime/60" | bc
s=$sumTime
if [ -z "$h" ];then
if [ -z "$m" ];then
echo "总计:$s秒"
else
echo "总计:$m 分 $s秒"
fi
else
echo "总计:$h 时 $m 分 $s秒"
fi