Bash:如何进行短暂的延迟?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7757655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Bash: How to make short delay?
提问by Raihan
How to make a short delay (for less than a second) in bash? The smallest time unit in sleep command is 1 sec. I am using bash 3.0 in SunOS 5.10.
如何在 bash 中进行短暂的延迟(不到一秒)?sleep 命令中的最小时间单位是 1 秒。我在 SunOS 5.10 中使用 bash 3.0。
采纳答案by Keith Thompson
SunOS (Solaris) probably doesn't have the GNU tools installed by default. You might consider installing them. It's also possible that they're already installed in your system, perhaps in some directory that isn't in your default $PATH. GNU sleepis part of the coreutils package.
SunOS (Solaris) 可能默认没有安装 GNU 工具。您可以考虑安装它们。也有可能它们已经安装在您的系统中,可能安装在某个不在您的默认$PATH. GNUsleep是 coreutils 包的一部分。
If you have Perl, then this:
如果你有 Perl,那么这个:
perl -MTime::HiRes -e 'Time::HiRes::usleep 500000'
should sleep for 500000 microseconds (0.5 second) -- but the overhead of invoking perl is substantial.
应该休眠 500000 微秒(0.5 秒)——但是调用 perl 的开销很大。
For minimal overhead, I'd write a small C program that calls usleep()or nanosleep(). Note that usleep()might not handle intervals greater than 1 second.
为了最小的开销,我会编写一个小的 C 程序,调用usleep()或nanosleep(). 请注意,usleep()可能无法处理大于 1 秒的间隔。
回答by Chriszuma
I don't know what version this was implemented in, but my version of sleep (v6.12) accepts decimals. sleep 0.5works.
我不知道这是在哪个版本中实现的,但我的 sleep (v6.12) 版本接受小数。sleep 0.5作品。
If yours is too old for that, a short python or C program would probably be your only solution.
如果你的程序太旧了,一个简短的 python 或 C 程序可能是你唯一的解决方案。
回答by Orwellophile
Write this to "usleep.c"
将此写入“usleep.c”
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv) {
usleep( atol( argv[1] ) );
}
And type
并输入
make usleep
./usleep 1000000
回答by Marc Volovic
A very very very simply pythonesque usleep in decimal second fractions. It is NOT very precise, and no error checking on command line args
一个非常非常非常简单的pythonesque usleep 十进制第二分数。它不是很精确,并且没有对命令行参数进行错误检查
#!/usr/bin/python
import sys
import time
if len(sys.argv) == 1:
sleepTime = 1.0
else:
sleepTime = str(sys.argv[1])
time.sleep(float(sleepTime))
回答by island_hopper
Have you tried looking at the man pages? It should have a way to do a delay that is less than a second, I am not a Linux machine right now so can't look it up for you.
您是否尝试查看手册页?它应该有一种方法可以延迟不到一秒,我现在不是 Linux 机器,所以无法为您查找。
回答by island_hopper
You can use usleep. Here's a link to the man page: http://linuxmanpages.com/man1/usleep.1.php
你可以使用usleep。这是手册页的链接:http: //linuxmanpages.com/man1/usleep.1.php

