LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Is it a leap year? (http://www.linuxquestions.org/questions/linux-newbie-8/is-it-a-leap-year-790863/)

jdwalk 02-22-2010 04:48 PM

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"

TB0ne 02-22-2010 05:13 PM

Quote:

Originally Posted by jdwalk (Post 3873094)
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.

jdwalk 02-22-2010 05:25 PM

Quote:

Originally Posted by TB0ne (Post 3873121)
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.

jlinkels 02-22-2010 06:34 PM

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

anomie 02-22-2010 07:06 PM

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.


All times are GMT -5. The time now is 03:51 AM.