一、shell 判断某日期是否是周日
1、shell 判断某日期是周几
通过date命令获取,获取结果:0为星期日,1-6为星期一至星期六。
date +%w 取得当天是星期几
date -d 20120311 +%w 取得2012年3月11日是星期几
date -d 2012-03-11 +%w 取得2012年3月11日是星期几
date -d $datebuf +%w 取得datebuf是星期几
2、shell 判断某日期是否是周日
##########################################################################################
### sunday judge ###
##########################################################################################
dayofweek_is_sunday=0
echo dayofweek_is_sunday_begin=$dayofweek_is_sunday
dayofweek=`date -d $datebuf +%w`
echo dayofweek_begin=$dayofweek
if [ $dayofweek -eq 0 ]
then
dayofweek_is_sunday=1
else
dayofweek_is_sunday=0
fi
echo dayofweek_is_sunday=$dayofweek_is_sunday
echo "Info::: dayofweek_is_sunday=1 : $datebuf is Sunday; dayofweek_is_sunday=0 : $datebuf is not Sunday"
##########################################################################################
二、shell 判断某日期是否是月末
思路:如果 日期+1 = 下月第一天,则是月末
shell 获取某日期的下月一号
[hadoop@uhadoop-jhr2pf-master2 coordinator_shell]$ datebuf=2018-08-08
[hadoop@uhadoop-jhr2pf-master2 coordinator_shell]$ date -d" 1 month $datebuf" +"%Y-%-m-1"
2018-9-1
[hadoop@uhadoop-jhr2pf-master2 coordinator_shell]$ date -d" 1 month $datebuf" +"%Y-%m-01"
2018-09-01
应采用%-m格式。如果采用%m格式,if判断时将报错(因shell中将08 09等默认视为8进制格式)
shell 判断某日期是否是月末
思路:如果 日期+1 = 下月第一天,则是月末
##########################################################################################
### the end of a month judge ###
##########################################################################################
dayofmonth_is_end=0
echo dayofmonth_is_end_begin=$dayofmonth_is_end
echo dayofmonth_begin=$datebuf
dayofmonth_add_one_day=`date -d" 1 day $datebuf" +"%Y-%-m-%-d"`
echo dayofmonth_add_one_day=$dayofmonth_add_one_day
-- the_first_day_of_next_month=`date -d" 1 month $datebuf" +"%Y-%-m-1"`
-- echo the_first_day_of_next_month=$the_first_day_of_next_month
-- if [[ $dayofmonth_add_one_day -eq $the_first_day_of_next_month ]]
-- 注释的方法error,因08-31加1月会变成10-1
cut_day=`date -d" $dayofmonth_add_one_day" +"%-d"`
echo cut_day=$cut_day
if [ $cut_day -eq 1 ]
then
dayofmonth_is_end=1
else
dayofmonth_is_end=0
fi
echo dayofmonth_is_end=$dayofmonth_is_end
echo "Info::: dayofmonth_is_end=1 : $datebuf is the end of a month; dayofmonth_is_end=0 : $datebuf is not the end of a month"
the_first_day_of_this_month=`date -d" $datebuf" +"%Y-%m-01"`
echo the_first_day_of_this_month=$the_first_day_of_this_month
##########################################################################################