1
#!/bin/bash

for i in $(seq $1)
        do
                echo -n '.'
                sleep 1
        done

echo

The script prints a dot per second and the number of dots on the screen depending on user's input. What I want to do is that when I press a key (for example, a), the script will run faster like one dot in half a second..so the more I press the same key, the faster it runs.

2
  • So you want to adjust the sleep duration interactively while the script is already running? Commented Mar 19, 2014 at 1:34
  • That's correct :D Could you help me? Commented Mar 19, 2014 at 1:44

1 Answer 1

1

Use a variable for the duration of the sleep:

duration=1
# ... inside loop:
                sleep $duration
# ...

Then each time a call to sleep finishes, have some code to check for new keypresses and alter the value of $duration as needed.

On another note, depending on the version of sleep you have available, it may not support fractional arguments so you might have to use a separate program like usleep.

Sign up to request clarification or add additional context in comments.

4 Comments

One problem is that sleep does work with seconds only. So usleep would be a better choice since it supports microseconds.
Ah... my system's sleep supports fractional seconds, so it varies. Anyway I'll add that.
Thanks for your answer, but can you help me with the code to check for a keypress?
@Cailou well that's not what you asked ;-) That would be a matter for a separate question, as I'm not sure of the details offhand but it could be somewhat more complicated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.