leap year script

leap year script

Post by Y W Won » Wed, 30 Sep 1998 04:00:00



The following script not correctly work :-

#!/bin/sh
# Purpose : check the year is leap or not
echo -n "Enter the year in NNNN : "
read year
if [ `expr $year % 4` = 0 -o `expr $year % 400` = 0 ]
then
    if [ `expr $year % 100` = 0 ]
    then
        echo $year is leap year
    else
        echo $year is not leap year
    fi
else
    echo $year is not leap year
fi

What's wrong ?

Thank you for your help in advance.

Y W Wong

 
 
 

leap year script

Post by Jim Harri » Wed, 30 Sep 1998 04:00:00


Your script says any year that is evenly divisible by 4 (anything
divisible by 400 is divisible by 4) and divisible by 100 is a leap year.
Anything else is not a leap year.

However:
Anything divisible by 4 but not by 100 is a leap year.
Anything divisible by 400 is a leap year.
Anything else is not a leap year (assuming Gregorian calandar, Orthodox
is slightly different)


> The following script not correctly work :-

> #!/bin/sh
> # Purpose : check the year is leap or not
> echo -n "Enter the year in NNNN : "
> read year
> if [ `expr $year % 4` = 0 -o `expr $year % 400` = 0 ]
> then
>     if [ `expr $year % 100` = 0 ]
>     then
>         echo $year is leap year
>     else
>         echo $year is not leap year
>     fi
> else
>     echo $year is not leap year
> fi

> What's wrong ?

> Thank you for your help in advance.

> Y W Wong


 
 
 

leap year script

Post by Y W Won » Wed, 30 Sep 1998 04:00:00


I am new in shell programming, what should be your suggested
answer.

Many thanks,

Y W Wong


>Your script says any year that is evenly divisible by 4 (anything
>divisible by 400 is divisible by 4) and divisible by 100 is a leap year.
>Anything else is not a leap year.

>However:
>Anything divisible by 4 but not by 100 is a leap year.
>Anything divisible by 400 is a leap year.
>Anything else is not a leap year (assuming Gregorian calandar, Orthodox
>is slightly different)


>> The following script not correctly work :-

>> #!/bin/sh
>> # Purpose : check the year is leap or not
>> echo -n "Enter the year in NNNN : "
>> read year
>> if [ `expr $year % 4` = 0 -o `expr $year % 400` = 0 ]
>> then
>>     if [ `expr $year % 100` = 0 ]
>>     then
>>         echo $year is leap year
>>     else
>>         echo $year is not leap year
>>     fi
>> else
>>     echo $year is not leap year
>> fi

>> What's wrong ?

>> Thank you for your help in advance.

>> Y W Wong

 
 
 

leap year script

Post by Michael P. Reill » Wed, 30 Sep 1998 04:00:00



: I am new in shell programming, what should be your suggested
: answer.

: Many thanks,

: Y W Wong

:>Your script says any year that is evenly divisible by 4 (anything
:>divisible by 400 is divisible by 4) and divisible by 100 is a leap year.
:>Anything else is not a leap year.
:>
:>However:
:>Anything divisible by 4 but not by 100 is a leap year.
:>Anything divisible by 400 is a leap year.
:>Anything else is not a leap year (assuming Gregorian calandar, Orthodox
:>is slightly different)
:>
:>
:>>
:>> The following script not correctly work :-
:>>
:>> #!/bin/sh
:>> # Purpose : check the year is leap or not
:>> echo -n "Enter the year in NNNN : "
:>> read year
:>> if [ `expr $year % 4` = 0 -o `expr $year % 400` = 0 ]
:>> then
:>>     if [ `expr $year % 100` = 0 ]
:>>     then
:>>         echo $year is leap year
:>>     else
:>>         echo $year is not leap year
:>>     fi
:>> else
:>>     echo $year is not leap year
:>> fi
:>>
:>> What's wrong ?
:>>
:>> Thank you for your help in advance.
:>>
:>> Y W Wong

You could use one expr(1) command for :
  year=`date +%Y`
  if expr \( $year % 400 \) == 0 \| \
    \( \( $year % 4 \) == 0 \& \( $year % 100 \) != 0 \) > /dev/null
  then
    ...
  fi

  -Arcege

 
 
 

leap year script

Post by J. S. Jense » Wed, 30 Sep 1998 04:00:00



> I am new in shell programming, what should be your suggested answer.

How about let another program worry about the calculation?  cal?

if [ `cal 2 $1 |sed -n -e '7p' |tail -c 3` -eq 29 ] ; then
        echo "$1 is a leap year."
else
        echo "$1 is not a leap year."
fi

--
J. S. Jensen

http://www.paramin.com

 
 
 

leap year script

