LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX
User Name
Password
Other *NIX This forum is for the discussion of any UNIX platform that does not have its own forum. Examples would include HP-UX, IRIX, Darwin, Tru64 and OS X.

Notices


Reply
  Search this Thread
Old 05-10-2010, 12:42 PM   #1
Coldmiser
Member
 
Registered: Sep 2003
Location: Chicago
Distribution: Ubuntu
Posts: 36

Rep: Reputation: 15
Bash scripting question (CAL & DATE)


Hi,

I am trying to create a calendar (on the fly) that will display next month.

I'm not talking about June 2010 (although since this is may that is next month). I want to have a script that will display a calendar for next month no matter when it is run.

If it is run in February, it will create a calendar for March.
If it is run in June, it will create a calendar for July.
If it is run in December, it will create a calendar for January.

I have tried:
Code:
cal -m `date +"%m"`
but it doesn't like that format (I'm assuming because of the leading zero.

Any suggestions anyone?

Last edited by Coldmiser; 05-10-2010 at 01:10 PM.
 
Old 05-10-2010, 01:08 PM   #2
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,387

Rep: Reputation: 174Reputation: 174
%M stands for minutes
 
Old 05-10-2010, 01:10 PM   #3
Coldmiser
Member
 
Registered: Sep 2003
Location: Chicago
Distribution: Ubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks for catching the typo
Lower case m for months

Last edited by Coldmiser; 05-10-2010 at 01:13 PM.
 
Old 05-10-2010, 01:42 PM   #4
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Something like this?
Code:
cal -m $(date +"%m %Y" -d "next month")
 
Old 05-10-2010, 02:16 PM   #5
Coldmiser
Member
 
Registered: Sep 2003
Location: Chicago
Distribution: Ubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Although that works on Linux, it doesn't seem to work on my Mac where I had intended on using it.
I was hoping there wouldn't be a difference but apparently there is.

Anyway to do this on a Mac?
(Yes, I realize this is LINUXQUESTIONS and not MACQUESTIONS but I can hope for an answer right?)
 
Old 05-10-2010, 02:22 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Not knowing how or why it's failing on your Mac I cannot answer this.
But I'm moving the question over to "Other *Nix" since it doesn't
belong here for obvious reasons.


Cheers,
Tink
 
Old 05-10-2010, 02:26 PM   #7
Coldmiser
Member
 
Registered: Sep 2003
Location: Chicago
Distribution: Ubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Thanks for your help.

Yeah, I should have mentioned that.
When I run the command on my Mac, I'm getting the following errors:

Code:
date: illegal time format
usage: date [-jnu] [-d dst] [-r seconds] [-t west] [-v[+|-]val[ymwdHMS]] ...
            [-f fmt date | [[[mm]dd]HH]MM[[cc]yy][.ss]] [+format]
cal: option requires an argument -- m
usage: cal [-jy] [[month] year]
       cal [-j] [-m month] [year]
       ncal [-Jjpwy] [-s country_code] [[month] year]
       ncal [-Jeo] [year]
Obviously date doesn't like the time format.
 
Old 05-10-2010, 02:39 PM   #8
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora
Posts: 3,935
Blog Entries: 5

Rep: Reputation: Disabled
Break the problem down into two parts. First, you need to focus on getting the numeric value for next month returned. Then you need to feed that value to the cal(1) command.

I won't have access to my OSX box until later today, but this may help in the meantime. (Or, it may be totally useless.)

Code:
# uname 
FreeBSD

# date -v+1m +%m
06
If your date(1) program honors those precise options and formatting, you should be in business. If not, I'll check OSX later.
 
Old 05-10-2010, 03:08 PM   #9
Coldmiser
Member
 
Registered: Sep 2003
Location: Chicago
Distribution: Ubuntu
Posts: 36

Original Poster
Rep: Reputation: 15
Yup, that was exactly what I needed.
This is the answer to my question:

Code:
cal -m`date -v-1m +"%m"`
Thanks for everyone's help.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I calculate the number of days from a particular date using the <cal> utility? deepumnit Linux - Desktop 3 12-31-2007 12:12 PM
Bash scripting using date pueblonative Programming 3 01-16-2007 09:34 AM
Use Cal instead of date command - Bash Blackout_08 Programming 1 06-22-2006 11:39 AM
Bash scripting and && operator Dark_Helmet Programming 16 09-18-2005 09:57 PM
bash shell scripting - && and || gui10 Programming 10 12-15-2001 03:37 PM

LinuxQuestions.org > Forums > Other *NIX Forums > Other *NIX

All times are GMT -5. The time now is 05:24 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration