help with expr command in script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help with expr command in script
# 1  
Old 02-26-2010
help with expr command in script

Hi,

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.

Code:
 
function get_elapsed_time
{
export PROPS_FILE=temp.properties
export CURRENT_TIME=`date '+%T'`
export PREVIOUS_TIME=`grep ABC $PROPS_FILE |cut -c15-22`
echo $PREVIOUS_TIME
echo START_HR=`echo "$PREVIOUS_TIME" | awk '{print substr($0,1,2)}'`
echo $START_HR
echo END_HR=`echo "$CURRENT_TIME" | awk '{print substr($0,1,2)}'`
echo $END_HR
export START_MINS=`echo \`expr $START_HR \* 60\``
echo $START_MINS
}

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

Please can you advise.
Thanks

Last edited by Nutan; 02-26-2010 at 01:52 PM.. Reason: missed one line
# 2  
Old 02-26-2010
We don't need the echo.

Code:
export START_MINS=`expr $START_HR \* 60`

# 3  
Old 02-26-2010
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

Code:
echo "1+2"|bc

# 4  
Old 02-26-2010
Quote:
Originally Posted by methyl
We don't need the echo.

Code:
export START_MINS=`expr $START_HR \* 60`


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:
Code:
export START_MINS=$(( ${START_HR#0} * 60 ))


Last edited by cfajohnson; 02-27-2010 at 12:20 AM..
# 5  
Old 02-27-2010
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
Code:
#!/usr/bin/ksh93
#
# diffdate "Tue, Feb 19, 2008 08:00:02 PM" "Wed, Feb 20, 2008 02:19:09 AM"
#

SDATE=$(printf '%(%s)T' "$1")
FDATE=$(printf '%(%s)T' "$2")

[[ $# -ne 2 ]] && {
   print "Usage: diffdate start-date finish-date"
   exit 1
}

DIFF=$(($FDATE-$SDATE))

SECS=$(($DIFF % 60))
MINS=$(($DIFF % (60 * 60) / 60))
HOURS=$(($DIFF / (60 * 60)))

printf "%02d:%02d:%02d\n" $HOURS $MINS $SECS

exit 0

# 6  
Old 02-28-2010
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).
Code:
(( SECS= DIFF % 60                  ))
(( MINS= DIFF % (60 * 60) / 60  ))
(( HOURS= DIFF / (60 * 60)        ))

Posix version need command substitution ex. in dash, works also in ksh, bash, ...
Code:
SECS=$((  DIFF % 60                  ))
MINS=$((  DIFF % (60 * 60) / 60  ))
HOURS=$(( DIFF / (60 * 60)        ))

And if you not use ksh (prev. printf using), then use ex. gnu date. Take epoc time and calculate.
Code:
...
SDATE=$( date -d "$1" '+%s' )
FDATE=$( date -d "$2" '+%s' )
(( DIFF = FDATE - SDATE ))
...


Last edited by kshji; 02-28-2010 at 12:11 PM..
# 7  
Old 02-28-2010
Quote:
Originally Posted by kshji
No need to use command substitute and/or expr or bc. Use builtin, it's enough.
Ksh, bash, zsh, dash, ...

This code will not work in dash; it is not POSIX syntax.
Quote:
Code:
(( SECS= DIFF % 60                  ))
(( MINS= DIFF % (60 * 60) / 60  ))
(( HOURS= DIFF / (60 * 60)        ))


The POSIX syntax, which will work in all the shells you mentioned, is:
Code:
SECS=$((  $DIFF % 60   ))
MINS=$((  $DIFF % (60 * 60) / 60  ))
HOURS=$(( $DIFF / (60 * 60) ))

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Problem with expr command in shell script

Hi, I have used expr command to increment the date. for e.g., case 1 : echo $(date -d $(echo `expr 20010101 + 1`)) it returns Tue Jan 2 00:00:00 IST 2001 case 2: echo $(date -d $(echo `expr 20010101 - 1`)) it returns date: invalid date `20010100' please suggest me, how to... (3 Replies)
Discussion started by: nanthagopal
3 Replies

2. Shell Programming and Scripting

expr command help

I'm trying to check if a variable'd string is only one character and use that in an if statement the only way I could find is: $expr "${var}" : . # expr STRING : regrep where the "." is the grep wildcard for any single character. Whats wrong with my code here and is there a... (3 Replies)
Discussion started by: Tewg
3 Replies

3. UNIX for Dummies Questions & Answers

substring without using expr command

Hi guys, For some reason the terminal on my mac does not let me run string manipulations commands using the expr command. I'm not sure how to fix this so I'm requesting a "work-around" to using the expr command... This is the string I'm working with: "neo_opls01_1.log" And I'm trying to... (9 Replies)
Discussion started by: ah7391
9 Replies

4. Shell Programming and Scripting

expr command

Hi Can anyone explain me the usage of this command and the arguments used here and what will be the expected output : v_num=`expr nav_d_20100204_1759 : '*\(*\)'` what will be the value returned in v_num. Thanks in Advance!!! Regards Naveen Purbia (3 Replies)
Discussion started by: trying_myluck
3 Replies

5. UNIX for Dummies Questions & Answers

using the expr command

Hi friends how can i execute expr $va1 * $var2 provided i m not supposed to use '/' also the nglob variable is turned off. (4 Replies)
Discussion started by: ashishj
4 Replies

6. UNIX for Dummies Questions & Answers

problem with expr command

:) hi Unix gurus, Pls consider the following piece of code str='hello' length=echo $str|wc -c echo $length y= ` expr \( 80 - $length \) ` echo $y :confused: The last echo stmt is displaying 0 as the result. If i put direct value like 6 instead of $length in i 3rd stmt it is giving... (8 Replies)
Discussion started by: ravi raj kumar
8 Replies

7. UNIX for Dummies Questions & Answers

expr command

hi guys.... i hava a command expr... where i m adding a value in a loop like Tc=`expr $Tc\+ $l` where Tc is declred as a variable and every time l contains a new vaue if Tc =0 initially and l =2 Tc should be equal to 0+ 2 and then l = 4 Tc = 2+4 and dispaly as 6 but after... (5 Replies)
Discussion started by: madhu_aqua14
5 Replies

8. Shell Programming and Scripting

about accecing `expr` command

Hi All, I have created 85 users on Linux server. I use bsh shell for all users.But after any user get logged in on unix,he/she is not able to access the expr command.So anyone can tell me that if i have to include some files to access expr command.Because when i write-> expr 6+2 on $ prompt i'll... (1 Reply)
Discussion started by: bhumi
1 Replies

9. UNIX for Dummies Questions & Answers

Problems with expr command

Hi All, I might be making a silly mistake but I need ur help. I have initialized various variables: cur_month=`date +%m` cur_year=`date +%y` last_year=`expr $cur_year \- 1` It works fine for cur_month & cur_year....but has problems with last_year. FOR LAST YEAR IT GIVES A FOLLOWING... (2 Replies)
Discussion started by: rooh
2 Replies

10. UNIX for Dummies Questions & Answers

expr command

I am looking for the correct syntax on the expr command in UNIX. I have a script that I am building at the moment. the script is creating file1 that is an actual .sql file that is going inside the oracle database to get some information in there. It take that information, puts it inside another... (2 Replies)
Discussion started by: wolf
2 Replies
Login or Register to Ask a Question