Post by David W. Tamk » Wed, 30 Sep 1998 04:00:00



| How about let another program worry about the calculation?  cal?
|
| if [ `cal 2 $1 |sed -n -e '7p' |tail -c 3` -eq 29 ] ; then
|         echo "$1 is a leap year."
| else
|         echo "$1 is not a leap year."
| fi

Excellent idea, but what's with sed?  Just tail -c 3 should do it.  We don't
need tail either.

  case "`cal 2 $1`" in
  *29) echo "$1 is a leap year." ;;
  *28) echo "$1 is a common year." ;;
  *) echo "Please give me a hug.  I'm confused." >&2 ;;
  esac

If you're really intent on avoiding forks,

case $1 in
1[789]00) echo "$1 was common by Gregorian, leap by Julian." ;;
# not a multiple of four
*[13579]|2|6|*[13579][048]|*[02468][26]) echo common ;;
# multiple of four but not of 100, or century year before Gregory XIII
*[02468][48]|*[2468]0|?00|1[1-5]00|[048]) echo leap ;;
# century years after 1600 that are not divisible by 400
*[13579]00|*[13579][048]00|*[02468][26]00) echo common ;;
# multiples of 400 after Gregory XII (1200 won't get this far but 11200 will)
*[02468][02468]00|*[13579][26]00) echo leap ;;
# in case I missed anything
*) echo "Put in a provision for $1.  It got overlooked." >&2 ;;
esac

--
The reply address is valid but I'll see mail to dattier at mcs dot net sooner.

 
 
 

leap year script

Post by Richard Howlet » Wed, 30 Sep 1998 04:00:00




> > I am new in shell programming, what should be your suggested answer.

> How about let another program worry about the calculation?  cal?

> if [ `cal 2 $1 |sed -n -e '7p' |tail -c 3` -eq 29 ] ; then
>         echo "$1 is a leap year."
> else
>         echo "$1 is not a leap year."
> fi

Even simpler than that:

# $1 is 4 digit year entered on command line
if [ "`cal 2 $1 |grep 29`" ] ; then
        echo "$1 is a leap year."
else
        echo "$1 is not a leap year."
fi

--
Richard Howlett


 
 
 

leap year script

Post by Terry R. McConne » Wed, 30 Sep 1998 04:00:00






>> > I am new in shell programming, what should be your suggested answer.

>> How about let another program worry about the calculation?  cal?

>> if [ `cal 2 $1 |sed -n -e '7p' |tail -c 3` -eq 29 ] ; then
>>         echo "$1 is a leap year."
>> else
>>         echo "$1 is not a leap year."
>> fi

>Even simpler than that:

># $1 is 4 digit year entered on command line
>if [ "`cal 2 $1 |grep 29`" ] ; then
>            echo "$1 is a leap year."
>else
>        echo "$1 is not a leap year."
>fi

On my version of cal, this gives the incorrect response for
1929, 1829, ... .  The previous solution is correct, at least in this
regard.

--
************************************************************************
Terry R. McConnell   Mathematics/304B Carnegie/Syracuse, N.Y. 13244-1150

************************************************************************

 
 
 

leap year script

Post by Peter Samuels » Wed, 30 Sep 1998 04:00:00



Quote:> # $1 is 4 digit year entered on command line
> if [ "`cal 2 $1 |grep 29`" ] ; then
>            echo "$1 is a leap year."
> else
>         echo "$1 is not a leap year."
> fi

