检查当月的最后一个星期六

时间:2020-11-06 13:50:31

标签: shell unix awk

我正在尝试运行一个Shell脚本,该脚本检查今天是否是当前月份的最后一个星期六,然后运行一些脚本。例如:28号是这个月的最后一个星期六。 这是我正在尝试的代码:

#!/bin/bash
DoDay=$(ncal -h | awk ' /Su/ {print $(NF-1)}')
Date=(date +%d)

if [[$Date == $DoDay]]
then 
    echo "Last Saturday"
else 
    echo "not last"
fi

在执行时,显示为:

ncal:command not found.

有人可以在这里帮助吗? 预先感谢

4 个答案:

答案 0 :(得分:1)

能否请您尝试以下。我将其作为命令运行,您可以将其保存在脚本中,也可以每天从cron运行它以进行检查。

cal $(date +%m) $(date +%Y) | 
tac | 
awk -v date=$(date +%d) '
  count==1{  exit  }
  NF==7{
    ++count
    if($(NF-1)==date){
      print "Today is last Saturday of month."
      exit
    }
    else{
      print "Today is NOT last Saturday of month."
    }
}'

对于今天的日期(2020年11月6日,星期五),是输出。

Today is NOT last Saturday of month.

说明: 添加以上详细说明。

cal $(date +%m) $(date +%Y) |                       ##Running cal command with passing current month and today date to it
tac |                                               ##Passing cal output to tac command to print it from bottom to top, since we want to read last lines only.
awk -v date=$(date +%d) '                           ##Passing previous output to awk as an Input and create date variable which has today date in it.
  count==1{  exit  }                                ##If count variable is 1 thne exiting from program.
  NF==7{                                            ##Checking condition if number of fields is 7 then do following.
    ++count                                         ##Increasing count with 1 here.
    if($(NF-1)==date){                              ##Checking condition if last field is date then do following.
      print "Today is last Saturday of month."      ##Printing that its last Saturday for this month.
      exit                                          ##exiting from program from here.
    }
    else{                                           ##Else part of above if condition here.
      print "Today is NOT last Saturday of month."  ##printing that its NOT last Saturday of this month.
    }
}'

答案 1 :(得分:1)

如果您使用的是GNU AWK,则可以利用其time functions。我将按照以下方式进行检查:

BEGIN{nowtime=systime();nowweekday=strftime("%u",nowtime);nowmonth=strftime("%m",nowtime);nextmonth=strftime("%m",nowtime+604800);if(nowweekday==6 && nowmonth!=nextmonth){print "Last saturday of month"}else{print "Not last saturday of month"}}

说明:首先我获得了从纪元开始到现在的秒数,然后我获得了当前工作日(数字(1-星期一,7-星期日)),当前月份作为数字(1-12)和日期为7的月份。天后(604800是7天中的秒数)。如果今天是星期六,而7天后将是另一个月,则是上个星期六,否则不是,我会相应打印

答案 2 :(得分:1)

一些替代方法可以获取该月的最后一个星期六:

  • ncal

    | Day        | startOfWork             | endOfWork               |
    |------------|-------------------------|-------------------------|
    | 2020-10-21 | 2020-10-21 14:45:00.000 | 2020-10-21 16:00:00.000 |
    | 2020-10-22 | 2020-10-22 08:00:00.000 | 2020-10-22 16:00:00.000 |
    | 2020-10-23 | 2020-10-23 08:00:00.000 | 2020-10-23 19:15:00.000 |
    
  • cal

    $ ncal | awk '$1 == "Sa" {print $NF}'
    28
    
  • 具有某些shell算术的GNU日期:它使用嵌套的日期调用来获取本月最后一天的日期和星期几,然后向后迭代直到星期几是星期六。 / p>

    $ cal | awk 'NF == 7 {day = $7} END {print day}'
    28
    

答案 3 :(得分:1)

GNU date具有一些简单的文本解析功能:

if test `date +%w` -eq 6 && test "$(date +%m)" -ne "$(date -d 'next saturday' +%m)"; then
    echo "Last Saturday"
else
    echo "not last"
fi