I am trying to code a unix function to calculate date difference between two date variables. I am stuck at a point where I am trying to convert hours into minutes. Below is the code I am doing.
Now the PREVIOUS_TIME will have value as say "26-Feb-10 08:16:23".
So the START_HR will have value as "08" and simlarly END_HR will have value as say "15".
The START_MINS should be like 08*60 which is 480. But when trying to run this temp.sh file i get the below error,
START_HR=08
END_HR=15
expr: syntax error
But if I run the same commands individually on the prompt, I get proper result. Its only while running the ksh script that the error is thrown.
#!/usr/bin/ksh
THe same command below runs perfectly fine on individual prompt
$export START_MINS=`echo \`expr $START_HR \* 60\``
$echo $START_MINS
480
May I suggest using bc to do math calculation instead of expr? I am not suggesting that expr may be your problem. In fact i dont the solution to your problem, but I think to math stuff, I usuall use bc, for e.g
You don't need expr (or bc or awk), but you do need to remove the leading zero or the number will be taken as octal, and 08 is not a valid octal number:
You indicated that you are using the Korn shell. If it is the latest version of the Korn shell rather than ksh88 or pdksh, the following script may help you
No need to use command substitute and/or expr or bc. Use builtin, it's enough.
Ksh, bash, zsh, ... but not ex. in dash. (posix not include this, so using ex. dash you need command substitution - thanks for cfajohnson).
Hi All,
Thanks for so many suggestions.
Removing echo, does not work as it still gives syntax error.
Removing the leading zero also does not work. This gives an error as *60 not found. Since * is used as wild card.
I am using ksh, so I guess date -d won't work in ksh.
If i use the printf command, the date is not calculated. The error is thrown as
The output is like below.
1-Mar-10 03:56:51
START_TIME=()T
END_TIME=()T
()T-()T: syntax error
date is not builtin command, it's in command in your system. -d option is in gnu date, not ex. in bsd date.
printf is builtin command in ksh93. So man ksh93 include documentation to use printf.
You don't need export in your code. Local variables is enough. Environment variables (export variable) are needed if you call subprocess and subprocess need those variables value also.
If you like to use ksh93, check download. Maybe your system is still supported.