Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

using cal terminal command for next and previous months

sorry if answer is elsewhere, i couldn't find it. i've looked over the support page with all the syntax for dates, and still can't figure out how to do this.

i use the terminal command:

cal -m $(date %mm)

to get a calender of the CURRENT month.

but what string does one use for the PREVIOUS or NEXT month?

thanks!

n/a, Mac OS X (10.5.6)

Posted on May 6, 2009 9:09 AM

Reply
10 replies

May 6, 2009 9:31 AM in response to e rose

I use the bash shell, and to get the current month, I just type cal and hit return:

NoobiX:~ francine$ cal
May 2009
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

For last month, just add the -m option and the number of the month:

NoobiX:~ francine$ cal -m 4
April 2009
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

One would do the same to get next month. To get the month of a particular year:

NoobiX:~ francine$ cal 10 1943
October 1943
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

I'm not sure what you mean by "i've looked over the support page"--but I just brought up the man page with a "man cal" command. Admittedly man pages can be rather, umm, opaque in figuring out what it is they are trying to tell you. :-0
Francine

User uploaded file
Francine
Schwieder

May 6, 2009 4:28 PM in response to e rose

thanks for your replies. to clarify, i'm not using bash (it's for a geektool script) and it needs to be dynamic, so not the specific month number or name as i'm not going to go in and change it every month. like the current month one is coded for whatever month it happens to be, i need to revise that so the result is the previous and next months, regardless of when that is.

thanks

May 6, 2009 6:06 PM in response to e rose

These work for me.

Previous month:
cal -m $(($(date +%m) - 1))

Next month:
cal -m $(($(date +%m) + 1))


Edit:
This may fail in December and January since the 12 + 1 = 13 and 1 - 1 = 0 (neither of which is accepted by cal). The general idea is correct though, you'll just need to add some extra logic to fix the edges.

Message was edited by: Noah Robbin

May 7, 2009 10:31 AM in response to Noah Robbin

One more thought, is there a good way to do this such that you don't call date more than once. Running this near/at midnight at the end of the month could cause a month to be skipped. (This is more an exercise than answering the original question)

This is as close as I can come, but it is more of a script

#!/bin/sh
D=( $(date "+%m %Y") )
M=${D[0]}
Y=${D[1]}
cal $((M-1 < 1 ? 12 : M-1 )) $((M-1 < 1 ? Y-1 : Y))
cal $M $Y
cal $((M+1 > 12 ? 1 : M+1 )) $((M+1 > 12 ? Y+1 : Y))

using cal terminal command for next and previous months

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.