Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bash sleep in milliseconds

Tags:

bash

sleep

timer

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 } 
like image 923
Noqrax Avatar asked Aug 25 '15 17:08

Noqrax


People also ask

How do you sleep for 5 seconds in bash?

sleep Command Bash Script Example Here is a simple example: #!/bin/bash echo "Hi, I'm sleeping for 5 seconds..." sleep 5 echo "all Done."

How do I sleep in a bash script?

How to Use the Bash Sleep Command. Sleep is a very versatile command with a very simple syntax. It is as easy as typing sleep N . This will pause your script for N seconds, with N being either a positive integer or a floating point number.

What is sleep 1 in shell script?

For example, sleep 1 means to introduce a delay of one second. Here are a few examples: sleep 1 # sleep 1 second (the default is seconds) sleep 1s # sleep 1 second sleep 1m # sleep 1 minute sleep 1h # sleep 1 hour sleep 1d # sleep 1 day. The sleep command actually has a bit more capability that what is shown here.


2 Answers

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.

like image 138
kenorb Avatar answered Sep 22 '22 16:09

kenorb


Some options:

read -p "Pause Time .5 seconds" -t 0.5 

or

read -p "Continuing in 0.5 Seconds...." -t 0.5 echo "Continuing ...." 
like image 21
Jose H. Rosa Avatar answered Sep 20 '22 16:09

Jose H. Rosa