shell判断给定日期是否是周末or月末

一、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


##########################################################################################

  • 0
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值