How to wrap a command to measure its elapsed time?
Currently I do it using eval:
do_cmd_named()
{
local name=$1
local cmd=$2
echo "$name"
local start_time=$(date +%s)
eval "$cmd 2>&1"
local exit_status=$?
local end_time=$(date +%s)
local elapsed_time_sec=$((end_time-start_time))
local elapsed_time_min_sec=$(date -ud "@$elapsed_time_sec" +'%M:%S')
if [[ $exit_status -ne 0 ]]
then
echo "$name failed with exit status $exit_status (elapsed time $elapsed_time_min_sec)"
return $exit_status
else
echo "$name done (elapsed time $elapsed_time_min_sec)"
fi
}
job()
{
sleep 1
}
do_cmd_named "do job" "job"
which leads to:
do job
do job done (elapsed time 00:01)
For my cases this approach almost works. However, this approach is considered bad because it violates some rules from BashFAQ. For example, "don't put code inside variables" from BashFAQ #50 (see also BashFAQ #48).
So, the question is: how to do it correctly?
timebe good enough? See How to get execution time of a script effectively? and How to measure time of program execution and store that inside a variable.cmd1 | cmd2orwhile cmd1; do cmd2; done? 1, 2 and 3 for each of cmd1 and cmd2 or 1, 2 and 3 for the whole construct?