Useless Use of Backticks.
Useless Use of Quotes.
Useless Use of Test.
(:

# $1 is a 4-digit year from wherever
if cal 2 $1 | grep -q 29; then
  echo "$1 is a leap year."
else
  echo "$1 is not a leap year."
fi

--
Peter Samuelson
<sampo.creighton.edu!psamuels>

 
 
 

leap year script

Post by d.. » Thu, 01 Oct 1998 04:00:00



|> > # $1 is 4 digit year entered on command line
|> > if [ "`cal 2 $1 |grep 29`" ] ; then
|> >      echo "$1 is a leap year."
|> > else
|> >         echo "$1 is not a leap year."
|> > fi
|>
|> Useless Use of Backticks.
|> Useless Use of Quotes.
|> Useless Use of Test.

Possibly true... but your example fails in 1929 etc.
Does that make it "useless example"?

|> (:
|>
|> # $1 is a 4-digit year from wherever
|> if cal 2 $1 | grep -q 29; then
|>   echo "$1 is a leap year."
|> else
|>   echo "$1 is not a leap year."
|> fi
|>
|> --
|> Peter Samuelson
|> <sampo.creighton.edu!psamuels>

--
Views expressed are personal and not necessarily shared by my employer.

 
 
 

leap year script

Post by J. S. Jense » Thu, 01 Oct 1998 04:00:00




> [CODE SNIP]
> Useless Use of Backticks.
> Useless Use of Quotes.
> Useless Use of Test.

:-( Yes, sometimes there is quite a bit of UUOEverything, however, if
you examine this:

Quote:> if cal 2 $1 | grep -q 29; then

Will match any year that has a 29 in it, not particularly a leap year.
That is why I sed'ed it, though I guess one could just use tail to
determine if the last number is a 29.  I saw on dejanews that someone
actually used case to match the output.

I'm just never quite comfortable enough with case to make good use of
it.

--
J. S. Jensen

http://www.paramin.com

 
 
 

leap year script

Post by Peter Samuels » Thu, 01 Oct 1998 04:00:00



Quote:> > Useless Use of Backticks.
> > Useless Use of Quotes.
> > Useless Use of Test.
> > (:


Quote:> Possibly true... but your example fails in 1929 etc.
> Does that make it "useless example"?

Doh, yes it does.  But then the original was just as bad.  I wasn't
thinking too closely about the code itself, just making the point that

  ... | grep -q xxx

is functionally equivalent but considerably simpler and cheaper than

  [ "` ... | grep xxx`" ]

As posted elsewhere, the real way to do the leap year thing is with a
case statement:

  case `cal 2 $1` in *29) ...  ;; esac

--
Peter Samuelson
<sampo.creighton.edu!psamuels>

 
 
 

leap year script

Post by Richard Howlet » Fri, 02 Oct 1998 04:00:00




> > # $1 is 4 digit year entered on command line
> > if [ "`cal 2 $1 |grep 29`" ] ; then
> >       echo "$1 is a leap year."
> > else
> >         echo "$1 is not a leap year."
> > fi

> Useless Use of Backticks.
> Useless Use of Quotes.
> Useless Use of Test.
> (:

> # $1 is a 4-digit year from wherever
> if cal 2 $1 | grep -q 29; then
>   echo "$1 is a leap year."
> else
>   echo "$1 is not a leap year."
> fi

Several things:

1. Your version produces unwanted screen output where as using
Backticks, Quotes and Test this output is not displayed.

2. Your version is only 6 bytes (excluding the -q you added) longer than
mine.

3. Your version is certainly less readable.

4. Get a life and stop criticizing others suggestions. There is
another thread for UUO whatever. Post something there instead.

--
Richard Howlett


 
 
 

leap year script

Post by Richard Howlet » Fri, 02 Oct 1998 04:00:00





> |> > # $1 is 4 digit year entered on command line
> |> > if [ "`cal 2 $1 |grep 29`" ] ; then
> |> >            echo "$1 is a leap year."
> |> > else
> |> >         echo "$1 is not a leap year."
> |> > fi
> |>
> |> Useless Use of Backticks.
> |> Useless Use of Quotes.
> |> Useless Use of Test.

> Possibly true... but your example fails in 1929 etc.
> Does that make it "useless example"?

No. It means I couldn't test it! I'm on a Windoze machine. Here's one
that doesn't fail when year has 29 in it:

# $1 is 4 digit year
if [ "`cal 2 $1 |tail +2 |grep 29`" ] ; then
        echo "$1 is a leap year."
else
        echo "$1 is not a leap year."
fi

I am using backticks, quotes and test to prevent unwanted output to the
screen that Peter Samuelson's version had (ie the result of the grep).

Hope that clears that one up ;-)

--
Richard Howlett


 
 
 

leap year script

Post by Richard Howlet » Fri, 02 Oct 1998 04:00:00





> > > # $1 is 4 digit year entered on command line
> > > if [ "`cal 2 $1 |grep 29`" ] ; then
> > >       echo "$1 is a leap year."
> > > else
> > >         echo "$1 is not a leap year."
> > > fi

> > Useless Use of Backticks.
> > Useless Use of Quotes.
> > Useless Use of Test.
> > (:

> > # $1 is a 4-digit year from wherever
> > if cal 2 $1 | grep -q 29; then
> >   echo "$1 is a leap year."
> > else
> >   echo "$1 is not a leap year."
> > fi

> Several things:

> 1. Your version produces unwanted screen output where as using
> Backticks, Quotes and Test this output is not displayed.

Now I'm at work and have Peter's version on a Unix box I can see that the -q
works quite nicely. Sorry!

Quote:> 2. Your version is only 6 bytes (excluding the -q you added) longer than
> mine.

That should read: Your version is only a few bytes shorter than mine.

Quote:> 3. Your version is certainly less readable.

> 4. Get a life and stop criticizing others suggestions. There is
> another thread for UUO whatever. Post something there instead.

I still agree with 3 and 4.

--
Richard Howlett