Reputation: 119
I am trying to run a shell script which checks whether today is the last Saturday of the current month and then it runs some script. For eg- 28th is the last Sat of this month. This is the code I am trying:
#!/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
While executing, it shows as :
ncal:command not found.
Can anybody please help here? Thanks in advance
Upvotes: 2
Views: 673
Reputation: 1083
GNU date has some text parsing powers that simplyfies this:
if test `date +%w` -eq 6 && test "$(date +%m)" -ne "$(date -d 'next saturday' +%m)"; then
echo "Last Saturday"
else
echo "not last"
fi
Upvotes: 2
Reputation: 246799
Some alternatives to get the last Saturday of the month:
ncal
$ ncal | awk '$1 == "Sa" {print $NF}'
28
cal
$ cal | awk 'NF == 7 {day = $7} END {print day}'
28
GNU date with some shell arithmetic: This uses nested date calls to get the day and day-of-week of the last day of this month, then iterates backwards until the day-of-week is Saturday.
$ read day dow < <(date -d "$(date '+%Y-%m-01 +1 month -1 day')" '+%d %w')
$ while ((dow != 6)); do ((day--)); ((dow = (dow - 1 + 7) % 7)); done
$ echo $day
28
Upvotes: 1
Reputation: 36390
If you are using GNU AWK you might harness its time functions. I would check it following way:
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"}}
Explanation: firstly I get current time in terms of seconds since start of epoch, then I get current weekday as number (1-Monday, 7-Sunday), current month as number (1-12) and month of date 7 days later (604800 is number of seconds in 7 days). If today is Saturday and 7 days later will be different month this is last saturday, otherwise it is not, which I print accordingly
Upvotes: 1
Reputation: 133458
Could you please try following. I am running it as a command you could save it in a script and could run it from cron to check it on daily basis too.
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."
}
}'
For today's date(its Friday today on 6th Nov 2020) following is the output.
Today is NOT last Saturday of month.
Explanation: Adding detailed explanation for above.
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.
}
}'
Upvotes: 1