16
#!/usr/bin/env bash
while true; do
    if xprintidle | grep -q 3000; then
      xdotool mousemove_relative 1 1
    fi
done

Currently I'm able to check if xprintidle is equal to 3000 and then if it is, execute xdotool. But I want to check if xprintidle is greater or equal than 3000 and then execute xdotool. How can I achieve this?

5
  • 3
    if you just have take 2 min to google your question about bash, you will have found it faster than here
    – damadam
    Jun 1, 2018 at 14:58
  • 1
    I was googling about 1h and coulnd't find any answer. Even here some of the answers didn't work for me.
    – Newb1996
    Jun 1, 2018 at 15:11
  • 2
    @damadam To be honest, googling only works when you know the buzzwords to google for.
    – PerlDuck
    Jun 1, 2018 at 15:13
  • 3
    @damadam So? This way it’s useful for others. Let’s be nice.
    – dessert
    Jun 1, 2018 at 15:55
  • While research effort is indeed something that is encouraged before asking questions, and OP could have definitely found something, question will be beneficial for others. Everyone uses different search term, and this question with its wording can be of service too. Jun 1, 2018 at 16:00

3 Answers 3

25
if [ $xprintidle -ge 3000 ]; then
  [...stuff...]

Here's a quick explanation:

  • gt - greater than
  • ge - greater or equal than
  • $( ... ) becomes the output of the command inside the parentheses
3
  • 4
    Because this uses [ instead of [[ and doesn't quote the command substitution, this doesn't quite capture OP's intention. It allows xprintidle to add additional conditions to test, like outputting 1 -o 2000 will also cause it to pass the condition. Not really a problem on its own, but it's bad practice. If later another insignificant bug, maybe in xprintidle, allows someone untrustworthy to prefix the output. You may also consider that bug to be no problem on its own, but together they can amount to something.
    – JoL
    Jun 1, 2018 at 20:59
  • If xprintidle is not a command rather than a variable it should be $xprintidle
    – alper
    Jun 6, 2020 at 0:17
  • @alper thnx, edited accordingly Jun 11, 2020 at 13:10
9

You can use bash’s Arithmetic Expansion directly to compare integers:

#!/usr/bin/env bash
while :; do
  (( $(xprintidle) >= 3000 )) && xdotool mousemove_relative 1 1
  sleep 0.5
done

If you just want the single command, && is a simple way. Alternatively, use if:

#!/usr/bin/env bash
while :; do
  if (( $(xprintidle) >= 3000 )); then
    xdotool mousemove_relative 1 1
  fi
  sleep 0.5
done

I added a sleep call to the loop to pause for half a second each run – adapt it as needed.

2
  • 2
    +1 for the suggestion to sleep a while thus preventing the loop from going berserk.
    – PerlDuck
    Jun 1, 2018 at 16:05
  • @PerlDuck Tbh, I don’t even understand fully what the user's idle time is – I’ll leave that to OP…
    – dessert
    Jun 1, 2018 at 16:55
4

To say if number is greater or equal to other you can use -ge. So your code can look like

#!/usr/bin/env bash
while true; do
    if [[ $(xprintidle) -ge 3000 ]]; then
        xdotool mousemove_relative 1 1
    fi
done
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .