[plug] Elapsed time in scripts

Onno Benschop onno at itmaze.com.au
Wed Jan 3 11:48:56 AWST 2024


In a bash script you could use something like:

time (
    echo "Hello World"
    find .
    echo "Done"
)

On Wed, 3 Jan 2024 at 11:40, William Kenworthy <bill at kenworthy.id.au> wrote:

> The time utility might give you options?
>
> "man time"
>
> BillK
>
>
> On 3/1/24 11:04, Brad Campbell wrote:
> > G'day mobsters..
> >
> > Quite often I find myself wanting to time tasks in scripts. I used to
> use all sorts of if/then maths to break up seconds to clock friendly
> formats.
> > I had a thought perhaps I could make date do it for me. Turns out the
> Epoch was a Thursday, so we need to fast forward 3 days to get a workable
> day count (start on day 0).
> >
> > This gives elapsed time in days:hours:minutes:seconds. However it only
> works for tasks that run less than 7 days or the day counter wraps.
> >
> > START=`date +%s`
> >
> > // insert task here
> >
> > END=`date +%s`
> > echo Task took `date -d @$((END-START+259200)) -u +%w:%H:%M:%S`
> >
> > Just to point out another ugly, ugly bruteforce way I've used in the
> past :
> >
> > # ------------ How long did the task take? ---------------
> > END=`date +%s`
> > runtime=$(($END-$START))
> > let days=$(($runtime/86400))
> > let hrt=$(($runtime%86400))
> > let hours=$(($hrt/3600))
> > let mint=$(($hrt%3600))
> > let minutes=$(($mint/60))
> > let secsd=$((mint%60))
> > echo Task took `printf "%.2d:%.2d:%.2d:%.2d" $days $hours $minutes
> $secsd`
> >
> > A couple of the many, many ways to skin a cat. I like the former method,
> but I'm always interested to see alternatives.
> >
> > Regards,
> > Brad
> > _______________________________________________
> > PLUG discussion list: plug at plug.org.au
> > http://lists.plug.org.au/mailman/listinfo/plug
> > Committee e-mail: committee at plug.org.au
> > PLUG Membership: http://www.plug.org.au/membership
> >
> >
> _______________________________________________
> PLUG discussion list: plug at plug.org.au
> http://lists.plug.org.au/mailman/listinfo/plug
> Committee e-mail: committee at plug.org.au
> PLUG Membership: http://www.plug.org.au/membership
>


-- 
Onno Benschop

()/)/)()        ..ASCII for Onno..
|>>?            ..EBCDIC for Onno..
--- -. -. ---   ..Morse for Onno..

If you need to know: "What computer should I buy?" http://goo.gl/spsb66

ITmaze   -   ABN: 56 178 057 063   -  ph: 04 1219 8888   -
onno at itmaze.com.au
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.plug.org.au/pipermail/plug/attachments/20240103/22323714/attachment.html>


More information about the plug mailing list