You could use the time
executable (not the bash builtin because the external program features the format option -f
and delivers the time as [hours:]minutes:seconds
and I am not in the mood right now to wait an hour to find out how the builtin shows us the hours :) ) and awk like this (using the example sleep 2
):
/usr/bin/time -f "%E" sleep 2 2> >(
awk -F : '
{ if(FN>2) printf("%02d hours %02d minutes %02d seconds\n", $1, $2, $3)
else printf("%02d minutes %02d seconds\n", $1, $2)
}'
)
Here we use /usr/bin/time
with its -f
option. Then we pipe the output of stderr into awk
splitting the string at :
. (time writes to stderr, thus we need the 2>
to redirect stderr into the >( awk ... )
filter.
The awk filter decides on the number of fields in NF
what printf
statement it is using.