Bash睡眠(以毫秒为单位) [英] Bash sleep in milliseconds

查看:118
本文介绍了Bash睡眠(以毫秒为单位)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个计时器,该计时器将以毫秒为单位.我试图在脚本中使用 sleep 0.1 命令,但看到此错误消息:

I need a timer which will work with milliseconds. I tried to use sleep 0.1 command in a script, but I see this error message:

语法错误:无效的算术运算符(错误标记为".1")

在终端中运行 sleep 0.1 时,效果很好.

When I run sleep 0.1 in terminal it works fine.

请帮助我!

抱歉,我犯了一个错误:

Sorry I have made a mistake:

function timer
{
while [[ 0 -ne $SECS ]]; do
    echo "$SECS.."
    sleep 0.1
    SECS=$[$SECS-0.1]
done
}

sleep 0.1 行是第5行,而 SECS = $ [$ SECS-0.1] 行是第6行.我只是乱码.问题出在第六行,因为bash不能使用浮点数.我将功能更改如下:

Line sleep 0.1 was 5th and SECS=$[$SECS-0.1] was 6th. I just garbled lines. The problem was in 6th line, because bash can't work with float numbers. I changed my function as below:

MS=1000
function timer
{
while [[ 0 -ne $MS ]]; do
    echo "$SECS.."
    sleep 0.1
    MS=$[$MS-100]
done
}

推荐答案

确保您在Bash中运行脚本,而不是/bin/sh .例如:

Make sure you're running your script in Bash, not /bin/sh. For example:

#!/usr/bin/env bash
sleep 0.1

换句话说,尝试显式指定外壳.然后通过以下方式运行: ./foo.sh bash foo.sh .

In other words, try to specify the shell explicitly. Then run either by: ./foo.sh or bash foo.sh.

如果 sleep 是别名或函数,请尝试将 sleep 替换为 \ sleep .

In case, sleep is an alias or a function, try replacing sleep with \sleep.

这篇关于Bash睡眠(以毫秒为单位)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