LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-07-2016, 05:48 PM   #1
theki
LQ Newbie
 
Registered: May 2016
Posts: 1

Rep: Reputation: Disabled
Bash script taking account a leap year


...

Last edited by theki; 05-08-2016 at 06:55 AM.
 
Old 05-07-2016, 06:13 PM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by theki View Post
Hello I am new at bash, the question is that I am creating a bash script which it shows the number of days in a month using arguments like "./numberMonth.sh 3 2016" and taking account a leap year.
I am having difficult to make it, can anyone help me a little?
I wrote this:

And it shows the number of days(31) but I want the name of month followed by the number of days like "March has 31 days"
I tried with echo command and doesn't work.
Read the "Question Guidelines" link in my posting signature. We are happy to help you with a script, but you've posted a single command...show us what you have written/tried on your own, and we will help.
 
Old 05-07-2016, 07:17 PM   #3
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
If you want to " taking account a leap year" then March is the wrong month.
http://xyproblem.info/
 
Old 05-08-2016, 04:50 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
I am not sure what the leap year part has to do with the question? You are going to supply the year and the numeral version of the month which cal will then return the necessary information (including
return 29 days for February if needed).

A simple awk seems to do the trick.
 
Old 05-08-2016, 07:39 AM   #5
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by grail View Post
A simple awk seems to do the trick.
Yep. Managed to do this with cal | tail | awk

You can do this using echo | tail

Another way is to use Python and monthrange, or php and cal_days_in_month

But the first way is probably fastest.

/HMW

Last edited by HMW; 05-08-2016 at 07:50 AM.
 
Old 05-08-2016, 08:50 AM   #6
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
...and this, right here, is why I always quote the original questions. The OP came and deleted what they posted, instead of posting what they ACTUALLY DID to solve their issue.
 
1 members found this post helpful.
Old 05-08-2016, 03:54 PM   #7
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
Yes ... poor form on behalf of the OP

So for others, here was my solution:
Code:
cal 3 2016 | awk 'NR == 1 { b = $1}NF{a = $NF}END{print b,"has",a,"days"}'

Last edited by grail; 05-08-2016 at 03:55 PM.
 
1 members found this post helpful.
Old 05-09-2016, 02:18 AM   #8
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
This is what I boiled it down to:
Code:
$ echo $(cal 5 2016) | tail -c 3
31
Best regards,
HMW

Edit: Just now I reread OP's original question. He/she also wants the name of the month. It's done fairly easy using my, or grail's approach. DS.

Last edited by HMW; 05-09-2016 at 02:28 AM.
 
Old 05-09-2016, 06:25 AM   #9
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by grail View Post
I am not sure what the leap year part has to do with the question?
Well, as much as I love "date math" in bash, I'd still have to defer to other more experienced
opinions, such as grail's.
 
Old 05-09-2016, 09:30 AM   #10
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,552

Rep: Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872Reputation: 872
Code:
date -d "5/1/2016 next month yesterday" "+%B has %d days"

Last edited by keefaz; 05-09-2016 at 09:34 AM.
 
3 members found this post helpful.
Old 05-09-2016, 09:55 AM   #11
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Code:
date -d "5/1/2016 next month yesterday" "+%B has %d days"
Now you're just showing off!
 
Old 05-10-2016, 02:21 AM   #12
HMW
Member
 
Registered: Aug 2013
Location: Sweden
Distribution: Debian, Arch, Red Hat, CentOS
Posts: 773
Blog Entries: 3

Rep: Reputation: 369Reputation: 369Reputation: 369Reputation: 369
Quote:
Originally Posted by keefaz View Post
Code:
date -d "5/1/2016 next month yesterday" "+%B has %d days"
Bravo!
 
Old 05-10-2016, 06:17 AM   #13
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
yesterday's solution cal 3 2016 | grep -v [a-z] | wc -w also seems to be deleted.

Last edited by AnanthaP; 05-12-2016 at 05:45 AM.
 
  


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
[SOLVED] Bash script for printing days in a specific month (taking into account a leap year) shayno90 Programming 28 08-23-2018 05:50 PM
[SOLVED] Run bash command taking input from HTML page - CGI script vickyk Programming 6 04-15-2013 11:36 PM
Is it a leap year? jdwalk Linux - Newbie 4 02-22-2010 06:06 PM
LXer: Taking the Vista leap? LXer Syndicated Linux News 0 05-13-2008 09:20 AM
LXer: Taking the Vista leap? LXer Syndicated Linux News 0 05-13-2008 08:40 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:26 AM.

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