coreutils
[Top][All Lists]
Advanced

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

Re: [PATCH] sleep: allow ms suffix for milliseconds


From: Kaz Kylheku (Coreutils)
Subject: Re: [PATCH] sleep: allow ms suffix for milliseconds
Date: Fri, 29 Nov 2019 12:58:33 -0800
User-agent: Roundcube Webmail/0.9.2

On 2019-11-29 09:38, Bernhard Voelker wrote:
On 2019-11-29 14:30, Rasmus Villemoes wrote:
When one wants to sleep for some number of milliseconds, one can
do (at least in bash)

  sleep $(printf '%d.%03d' $((x/1000)) $((x%1000)))

but that's a bit cumbersome.

Why not use floating-point numbers directly?

  $ sleep 0.01234

I think the point is that the above example is doing exactly that,
but it has to convert from a value x which is in milliseconds.
The shell has only integer arithmetic, so a clumsy expression is
required.

If the shell had floating arithmetic, it would just be this:

   sleep $((x / 1000))

With GNU dc we can do:

   sleep $(dc -e "3k $x 1000/p")


Calling sleep(1) with a small milliseconds argument seems anyway a very
rough hammer, because the overhead to launch the executable is larger
than the actual nanosleep(2) system call.

Well, nobody says that the x value is in the range [0, 1000).

Sleeping for 15500 milliseconds is valid.

But in any case, we can already do that with "sleep 15.500".

The issue is that it's cumbersome to convert from 15500 to 15.500
in a shell script.

That's the problem to fix.

Next time someone will need another such conversion in another
context, and then yet another; we can't be adding units suffixes
into every utility that takes numeric arguments.

Fix the right problem in the right place.

That goes especially for issues that aren't blockers; there is
no urgency to address this problem with a quick fix like "sleep 123m"
because the cumbersome shell code works fine.




reply via email to

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