Using bc in bash script
Joshua Oreman
oremanj at get-linux.org
Thu Aug 14 12:02:06 PDT 2003
On Thu, Aug 14, 2003 at 12:58:01PM -0500 or thereabouts, Stephen Hilton wrote:
> On Thu, 14 Aug 2003 18:34:25 +0100
> Jez Hancock <jez.hancock at munk.nu> wrote:
>
> > 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,
>
> Your shell script works fine for me, resolving to 100th's of a second.
>
> Looks like a good answer for Charles :-)
>
> I still am wondering why the date command does not have a format
> string for seconds (down to 100th's) like "+%ss" and also why
> the time command stops at 100th's when other programs resolve
> time to 5 or 6 decimal places ?
All the good % things are taken :-)
Here are three ways of doing it:
% cat > gettimeofday.c <<'EOF'
#include <stdio.h>
#include <sys/time.h>
int main() {
struct timeval tv;
struct timezone unused;
gettimeofday (&tv, &unused);
printf ("%li.%li\n", tv.tv_sec, tv.tv_usec);
return 0;
}
EOF
% cc -o gettimeofday gettimeofday.c
% ./gettimeofday; echo hello, world; ./gettimeofday
1060886109.667054
hello, world
1060886109.687446
% gettimeofday() {
> perl -MTime::HiRes=gettimeofday -e '($sec, $usec) = gettimeofday(); print $sec, ".", $usec, "\n"'
> }
% gettimeofday; echo hello, world; gettimeofday
1060886661.274900
hello, world
1060886661.313071
% gettimeofday2() {
> perl <<'EOF'
> $now = pack ("LL", ());
> syscall (116, $now, 0) != 1 or die "gettimeofday: $!";
> @now = unpack ("LL", $now);
> print $now[0], ".", $now[1], "\n";
> EOF
> }
% gettimeofday2; echo hello, world; gettimeofday2
1060887546.767676
hello, world
1060887546.938097
% rm gettimeofday gettimeofday.c
% unset gettimeofday
% unset gettimeofday2
The first one (the C program) works anywhere but you have to compile it.
The second one (perl -MTime::HiRes...) works if you have either Time::HiRes from CPAN or perl>=5.8.
The third one (perl ... syscall 116 ...) is specific to FreeBSD/i386 and a bit slower, but it works.
HTH
-- Josh
>
> Thanks for sharing the info,
>
> Stephen Hilton
> nospam at hiltonbsd.com
> _______________________________________________
> freebsd-questions at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-questions
> To unsubscribe, send any mail to "freebsd-questions-unsubscribe at freebsd.org"
More information about the freebsd-questions
mailing list