问题描述
我需要一个以毫秒为单位的计时器.我尝试在脚本中使用 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 行.我只是乱码.问题出在第 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
换句话说,尝试明确指定 shell.然后运行: ./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 睡眠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!