#!/usr/bin/env bash
while true; do
if xprintidle | grep -q 3000; then
xdotool mousemove_relative 1 1
fi
done
目前我可以檢查是否xprintidle
等於 3000,如果等於,則執行xdotool
。但我想檢查是否xprintidle
大於或等於3000,然後執行xdotool
。我怎樣才能實現這個目標?
答案1
if [ $xprintidle -ge 3000 ]; then
[...stuff...]
這是一個快速解釋:
- GT- 比...更棒
- 葛- 大於或等於
- $(…)成為括號內命令的輸出
答案2
您可以使用bash
的算術展開式直接比較整數:
#!/usr/bin/env bash
while :; do
(( $(xprintidle) >= 3000 )) && xdotool mousemove_relative 1 1
sleep 0.5
done
如果您只想要單一命令,&&
這是一個簡單的方法。或者,使用if
:
#!/usr/bin/env bash
while :; do
if (( $(xprintidle) >= 3000 )); then
xdotool mousemove_relative 1 1
fi
sleep 0.5
done
我添加了一個sleep
對循環的調用,以便每次運行暫停半秒 - 根據需要進行調整。
答案3
要表示數字是否大於或等於其他數字,您可以使用-ge
。所以你的程式碼可以看起來像
#!/usr/bin/env bash
while true; do
if [[ $(xprintidle) -ge 3000 ]]; then
xdotool mousemove_relative 1 1
fi
done