Re: days-between

From: Chris F.A. Johnson (c.fa.johnson_at_rogers.com)
Date: 01/07/04


Date: 7 Jan 2004 04:49:05 GMT

On Wed, 07 Jan 2004 at 03:21 GMT, Ted Breen wrote:
>
> Chris,
> I put the two changes to date1 and date2 and the original script now
> seems OK.
> I am having some trouble running the new script with #!/bin/sh.
> I get a list of files interspersed with the maths.

     The list of files is probably because a variable is not quoted;
     the quick solution is to add "set -f" at the beginning of the
     sript.

> I changed the last "echo" for a test and the maths is not being resolved.
>
> echo "$diff End"
> #echo ${diff#-}
>
> ## EOS
>
> sh days_between.cfj 07/01/2004 19/01/2004
> $(( $(( ( 1461 * ( 2004 + 4800 + ( 01 - 14 ) / 12 ) ) / 4 +
> ( 367 * ( 01 - 2 - 12 * ( ( 01 - 14 ) / 12 ) ) ) / 12 -
> ( 3 * ( ( 2004 + 4900 + ( 01 - 14 ) / 12 ) / 100 ) ) / 4 +
> 19 - 32075 )) - $(( ( 1461 * ( 2004 + 4800 + ( 01 - 14 ) /
> 12 ) ) / 4 +
> ( 367 * ( 01 - 2 - 12 * ( ( 01 - 14 ) / 12 ) ) ) / 12 -
> ( 3 * ( ( 2004 + 4900 + ( 01 - 14 ) / 12 ) / 100 ) ) / 4 +
> 07 - 32075 )) )) End

   Are you sure that's a Korn shell?

   If it's not, you should get a syntax error (Bourne shell):

$ echo $(( 3 + 4 ))
syntax error: `(' unexpected

   The expression is correct, at least.
   It evaluates on all the shells I've tried, including ksh88, ksh93
   pdksh, ash, bash....

$ echo $(( $(( ( 1461 * ( 2004 + 4800 + ( 01 - 14 ) / 12 ) ) / 4 +
      ( 367 * ( 01 - 2 - 12 * ( ( 01 - 14 ) / 12 ) ) ) / 12 -
      ( 3 * ( ( 2004 + 4900 + ( 01 - 14 ) / 12 ) / 100 ) ) / 4 +
      19 - 32075 )) - $(( ( 1461 * ( 2004 + 4800 + ( 01 - 14 ) / 12 ) ) / 4 +
      ( 367 * ( 01 - 2 - 12 * ( ( 01 - 14 ) / 12 ) ) ) / 12 -
      ( 3 * ( ( 2004 + 4900 + ( 01 - 14 ) / 12 ) / 100 ) ) / 4 +
      07 - 32075 )) ))
12

   Try a simple addition:

echo $(( 4 + 4 ))

   Or even:

eval echo $(( 4 + 4 ))

   The list of files is coming from expansion of the asterisks in the
   arithmetic expression, which shouldn't be there at this point.

   The leading zeroes shouldn't present a problem in this instance,
   but let's get rid of them. Try this parse_date function:

parse_date()
{
  dt="$*"
  f1=${dt%%[!0-9]*}
  f2=${dt%[!0-9]*}
  f2=${f2#*[!0-9]}
  f3=${dt##*[!0-9]}
  case ${DMY:=213} in
      123) d=${f1#0}; m=${f2#0}; y=$f3 ;;
      213) d=${f2#0}; m=${f1#0}; y=$f3 ;;
      321) d=${f3#0}; m=${f2#0}; y=$f1 ;;
      312) d=${f3#0}; m=${f1#0}; y=$f2 ;;
      132) d=${f1#0}; m=${f3#0}; y=$f2 ;;
      231) d=${f2#0}; m=${f3#0}; y=$f1 ;;
      *) return 1 ;;
  esac
}

-- 
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2004, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License


Relevant Pages

  • Re: Breaking up a string
    ... On Tue, 28 Oct 2003 at 21:52 GMT, DA Singh wrote: ... > In korn shell, ... My code in this post is copyright 2003, Chris F.A. Johnson and may be copied under the terms of the GNU General Public License ...
    (comp.unix.shell)
  • Re: File name substitution
    ... On Tue, 08 Apr 2003 at 02:33 GMT, Michael Acosta wrote: ... > If you're using Korn shell, ... My code in this post is copyright 2003, Chris F.A. Johnson ...
    (comp.unix.shell)
  • Re: OED
    ... Mike Ruddock wrote: ... >problems using equations and these can be solved by various pure maths ... There are so many phrases which bring back memories of maths (or ... Chris J Dixon Nottingham ...
    (uk.media.radio.archers)
  • Re: Ksh -- heres what I tried but failed to add integers in a file ...
    ... On 2004-06-09, Rafley wrote: ... >> If you think the problem is in the arithmetic expression, ... But this is the message I get when I try to run the script: ... My code in this post is copyright 2004, Chris F.A. Johnson ...
    (comp.unix.shell)
  • Re: School ads
    ... Cross product does not commute. ... I gotta keep up with these new maths. ...
    (rec.sport.football.college)