Using bc in bash script

Jez Hancock jez.hancock at munk.nu
Thu Aug 14 09:30:18 PDT 2003


On Thu, Aug 14, 2003 at 11:08:21AM -0500, Charles Howse wrote:
> > On Thu, Aug 14, 2003 at 10:46:45AM -0500, Charles Howse wrote:
> > > Hello List,
> > > 
> > > I've migrated from Redhat Linux 9 to FreeBSD 4.8-RELEASE, 
> > character mode
> > > - no gui.
> > > 
> > > I'm trying to calculate the number of seconds between 
> > $start_time and
> > > $end_time in a bash script.
> > > 
> > > Start_time=`date +%s` # Seconds past midnight at start of script
> > > [ do lots of stuff ]
> > > End_time=`date +%s`   # Seconds past midnight at end of script
> > > 
> > > Then I want to: et=`bc $end_time - $start_time` to get the number of
> > > seconds or fractions of seconds elapsed.
> > How about:
> > 
> > et=`echo "$end_time - $start_time" | bc`
> 
> Hi Jez, thanks for the reply!
> 
> As my daughter would say, "Well, DUHHHHHHH!"
:)

> That worked fine, it reported 0 seconds.
Hah, computers are just too damn fast :)

> Can I refine it to give me something like: .784 seconds?
You could use 'time' perhaps to time whatever it is you're timing - I'm
not overly familiar with the time(1) command though, best to check the man
page :)

-- 
Jez

http://www.munk.nu/


More information about the freebsd-questions mailing list