Shell根据日期、月份、星期判断年份的代码
程序员文章站
2022-04-05 22:15:05
已知三个时间变量:
time1="fri aug 23 11:35:21"
time2="mon sep 2 16:18:09"
time3="tue sep 3 1...
已知三个时间变量:
time1="fri aug 23 11:35:21"
time2="mon sep 2 16:18:09"
time3="tue sep 3 16:06:33"
已知这三个时间可以确定是发生在2011-2013年,如何快速确定是哪一年呢?
希望输出:
time1="2013/8/23 11:35:21"
time2="2013/9/2 16:18:09"
time3="2013/9/3 16:06:33"
#!/bin/bash time1="fri aug 23 11:35:21" time2="mon sep 2 16:18:09" time3="tue sep 3 16:06:33" time4="sun dec 2 16:06:34" for((i=1;i<=4;i++)); do for((j=2011;j<=2013;j++)); do str=$(eval echo \$time$i) week_day=$(date -d "${str:0:10} $j ${str:10}" "+%a") if [ "${str:0:3}" == $week_day ]; then echo time$i=\"$(date -d "${str:0:10} $j ${str:10}" "+%y/%m/%d %t")\" # break fi done done