I'm trying to get the elapsed time. The following gives me '1', however I would like it to give me 1.98333333. Where am I missing the boat?
#!/bin/sh
TIME1=`date +%s` echo $TIME1 sleep 119 TIME2=`date +%s` echo $TIME2
ELTIME=$[ ($TIME2 - $TIME1)/60 ] echo $ELTIME
On Sun, 2006-03-12 at 02:38 -0500, M. Lewis wrote:
I'm trying to get the elapsed time. The following gives me '1', however I would like it to give me 1.98333333. Where am I missing the boat?
#!/bin/sh
TIME1=`date +%s` echo $TIME1 sleep 119 TIME2=`date +%s` echo $TIME2
ELTIME=$[ ($TIME2 - $TIME1)/60 ] echo $ELTIME
Try using something that handles floating point numbers better:
ELTIME=$(echo "scale = 8; ($TIME2 - $TIME1)/60" | bc)
Paul.
On Sun, 2006-03-12 at 02:38 -0500, M. Lewis wrote:
I'm trying to get the elapsed time. The following gives me '1', however I would like it to give me 1.98333333. Where am I missing the boat?
#!/bin/sh
TIME1=`date +%s` echo $TIME1 sleep 119 TIME2=`date +%s` echo $TIME2
ELTIME=$[ ($TIME2 - $TIME1)/60 ]
"bc" has better precision. Try this instead:
ELTIME=$( echo "scale=10; ($TIME2 - $TIME1)/60" | bc -l)
Cheers, Ben
echo $ELTIME
Ben Stringer ===== ben@burbong.com ==================================
On Sun, 12 Mar 2006, M. Lewis wrote:
I'm trying to get the elapsed time. The following gives me '1', however I would like it to give me 1.98333333. Where am I missing the boat?
#!/bin/sh
TIME1=`date +%s` echo $TIME1 sleep 119 TIME2=`date +%s` echo $TIME2
ELTIME=$[ ($TIME2 - $TIME1)/60 ] echo $ELTIME
Do you need elapsed time _in_ your script or elapsed time _for_ your script/program? If the latter,
time myscriptname
will give you good numbers (see 'man time' for info on 'time' options) a lot easier.
If the former, look at the '%N' option to date (IOW: date '+%s.%N') (otherwise you have up to a full second error in the timing) and bc for floating point arithmetic.
#!/bin/sh
TIME1=`date '+%s.%N'` echo $TIME1 sleep 2 TIME2=`date '+%s.%N'` echo $TIME2 ELTIME=`echo "($TIME2 - $TIME1)/60" | bc -l` echo $ELTIME
Thanks Benjamin, Ben and Paul. I'll give this a whirl.
Thanks, M
Benjamin Franz wrote:
On Sun, 12 Mar 2006, M. Lewis wrote:
I'm trying to get the elapsed time. The following gives me '1', however I would like it to give me 1.98333333. Where am I missing the boat?
#!/bin/sh
TIME1=`date +%s` echo $TIME1 sleep 119 TIME2=`date +%s` echo $TIME2
ELTIME=$[ ($TIME2 - $TIME1)/60 ] echo $ELTIME
Do you need elapsed time _in_ your script or elapsed time _for_ your script/program? If the latter,
time myscriptname
will give you good numbers (see 'man time' for info on 'time' options) a lot easier.
If the former, look at the '%N' option to date (IOW: date '+%s.%N') (otherwise you have up to a full second error in the timing) and bc for floating point arithmetic.
#!/bin/sh
TIME1=`date '+%s.%N'` echo $TIME1 sleep 2 TIME2=`date '+%s.%N'` echo $TIME2 ELTIME=`echo "($TIME2 - $TIME1)/60" | bc -l` echo $ELTIME