Re: days-between
From: Chris F.A. Johnson (c.fa.johnson_at_rogers.com)
Date: 01/07/04
- Next message: those who know me have no need of my name: "Re: How to write file names with cyrill characters?"
- Previous message: Ted Breen: "Re: days-between"
- In reply to: Ted Breen: "Re: days-between"
- Next in thread: Ted Breen: "Re: days-between"
- Reply: Ted Breen: "Re: days-between"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: those who know me have no need of my name: "Re: How to write file names with cyrill characters?"
- Previous message: Ted Breen: "Re: days-between"
- In reply to: Ted Breen: "Re: days-between"
- Next in thread: Ted Breen: "Re: days-between"
- Reply: Ted Breen: "Re: days-between"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|