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: Thu, 4 Jun 2020 15:52:50 +0200

On Thu, Jun 04, 2020 at 09:32:45AM -0400, dan braun wrote:
> Andreas,
> 
> I have found that many times operations take much less than a second to
> complete.

I might have been a bit imprecise in describing my issue.

Most of the time, removing the directories takes less than a second.
For these cases, I don't want to execute the sleep at all.  Only when a
call to rm takes more than one second do I want to sleep.

However, my loop calls sleep once every second, regardless of the fact
that no call to rm took more than a second to complete.

This is consistent with the SECONDS parameter being set to current
second according to the system's clock, the rm running (taking, say 0.2
seconds), the system's clock ticking into a new full second, and SECONDS
being 1 when I test it, even though I set it to 0 only 0.2 (or whatever)
seconds ago.

I took the liberty of testing my code in zsh too, and there the SECONDS
parameter behaves like a proper timer.  This means that the [[ SECONDS
-gt 0 ]] test will *never* be true unless the call to rm takes more than
a second.  I.e., if I set SECONDS to 0 at 12:34:45.6 and test it at
12:34:46.2, in zsh its value would be 0 while it *appears* as if in bash
it will be 1 (even though less than a second has passed).

My question is whether this analysis is correct and I just couldn't find
it in the bash manual, or whether it's a bug, or something else.


> Therefore a single second may not be enough resolution for the task.
> 
> I sometimes use the following;
> #nano seconds
> # date +%N
> 688264274

This is not relevant to my issue.  Also (on all my machines):

        $ date +%N
        N

        $ printf '%(%N)T\n' -1
        N

> 
> #epoch seconds
> # date +%s
> 1591274861
> 
> # sleep 500 milliseconds
> # sleep 0.500
> 
> Cheers,
> Dan
> --
> Dan Braun (dan @ tekvax . com )
> Broadcast Engineering
> Burlington, Ontario, Canada
> 
> 
> On Thu, 4 Jun 2020 at 09:03, Andreas Kusalananda Kähäri <
> andreas.kahari@abc.se> wrote:
> 
> > On Thu, Jun 04, 2020 at 03:00:40PM +0200, Kusalananda Kähäri wrote:
> > > On Thu, Jun 04, 2020 at 08:48:34AM -0400, Steve Amerige wrote:
> > > > On 6/4/2020 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.
> > > >
> > > > Note that your script doesn't have a semicolon after "for name":
> > > >
> > > > for name; do
> > > > ...
> > > > done
> > >
> > > That's because it doesn't need one.
> > >
> > > >From the manual:
> > >
> > >       for name [ [ in [ word ... ] ] ; ] do list ; done
> > >
> > > That is, "for name in do list; done" is valid.
> >
> > Sorry, I meant "for name do list; done" above, which is what I have in
> > my code.
> >
> >
> > --
> > Andreas (Kusalananda) Kähäri
> > SciLifeLab, NBIS, ICM
> > Uppsala University, Sweden
> >
> > .
> >
> >

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

.



reply via email to

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