bash 'time' function

Chris F.A. Johnson c.f.a.johnson at rogers.com
Thu Dec 20 22:26:26 PST 2001


On Thu, 20 Dec 2001, Soren Jacobsen wrote:

> On Thu, Dec 20, 2001 at 11:39:52PM -0500, Chris F.A. Johnson wrote:
> > On Fri, 21 Dec 2001, R. Bosch wrote:
> >
> > 	{ time sleep 3s } 2> >( grep sys )
>
> You need a semicolon in a group command like that.

Quite right. It should be

	{ time sleep 3s; } 2> >( grep sys )

>    Where are you
> redirecting stderr? Perhaps you meant 2>&1. Why are you redirecting
> stdout to grep in a subshell? Did you mean to pipe it? If so, why the
> subshell?

No, I am redirecting stderr, and using process substitution.

man bash:

   Process Substitution
       Process substitution is supported on systems that  support
       named  pipes  (FIFOs) or the /dev/fd method of naming open
       files.  It takes the form of <(list) or >(list).  The pro­
       cess  list  is run with its input or output connected to a
       FIFO or some file in /dev/fd.  The name of  this  file  is
       passed as an argument to the current command as the result
       of the expansion.  If the >(list) form is used, writing to
       the file will provide input for list.  If the <(list) form
       is used, the file passed as an argument should be read  to
       obtain the output of list.


> { time yourprogram &>/dev/null } 2>&1 | grep sys
>
> soren:~$ { time ls -R /usr/local &>/dev/null; } 2>&1 | grep sys
> sys     0m0.600s

If the command sends anything to stdout, this will lose it, or give
unwanted output if it happens to contain the string "sys".


Of course, it is much simpler to use the TIMEFORMAT variable:

       TIMEFORMAT
              The  value  of  this  parameter is used as a format
              string specifying how the  timing  information  for
              pipelines  prefixed  with  the  time  reserved word
              should be displayed.  The % character introduces an
              escape sequence that is expanded to a time value or
              other information.  The escape sequences and  their
              meanings are as follows; the braces denote optional
              portions.
              %%        A literal %.
              %[p][l]R  The elapsed time in seconds.
              %[p][l]U  The number of CPU seconds spent  in  user
                        mode.
              %[p][l]S  The number of CPU seconds spent in system
                        mode.
              %P        The CPU percentage, computed as (%U + %S)
                        / %R.

              The optional p is a digit specifying the precision,
              the number of fractional  digits  after  a  decimal
              point.   A  value  of  0 causes no decimal point or
              fraction to be output.  At most three places  after
              the  decimal  point  may  be specified; values of p
              greater than 3 are changed to 3.  If p is not spec­
              ified, the value 3 is used.

              The optional l specifies a longer format, including
              minutes, of the form MMmSS.FFs.   The  value  of  p
              determines whether or not the fraction is included.

              If this variable is not set, bash acts as if it had
              the value $'\nreal\t%3lR\nuser\t%3lU\nsys%3lS'.  If
              the value is null, no timing  information  is  dis­
              played.   A trailing newline is added when the for­
              mat string is displayed.

-- 
	Chris F.A. Johnson                              bq933 at torfree.net
	=================================================================
	c.f.a.johnson at rogers.com                http://cfaj.freeshell.org
	cfaj at freeshell.org        http://members.rogers.com/c.f.a.johnson

-- 
Unsubscribe: send email to listar at linuxfromscratch.org
and put 'unsubscribe blfs-support' in the subject header of the message



More information about the blfs-support mailing list