Re: SH programming

2005-06-27 Thread Otto Moerbeek
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



Re: SH programming

2005-06-27 Thread Tony
The following seems to work.

$ year=2005
$ foo=$(expr $year - 1900 )
$ dayscount=$(expr $foo \* 365 )
$ echo $dayscount
38325

Problems include an unescaped asterisk
man expr indicates that parentheses should work
but my playing with them seems to indicate otherwise.
---Correction:
$ dayscount=$(expr \( $year - 1900 \) \* 365 )
$ echo $dayscount
38325

Parens that are destined for expr instead of the shell must also be escaped.




-Original Message-
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of
Otto Moerbeek
Sent: Monday, June 27, 2005 2:08 AM
To: Peter Bako
Cc: misc@openbsd.org
Subject: Re: SH programming


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



Re: SH programming

2005-06-27 Thread Otto Moerbeek
On Mon, 27 Jun 2005 [EMAIL PROTECTED] wrote:

 The following seems to work.
 
 $ year=2005
 $ foo=$(expr $year - 1900 )
 $ dayscount=$(expr $foo \* 365 )
 $ echo $dayscount
 38325
 
 Problems include an unescaped asterisk
 man expr indicates that parentheses should work
   but my playing with them seems to indicate otherwise.
 ---Correction:
 $ dayscount=$(expr \( $year - 1900 \) \* 365 )
 $ echo $dayscount
 38325
 
 Parens that are destined for expr instead of the shell must also be escaped.

And this is almost exaclty the sh script I sent in my reply. The
escaping of parentheses is obviously needed to avoid them being
interpreted by the shell. That is standard shell programming stuff.

And please do not toppost.

-Otto

 
 
 
 
 -Original Message-
 From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Behalf Of
 Otto Moerbeek
 Sent: Monday, June 27, 2005 2:08 AM
 To: Peter Bako
 Cc: misc@openbsd.org
 Subject: Re: SH programming
 
 
 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



Re: SH programming

2005-06-27 Thread Dimitri
Try escaping the *

\*



Peter Bako wrote:
 Hum, I get a syntax error: '*' unexpected
 
 -Original Message-
 From: Michael Erdely [mailto:[EMAIL PROTECTED] 
 Sent: Sunday, June 26, 2005 6:20 PM
 To: Peter Bako
 Cc: misc@openbsd.org
 Subject: Re: SH programming
 
 
 On 6/26/05, Peter Bako [EMAIL PROTECTED] wrote:
 
dayscount=$(expr ($year - 1900) * 365)
 
 
 Try:
 dayscount=$((($year - 1900) * 365))



SH programming

2005-06-26 Thread Peter Bako
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!!!

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

Thanks,
Peter



Re: SH programming

2005-06-26 Thread Ted Unangst
On Sun, 26 Jun 2005, Peter Bako wrote:

 #!/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!!!

man sh says arithmetic expressions take double parens:

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

don't forget about leap years.

-- 
And that's why we need security.



Re: SH programming

2005-06-26 Thread Peter Bako
Hum, I get a syntax error: '*' unexpected

-Original Message-
From: Michael Erdely [mailto:[EMAIL PROTECTED] 
Sent: Sunday, June 26, 2005 6:20 PM
To: Peter Bako
Cc: misc@openbsd.org
Subject: Re: SH programming


On 6/26/05, Peter Bako [EMAIL PROTECTED] wrote:
 dayscount=$(expr ($year - 1900) * 365)

Try:
dayscount=$((($year - 1900) * 365))

-- 
http://erdelynet.com/

Support OpenBSD! http://www.openbsd.org/orders.html



Re: SH programming

2005-06-26 Thread Stephen Marley
On Sun, Jun 26, 2005 at 09:32:36PM -0400, Ted Unangst wrote:
 On Sun, 26 Jun 2005, Peter Bako wrote:
 
  #!/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!!!
 
 man sh says arithmetic expressions take double parens:
 
 dayscount=$((($year - 1900) * 365))
 
 don't forget about leap years.

Traditional Bourne shell doesn't have arithmetic substitutions so it
would be done with expr like this:

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

or even:

dayscount=`expr  \`expr $year - 1900\` \* 365`

This only matters if your script needs to be portable.

-- 
stephen



Re: SH programming

2005-06-26 Thread Rod.. Whitworth
On Sun, 26 Jun 2005 20:51:07 -0700, Peter Bako wrote:

Hum, I get a syntax error: '*' unexpected

IJWFM using sh or ksh on 3.7 i386 entering year and the calc line at
the prompt and echoing $daycount at the prompt.


-Original Message-
From: Michael Erdely [mailto:[EMAIL PROTECTED] 
Sent: Sunday, June 26, 2005 6:20 PM
To: Peter Bako
Cc: misc@openbsd.org
Subject: Re: SH programming


On 6/26/05, Peter Bako [EMAIL PROTECTED] wrote:
 dayscount=$(expr ($year - 1900) * 365)

Try:
dayscount=$((($year - 1900) * 365))

-- 
http://erdelynet.com/

Support OpenBSD! http://www.openbsd.org/orders.html



From the land down under: Australia.
Do we look umop apisdn from up over?

Do NOT CC me - I am subscribed to the list.
Replies to the sender address will fail except from the list-server.