LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-22-2010, 03:48 PM   #1
jdwalk
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Rep: Reputation: 0
Is it a leap year?


I have a point in my code where I'm trying to test a date coming in from the command line to see if it's a leap year and verify the number of days in the month. Arg $1 is the month, arg $2 is the day and arg $3 is the year.

I get: syntax error near unexpected token `$1'



elif [ ($1 = Feb -a $2 -gt 28 ) -o ( $1 = Feb -a $2 -gt 29 -a $3 % 4 -eq 0 ) ]; then
echo "BAD DATE"
 
Old 02-22-2010, 04:13 PM   #2
TB0ne
Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 14,456

Rep: Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538Reputation: 2538
Quote:
Originally Posted by jdwalk View Post
I have a point in my code where I'm trying to test a date coming in from the command line to see if it's a leap year and verify the number of days in the month. Arg $1 is the month, arg $2 is the day and arg $3 is the year.

I get: syntax error near unexpected token `$1'

elif [ ($1 = Feb -a $2 -gt 28 ) -o ( $1 = Feb -a $2 -gt 29 -a $3 % 4 -eq 0 ) ]; then
echo "BAD DATE"
Without seeing the rest of the code, it's hard to say. But a quick Google search can tell you that this is not a leap year in 2010.

But go with what you're getting told...you've got a syntax error.
 
Old 02-22-2010, 04:25 PM   #3
jdwalk
LQ Newbie
 
Registered: Feb 2010
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by TB0ne View Post
Without seeing the rest of the code, it's hard to say. But a quick Google search can tell you that this is not a leap year in 2010.

But go with what you're getting told...you've got a syntax error.


It's part of a homework assignment.
 
Old 02-22-2010, 05:34 PM   #4
jlinkels
Senior Member
 
Registered: Oct 2003
Location: Bonaire
Distribution: Debian Lenny/Squeeze/Wheezy/Sid
Posts: 4,067

Rep: Reputation: 491Reputation: 491Reputation: 491Reputation: 491Reputation: 491
The syntax in Bash for such a complicated statement becomes awkwrd in Bash:

Code:
if [ \( $1 = Feb -a $2 -gt 28 \)   -o  \( $1 = Feb -a $2 -gt 29 -a $(($3 % 4)) -eq 0 \) ];
It is better to split the statement in a number of separate tests and test the outcome of those tests like:

Code:
if [ $1 = Feb -a $2 -gt 28 ]; then
   L28=1
else
   L28=0
fi

modyear=$(($3 % 4))
if [ $1 = Feb -a $2 -gt 29 -a $modyear -eq 0 ]; then
   DIV4=1
else
  DIV4=0
fi

if [ $L28 -eq 1 -o $DIV4 -eq 1 ]; then
...
fi
This is by far not the most efficient way to write, but it is much clearer. Also run your script with sh -x yourscript to obtain debugging information.

Also remember that if Year % 100 == 0, you must find Year % 400 to determine a leap year. Imagine the nightmare if you want to put all that in a single statement.

Are you sure you are required to do this Bash arithmetic? Otherwise you might want to take a look at the 'date' command.

jlinkels

Last edited by jlinkels; 02-22-2010 at 06:57 PM.
 
Old 02-22-2010, 06:06 PM   #5
anomie
Senior Member
 
Registered: Nov 2004
Location: Texas
Distribution: RHEL, Scientific Linux, Debian, Fedora, Lubuntu, FreeBSD
Posts: 3,930
Blog Entries: 5

Rep: Reputation: Disabled
Quote:
Originally Posted by jdwalk
I have a point in my code where I'm trying to test a date coming in from the command line to see if it's a leap year and verify the number of days in the month. Arg $1 is the month, arg $2 is the day and arg $3 is the year.
That's an odd homework assignment. A command-line-supplied month and day have nothing to do with whether "it's a leap year". Only year matters.

Check out the cal(1) command if you want to simplify things.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Best Linux Distro for a 5 year old desktop and a 3 year old laptop silencer07 Linux - General 15 07-25-2008 03:44 PM
LXer: New Year 2007 - The year of GNU/Linux LXer Syndicated Linux News 0 01-01-2007 03:21 AM
LXer: EE Times Announces Recipients of the Educator of the Year and Student of the Year ACE Awards LXer Syndicated Linux News 0 03-29-2006 02:21 AM


All times are GMT -5. The time now is 06:15 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration