首页 > 解决方案 > Linux shell script on background, loop exit reading keyboard buffer

问题描述

On one laptop with x-ubuntu 18.04 I have a shell script (.sh) which automates a series of clicks on a webapp (via xdotool). Everything works fine but I need to be able to stop the script at the push of a button (while the focus is on the browser due to the clicks). For now I have buffered by clicking the script on the terminal window every k (read from terminal) iterations and giving a sleep of 5 seconds, so that I can press ctrl + c. This solution, however, as well as being not very elegant, forces me to always keep the terminal and browser in the same position and a 5 seconds "lost" every k iterations. I attach summary script, I really tried everything but I can't figure out with it, some time ago I made the same script in C++ (but I sadly was under windows and I can't use c++ on this laptop) going to read the keyboard buffer... ps: I can't use various autoclicker because the script in addition to clicking also does other stuff (it launches programs, updates databases, etc.)

#!/bin/sh
p_p()
{ # brings the terminal window to the front
xdotool mousemove 300 0
sleep 0.5   # top-panel up
xdotool click 1
}

i=1
echo "every how many iterations do you want the possibility to interrupt?"
read k

until [ $i -gt 100000000000 ] 
do

for X in $(seq 3)
do
    xdotool mousemove 974 431
    xdotool click 1
    sleep 0.1
done

    #here there is a function for other stuff

remainder=$(( i % k ))

    if [ "$remainder" -eq 0 ]; then
    p_p
    echo "press ctrl + c for interrupt"
    sleep 5
else
    sleep 0.5 
fi  

i=$(( i+1 ))
echo "$i"
    done
    echo "security limit reached"

标签: linuxshexitkeyboard-eventsuntil-loop

解决方案


推荐阅读