i try writing script on ESXI and i need add "if last Sunday of month". I try

date -d @$(( $(date -d $(date -d @$(( $(date +%s) + 2678400 )) +%Y%m01) +%s) - 604800 )) +%d

It can not work, but it work on Debian.

On ESXI now output August


I believe the question is

Given a particular date, can I determine whether it is the last Sunday in the month?

and not the more general question

Given a particular month, on what day is its last Sunday?

Given that, we can divide the problem in two:

  • Is the date a Sunday?
  • Is it the last week of the month?

For the first part, the test is easy enough:

date -d "$date" +%a  # outputs "Sun" for a Sunday

We can test that:

test $(date -d "$date" +%a) = Sun  # success if $date is a Sunday

Now, to test whether it's the last week of the month, we can add one week to the date, and see if that gives us one of the first 7 days of the next month:

test $(date -d "$date + 1week" +%e) -le 7

Since the weekday of $date + 1week is the same as that of $date, we can generate both parts of the test in one go, and use a Bash regular expression test:

if [[ $(date -d "$date + 1week" +%d%a) =~ 0[1-7]Sun ]]
    echo "$date is the last Sunday of the month!"


$ ./330571.sh 2016-12-01
$ ./330571.sh 2016-12-04
$ ./330571.sh 2016-12-25
2016-12-25 is the last Sunday of the month!
$ ./330571.sh 2017-01-28
$ ./330571.sh 2017-01-29
2017-01-29 is the last Sunday of the month!
  • Ok, but ESXI not understand 1week. "$date + 1week" may replace on date -d @$(( $(date +%s) + 604800)) – Nikita Dec 15 '16 at 16:32
  • I was assuming the GNU implementation of date - if you can't install that, then I guess you're stuck with the one you're given. Seems like you've found a suitable workaround, anyway. – Toby Speight Dec 15 '16 at 16:40

You can do this with cal and awk:

$ cal | awk '/^ *[0-9]/ { d=$1 } END { print d }'


cal prints the current month with Sunday as the first column (by default):

$ cal
    December 2016
Su Mo Tu We Th Fr Sa
             1  2  3
 4  5  6  7  8  9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31

The awk command matches for lines starting with zero or more spaces, followed by a digit, and stores the first field into variable d. At the end, d will be the last Sunday of the current month.

  • In ESXI not command cal or ncal. – Nikita Dec 15 '16 at 12:28
if ( (( $(date +%d -d "$now") > `expr $(date +%d -d "$(date +%Y -d "$now")/$(date +%m -d "$now")/1 month day ago") - 7` )) && (( $(date +%w -d "$now") == 6 )) );
  then echo "Today ($now) is BLACK SABBATH"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.