58

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
}
Noqrax
  • 1,049
  • 2
  • 8
  • 15
  • 3
    What shell is running the script? Is it `/bin/sh` and not `/bin/bash`? Is the script even a shell script? – Etan Reisner Aug 25 '15 at 17:17
  • `sleep` isn't going to produce a syntax error. Please show a minimal script that produces your error. – chepner Aug 25 '15 at 17:24
  • Please add first line of your script and show how you start script. – Cyrus Aug 25 '15 at 17:27
  • 1
    @EtanReisner: That shouldn't matter. `sleep` is not a bash built-in, so `sleep 0.1` should work the same regardless of which shell you're using, as long as a suitable `sleep` command is in your `$PATH`. – Keith Thompson Aug 25 '15 at 17:57
  • Don't just show us the first line of the script. Show us the entire script. If possible, trim it down to a short self-contained version that produces the problem (if it's longer than 5 lines you probably haven't trimmed it enough). @Cyrus – Keith Thompson Aug 25 '15 at 17:58
  • does `/bin/sleep 0.1` in script work? – vlp Aug 25 '15 at 18:08
  • @vlp: Interesting, but I doubt that's the problem here. The linked question was about a problem with Windows-style CRLF line endings. The error message here doesn't indicate that kind of problem. We just won't know until the OP provides more information. (I can reproduce the error message with `sleep $((0.1))` but I don't think that's what the OP is doing.) – Keith Thompson Aug 25 '15 at 18:24
  • Perhaps it's `$(( 1 + 0.1 ))` bash: 1 + 0.1 : syntax error: invalid arithmetic operator (error token is ".1 ") – Diego Torres Milano Aug 25 '15 at 18:28
  • @KeithThompson Yeah, my thinking was more along the lines of parsing problems (due to non-bash) causing the issue but that's probably hard to actually pull off. I think it is more likely something like `sleep $((0.1))` trying to assign `0.1` to an integer variable or something. – Etan Reisner Aug 25 '15 at 18:45
  • Sorry I just was wrong, I edit my post to clarify where wasthe problem. – Noqrax Aug 26 '15 at 12:20

3 Answers3

107

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.

kenorb
  • 155,785
  • 88
  • 678
  • 743
11

Some options:

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

or

read -p "Continuing in 0.5 Seconds...." -t 0.5
echo "Continuing ...."
Jose H. Rosa
  • 514
  • 4
  • 8
  • 3
    read: 0.5: invalid timeout specification `GNU bash, version 3.2.53(1)-release (x86_64-apple-darwin14)` – Evhz May 24 '18 at 14:02
7

Bash was complaining about decimal values,

read: 0.5: invalid timeout specification

I came around with this solution which works great.

sleep_fraction() {
  /usr/bin/perl -e "select(undef, undef, undef, $1)"
}

sleep_fraction 0.01428
Evhz
  • 8,852
  • 9
  • 51
  • 69