On Sun, 26 Jun 2005, Peter Bako wrote:

> Ok, so this is not really an OpenBSD question but I am doing this on an
> OpenBSD system and I am about to lose my mind...
> 
> I have done some basic shell scripting before but I've not had to deal with
> actual integer math before and now it is killing me.  The script takes a
> parameter in (year number) and is supposed to subtract 1900 from it and then
> multiply the result by 365.  (This is part of a larger script that deal with
> converting dates to a single numeric value, but this one problem is an
> example of the problems I am having with this entire script.)  So, this is
> what I have:
> 
> #!/bin/sh
> month=$1 
> day=$2
> year=$3
> 
> dayscount=$(expr ($year - 1900) * 365)
> echo $dayscount
> exit
> 
> This will generate a "syntax error: `$year' unexpected" error.  I have tried
> all sorts of variations and I am not getting it!!!  HELP!!!

When using ksh, you can do:

#!/bin/ksh
month=$1 
day=$2
year=$3

dayscount=$((($year - 1900) * 365))
echo $dayscount
exit

When using sh, you'll need expr(1), for which all parts of the
expression are separate arguments, and you need to escape all special
shell chars:

#!/bin/sh
month=$1 
day=$2
year=$3

dayscount=`expr \( $year - 1900 \) \* 365`
echo $dayscount
exit

> BTW, obviously I need a good book on SH programming.  Any suggestions?

For ksh, the Korn Shell Book by David Korn and (iirc Morris Bolsky)
comes to mind.

        -Otto

Reply via email to