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: Steve Amerige
Subject: Re: SECONDS=0 does not reset SECONDS, or I'm missing something
Date: Thu, 4 Jun 2020 08:48:34 -0400
User-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:68.0) Gecko/20100101 Thunderbird/68.8.1

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

Note that you're not using parameter expansion for SECONDS.
It should have been $SECONDS. You could have alternative used:

(( SECONDS > 0 ))

in which case the omission of the parameter expansion syntax is permitted.

Also, as a side note, if your script were running with set -o errexit,
the conditional idiom would fail because it doesn't handle the false case.
Better:

if (( SECONDS > 0 )); then
   sleep $SECONDS
fi




reply via email to

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