我想查找两个日期之间的一些信息(一个月的最后一个星期五和下一个星期日)。
现在,我将所有日期(格式为mm/dd/yy
)添加到文件dates_file.in
中,如下所示:
05/29/20,05/31/20
06/26/20,06/28/20
07/31/20,08/02/20
并像这样运行for
循环
for dates in `cat dates_file.in`
do
date1=`echo $dates | cut -d',' -f1`
date2=`echo $dates | cut -d',' -f2`
xyz_cmd -start="$date1" -end="$date2" -report_parms
done
有没有一种方法可以使用calendar
来获取日期或将日期导出到dates_file.in
文件?
OS是RHEL 6.5
答案 0 :(得分:1)
# a function to return a positive number for "x % y"
# e.g. `floorMod -1 7` outputs: 6
floorMod() { local num=$1 div=$2; echo $(( ((num % div) + div) % div )); }
formatDate() { local y=$1 m=$2 d=$3 delta=$4; date -d "$y-$m-$d $delta" "+%m/%d/%y"; }
year=2020
for month in {1..12}; do
# get the last day of this month, and its day of the week
read -r day dow < <(date -d "$year-$month-01 + 1 month - 1 day" "+%d %w")
# decrement until we find Friday
until ((dow == 5)); do
((day--))
dow=$(floorMod $((dow - 1)) 7)
done
start=$(formatDate $year $month $day)
end=$(formatDate $year $month $day "+2 days")
echo "$start $end"
done
01/31/20 02/02/20
02/28/20 03/01/20
03/27/20 03/29/20
04/24/20 04/26/20
05/29/20 05/31/20
06/26/20 06/28/20
07/31/20 08/02/20
08/28/20 08/30/20
09/25/20 09/27/20
10/30/20 11/01/20
11/27/20 11/29/20
12/25/20 12/27/20
答案 1 :(得分:0)
能否请您在此处使用cal
进行尝试。
cat script.ksh
month="$1"
year="$2"
####To get current month's Friday.
cal "$month" "$year" 2>/dev/null |
tac |
awk '
NF>=6{
print "current month last Friday is:"$(NF-1)" and Sunday is:"$NF
exit
}
'