Using bc in bash script

Jez Hancock jez.hancock at munk.nu
Thu Aug 14 10:34:28 PDT 2003


On Thu, Aug 14, 2003 at 12:23:34PM -0500, Stephen Hilton wrote:
> On Thu, 14 Aug 2003 12:11:55 -0500
> "Charles Howse" <chowse at charter.net> wrote:
> 
> > > Charles,
> > > 
> > > This will set bc precision to 5 decimal places:
> > > 
> > > et=`echo "scale=5 ; $end_time - $start_time" | bc`
> > 
> > Ohhh, I was really hoping on that one...but no, it still reports 0
> > seconds.
> 
> 
> Sorry I jumped the gun there, the scale is needed for this to work 
> but the "date +%s" willonly resolve into whole seconds after reading 
> the date man page.
> 
> I sure am curious as to how to solve this also, the /usr/bin/time 
> command man page says this:
> 
> -----------------snip------------------
> DESCRIPTION
>      The time utility executes and times the specified utility.  After the
>      utility finishes, time writes to the standard error stream, (in seconds):
>      the total time elapsed, the time used to execute the utility process and
>      the time consumed by system overhead.
> -----------------snip------------------
> 
> So that looks like seconds only also.
The precision is in hundredths of a second as I understand it from
playing with time(!):

#!/bin/sh
time_file=tmp.time
time="time -a -o $time_file"
$time cat /var/log/messages >/dev/null 2>&1
$time cat /var/log/maillog >/dev/null 2>&1
awk '{sum+=$1}END{print sum}' $time_file
rm $time_file

which outputs:

[18:34:03] munk at users /home/munk# sh tmp.sh
0.01

This simple script just times each cat command and appends the output from
time to the $time_file, then prints out the sum of the first columns of
the time outputs found in the time file. 

Just an idea.
-- 
Jez

http://www.munk.nu/


More information about the freebsd-questions mailing list