Search code examples
linuxbashscriptingautomationxdotool

BASH script 'sleep' command not working


I have written a script to work as an auto-clicker on my Ubuntu distribution. The clicking works, but the 'sleep' command doesn't seem to be working correctly. If I take out the click and run it from terminal then the sleeps work as intended. But when I bind it to a shortcut and run it while playing a game it doesn't sleep it seems it just keeps clicking.

 #!/bin/bash
counter=0     #number of iterations executed
loop_to=1800  #number of iterations to do
break_interval=10 #time to break, in seconds
click_interval=.5 #time between iterations, in seconds
break_at=15   #how many iterations before taking a break


while [ $counter -lt $loop_to ]; do
    xdotool click 1
    sleep $click_interval
    echo $counter
    let counter=counter+1
    if [[ $(( $counter % $break_at )) == 0 ]]; then   
        sleep $break_interval
        echo I slept
    fi
done

The most important sleep is the break_interval one because that is to pause the autoclicker for x amount of time.


Solution

  • The test:

    [[ $(( $counter % $break_at )) == 0 ]]

    could be written:

    [ $(( $counter % $break_at )) -eq 0 ]

    [[ x == y ]] is bash-specific and tests for string equality.

    [ x -eq y ] works with any POSIX shell and tests for integer equality.

    As @cdarke notes, your shebang line is incorrect, the 1st two characters must be #!, and so you're probably getting /bin/sh to run this.