Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Shell 检查当月的最后一个星期六_Shell_Unix_Awk - Fatal编程技术网

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

Shell 检查当月的最后一个星期六,shell,unix,awk,Shell,Unix,Awk,我正在尝试运行一个shell脚本,它检查今天是否是本月的最后一个星期六,然后运行一些脚本。因为eg-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&quo

我正在尝试运行一个shell脚本,它检查今天是否是本月的最后一个星期六,然后运行一些脚本。因为eg-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.
有人能帮忙吗?
提前感谢

请尝试以下内容。我将它作为一个命令运行,您可以将它保存在脚本中,也可以从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.
    }
}'

如果您使用的是GNU AWK,您可能会利用它的功能。我会按以下方式检查:

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天后将是不同的月份,则这是上周六,否则不是,我将相应地打印一些备选方案,以获得本月的最后一个周六:

  • ncal

    $ncal | awk'$1==“Sa”{print$NF}”
    28
    
  • 卡尔

    $cal | awk'NF==7{day=$7}END{print day}
    28
    
  • GNU date和一些shell算法:它使用嵌套的日期调用获取本月最后一天的日期和星期几,然后向后迭代直到星期六


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

    在某些BSD系统上,可以使用
    date-v1d-v+1m-v-1d-v-sat
    获取当月的最后一个星期六。您在
    date=(date+%d)
    中缺少一个$好主意,但您应该使用今天中午来获取从纪元到今天的秒数(nowtime)而不是今天的当前实际时间,这样,如果当前时间(或1周后的等效时间)在受DST转换影响的一个小时内,当您在一周后添加典型秒数时,您不会得到错误的结果,因为这可能会使您的“+1周”计算将在星期五或星期天结束,而不是一周后的星期六。请添加一些空格,以便您的代码可读。@EdMorton:我必须承认我并没有考虑这个edge案例谢谢您的回复。您能告诉我这里的测试表明了什么吗?测试是一个实用程序二进制和/或shell内部,它进行比较并返回真/假返回值