[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 14:51:25 +0200 |
On Thu, Jun 04, 2020 at 02:39:40PM +0200, Pier Paolo Grassi wrote:
> [[ SECONDS -gt 0 ]] && sleep "$SECONDS"
>
> you are missing a $ sigil here, it should be:
>
> [[ $SECONDS -gt 0 ]] && sleep "$SECONDS"
No, sorry, I don't. The [[ -gt ]] test is performed in an arithmetic
context. In arithmetic contexts, variables don't generally need an $ on
them:
$ n=12
$ [[ n -gt 10 ]] && echo greater than 10
greater than 10
$ [[ n -gt 15 ]] && echo greater than 15
>
> Il giorno gio 4 giu 2020 alle 12:14 Andreas Kusalananda Kähäri <
> andreas.kahari@abc.se> ha scritto:
>
> > I wrote a small script to delete many hundreds of thousands of files
> > off a very slow disk. The script is executed from find(1) and is
> > given pathnames of directories to delete as arguments. If a directory
> > takes a second or more to delete a directory, the script sleeps for
> > the amount of time it took to delete that directory before proceeding,
> > in an attempt to let the filesystem sync metadata (this may have been
> > misguided but it's not really important).
> >
> > #!/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. Or, that I have misunderstood something about how SECONDS is
> > supposed to work. Or, all of the above (please confirm).
> >
> > A sample script (see below) reproduces this with a tight loop on macOS
> > Catalina running "5.0.17(1)-release (x86_64-apple-darwin19.4.0)"
> > (bash from Homebrew) and on OpenBSD 6.7 running "5.0.17(1)-release
> > (x86_64-unknown-openbsd6.7)".
> >
> > while true; do SECONDS=0; [[ SECONDS -gt 0 ]] && echo hi; done
> >
> > (will print "hi" more or less once a second, and possibly slightly less
> > often occasionally)
> >
> > So, I don't know whether this is an error in the documentation, my
> > understanding of it, or in how the SECONDS variable is set/managed
> > internally by the shell. I'm assuming I'm misunderstanding.
> >
> > For reference (from the bash manual):
> >
> > SECONDS
> > Each time this parameter is referenced, the number
> > of seconds since shell invocation is returned. If a
> > value is assigned to SECONDS, the value returned upon
> > subsequent references is the number of seconds since
> > the assignment plus the value assigned. If SECONDS is
> > unset, it loses its special properties, even if it is
> > subsequently reset.
> >
> >
> >
> > --
> > Andreas (Kusalananda) Kähäri
> > SciLifeLab, NBIS, ICM
> > Uppsala University, Sweden
> >
> > .
> >
> > --
> Pier Paolo Grassi
--
Andreas (Kusalananda) Kähäri
SciLifeLab, NBIS, ICM
Uppsala University, Sweden
.
- SECONDS=0 does not reset SECONDS, or I'm missing something, Andreas Kusalananda Kähäri, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Pier Paolo Grassi, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something,
Andreas Kusalananda Kähäri <=
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Steve Amerige, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Andreas Kusalananda Kähäri, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Andreas Kusalananda Kähäri, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, dan braun, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Pier Paolo Grassi, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Andreas Kusalananda Kähäri, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Greg Wooledge, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Pier Paolo Grassi, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Andreas Kusalananda Kähäri, 2020/06/04
- Re: SECONDS=0 does not reset SECONDS, or I'm missing something, Pier Paolo Grassi, 2020/06/04