Cal command to display 5 months ?

Hi,

I'm curious to know if we can display 5 months of a calendar using the cal command. I know we can three successive months (cal -3) but I wanted to know if we can do it with 5 months for example. (Give a specific month, and get as a result two previous months + the month in question + two next months)

As I was looking for options to use with the command cal, I found there's one : cal -con[tinue]=n which displays the next n successive months starting with the month specified. I wanted to know if there's a similar command that displays the previous n months for example?

Thank you :slight_smile:

You could try:

for months in 6 7 8 9 10;do cal $months $(date +'%Y');done

Hope this helps

Oh thank you :slight_smile: ! This one works very well :smiley: !
I have another question related to this subject, I have found in this website : (unix.com/man-page/Linux/1/cal/ Man Page for cal) that I can use the command cal with these options :

cal [-3hj] [-A number] [-B number] -m month [year]

knowing that :
-A number
Display the number of months after the current month.

 -B number
     Display the number of months before the current month.

But when I try it, I get the same message as you did (invalid option -- 'A') . I don't understand why :frowning:

I'm using linux, so i cannot test. (manpage differs from what is shown here as linux manpage)
Did you try it like:

cal -A 1 -B 2

Yes, I wrote it like this :

cal -A 2 -B 2 7 2013

Please get used to the code tags...
This one's not so tragic, but for 'longer' codes in the future.

Kinda looks 'good' to me.
Just out of curiosity, i'd switch the arguments and try again, if that doesnt help, i have no further idea.

cal 7 2013 -A 2 -B 2

Since the first post did work, it would be nice to click on the "thanks" button too.

Thank you and hope this helps.

Okay, I will .. I'm new to this forum and I'm still discovering its features :slight_smile: !
Unfortunately, it didn't work. I was thinking that maybe it's because I don't have the same synopsis of the command cal when I try it compared to the one on the website I took the options (A and B) from..

That won't work for Jan, Feb, Nov, and Dec.

That documentation probably doesn't apply to your cal implemenation (it looks like the FreeBSD implementation). Those options are non-standard. Read your system's man cal instead.

Regards,
Alister

It actually did work for me when I tried Jan, Feb, Nov and Dec.
But your second answer is true :slight_smile: ! Thank you :slight_smile:

That for-loop from post #2 won't work with those four months because, even if you wrap the months using modulo-12 arithmetic, the year is left unadjusted.

Regards,
Alister

Yes, those 5! numbers represent the 5 months to be shown, i dont know which 5 months he wants.
Year is untouched, using the current, since i dont know which year he want to have shown either.

If you want to display the COMLPETE year 2042, your supposed to use:

cal -y 2042

Regards
sea

Oh yeees, now I understand ! You're right, if I choose November for example, then in the two next months, I obviously have December and January of the SAME year, which isn't what I want :confused: !
Thanks for highlighting this, I didn't pay attention to that !

Started to get fun on this so i prepared this to be used as executable file in either (on linux):

  • ~/bin
  • ~/.local/bin
  • /bin
  • /usr/bin
#!/bin/sh
ME=$(basename $0)	# This is the filename without the path
help_text="\nUsage: $ME MONTH [YEAR=$(date +'%Y') [RANGE=2  [STEP=1]]]
		\rExample:\t$ME $(date +'%m') $(date +'%Y')
		\r\t\t$ME $(date +'%m') $(date +'%Y') 4 3\n"
[[ -z $1 ]] && printf  "$help_text\n" && exit 99

showMonths_function () {  MONTH=$1 ; YEAR=$2 ; RANGE=$3 ; STEP=$4
	[[ -z $2 ]] && YEAR=$(date +'%Y')
	[[ -z $3 ]] && RANGE=2
	[[ -z $4 ]] && STEP=1
	FROM=$[ $MONTH - $RANGE ]
	TO=$[ $MONTH + $RANGE ]
	for months in $(seq $FROM $STEP $TO );do cal $months $YEAR;done
}
showMonths_function $@

Then use like:

showMonths 6 2014 4 2

Shows: Feb, Apr, Jun, Aug, Oct
As it shows June 2014 with a range of 4 months, but only showing every 2nd.

The 3rd argument, to alter the range, is optional!
Default is 2.
4th argument could alter the steps it takes, 1 for each, 2 for every 2nd. etc.
Default is 1.

showMonths 6 2022

Shows April-August of 2022

Or even just:

showMonths 6

For the same timerange of the current year.

99 ~ $ showMonths 6
     April 2013     
Mo Di Mi Do Fr Sa So
 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

      Mai 2013      
Mo Di Mi Do Fr Sa So
       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

      Juni 2013     
Mo Di Mi Do Fr Sa So
                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

      Juli 2013     
Mo Di Mi Do Fr Sa So
 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

     August 2013    
Mo Di Mi Do Fr Sa So
          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

Hope this helps :slight_smile: