Wednesday, December 15, 2010

Korn shell - Time (built-in command)

In this and some later blogs about I will look into a couple of Korn shell features, I'm only interested in Korn-shell features for scripts though, not in the interactive usage.
Today my topic is the

Time (built-in command)

The built-in time command has a nice feature which distinguishes it from the system commands /bin/time or /bin/timex or the Bourne shell built-in time. (I didn't check other shells built-in time).

You cannot just run it with an external command but also with a function.

f() {
    sleep $1
time f 62
will report
real    1m2.01s
user    0m0.00s
sys     0m0.00s
Note: the space is a tab (not a sequence of spaces as in timex).

A little sed editing will get the output format closer to timex:
(time f 62) 2>&1 | sed '/^real/,/^sys/ {
# this is: tab zero m to be replaced by tab
s/ 0m/ /
# replace m by colon
# remove trailing s

but still not quite (timex runs e.g. on Solaris 10)
timex sleep 62

real        1:02.02
user           0.00
sys            0.00
In later posts I will look into coprocesses and job control.

No comments:

Post a Comment