![]() |
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" |
Quote:
But go with what you're getting told...you've got a syntax error. |
Quote:
It's part of a homework assignment. |
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 \) ];Code:
if [ $1 = Feb -a $2 -gt 28 ]; thenAlso 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 |
Quote:
Check out the cal(1) command if you want to simplify things. |
| All times are GMT -5. The time now is 07:57 PM. |