[nmglug] Bash Scripting help

Karl Hegbloom karlheg at laclinux.com
Wed Mar 30 20:28:13 PST 2005


On Wed, 2005-03-30 at 18:08 -0700, Mars DeLapp wrote:
> Store the start time and caclulate total time at the end.

Another way to do that is to use the '/usr/bin/time' command.  In
Debian, it's not installed by default; you have to install a package to
have it.  It acts as a wrapper around a command, and times the execution
time.  The output format is adjustable, so you can make it parseable.
The documentation is in "info time".

I prefer to use $(..) rather than `...` for capturing command output
into a string.  The ` are not nestable, but the $() are.  IIRC, the
rules are that the output is split using the characters in the $IFS
variable, then pasted back together using the first character of $IFS.
Since IFS normally contains space, tab, newline, that means that the
output of the command is split into tokens at each space, tab, or line
break, and then all of it is put back together with single spaces in
between.  Leading and trailing spaces and newlines are thus dropped.

A useful magic trick is to have the output format print it like:

  WALLTIME=1234.00 OTHERTIME=whatever

... and then 'eval' that string in the shell, so that it sets those
environment variables.  Here's what I mean:

karlheg at journeyhawk:~ $ eval "$(echo BLAH=foo)"
karlheg at journeyhawk:~ $ echo $BLAH
foo






More information about the nmglug mailing list