help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: SECONDS=0 does not reset SECONDS, or I'm missing something


From: Andreas Kusalananda Kähäri
Subject: Re: SECONDS=0 does not reset SECONDS, or I'm missing something
Date: Fri, 5 Jun 2020 00:01:08 +0200

On Thu, Jun 04, 2020 at 02:34:35PM -0400, Chet Ramey wrote:
> On 6/4/20 6:14 AM, Andreas Kusalananda Kähäri wrote:
> 
> >     #!/usr/local/bin/bash -x
> > 
> >     for name do
> >             SECONDS=0
> >             rm -r -f "$name"
> >             [[ SECONDS -gt 0 ]] && sleep "$SECONDS"
> >     done
> > 
> > What I noticed was that the script would go to sleep for a second
> > every second, even if the deletion of directories was quick.  This
> > indicates that SECONDS=0 didn't properly reset the SECONDS timer.  Or,
> > it indicates that the timer is set by the system's clock (the current
> > second according to the current time), and that it is not a real "timer"
> > at all. 
> 
> Yes, SECONDS is in seconds granularity, whether that is seconds since the
> epoch or seconds since an assignment to it, as you quoted below. There's
> no expectation of subsecond granularity -- it's just whatever the system
> tells the shell the current number of seconds is.
> 
> The next version will use the number of seconds returned from gettimeofday,
> but there's still nothing in there that attempts to round it up or down
> depending on tv_usec.

Oh, I'm not expecting subsecond granularity!  I'm expecting that

        SECONDS=0; sleep 0.5; echo $SECONDS

always outputs a zero, which it does not do now if the system's clock
rolls over to a new full second during the 0.5 seconds of sleep.

Another test: The following loops prints alternating zeros and ones
after the colon on each line

        SECONDS=0
        while true; do
                sleep 0.5
                printf '%(%T)T: %s\n' -1 "$SECONDS"
                SECONDS=0
        done

... while I'm expecting only zeroes.  I'm expecting zeros because the
manual says "If a value is assigned to SECONDS, the value returned upon
subsequent references is the number of seconds since the assignment
[...]", and each assignment to SECONDS in the code above happens about
half a second after the last one.

It's just a bit odd to have the SECONDS parameter tick with the system
time, which IMHO is not implied by the words "number of seconds since
the assignment", that's all.

-- 
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden

.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]