shell(9)秒转换为时分秒

计算文件的创建了多久,距离当前的时间:

ans=0s 
if [ -f ${File} ]; then  #如果文件存在
      a=`stat -c %Y $File`
      b=`date +%s`
      c=$((b-a))
      swap_seconds $c   #转换为时分秒
 fi
echo $ans

转换为时分秒:如果是在shell 中,可以把 ans 设置为全局变量;

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
原文地址:https://www.cnblogs.com/lovychen/p/14467989.html