Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
823 views
in Technique[技术] by (71.8m points)

timer - Bash sleep in milliseconds

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:

syntax error: invalid arithmetic operator (error token is ".1")

When I run sleep 0.1 in terminal it works fine.

Please help me!

EDIT: Sorry I have made a mistake:

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

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
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

#!/usr/bin/env bash
sleep 0.1

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...