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 设置为全局变量;

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

 效果:1m40s

 

posted on 2021-03-02 11:18  细雨微光  阅读(1390)  评论(0)    收藏  举报