I'm trying to write a small bash script that will take values from my mouse's horizontal scroll wheel to change my system's volume.
Using this guide, I've come up with the following command to successfully read the values:
evemu-record /dev/input/event16 | grep --line-buffered "0002 0006" | awk '{ print $10 }'
This displays the values to the terminal as they come in, but I want to be able to use conditional logic on each value to determine whether to increase or decrease the volume (and by how much).
My previous attempt at this was using a while loop (and suggestions from some other posts that seemed related to my problem), which was able to capture the output, but using grep slowed down everything too much for it to be usable:
stdbuf -oL evemu-record /dev/input/event16 |
while IFS= read -r line; do
g=$(echo "$line" | grep "0002 0006") # falls behind substantially on mouse movement
# g="$line" # does not fall behind
if [ -n "$g" ]; then
echo "$g"
fi
done
This does do what I want, but when moving the mouse, data comes in much more frequently, and it seems like this implementation is too inefficient to keep up. Substituting the commented g assignment line keeps the output synced with the latest output from evemu-record.
I have tried piping the first command above into the while loop, but it doesn't seem to work.
while/readloop behind your filteringawkpre-processor? E.g.evemu-record … | awk '/0002 0006/ { print $10 }' | while read -r col10; do … something … with … "$col10" …; donegrep, just doif [[ $line = *0002\ 0006* ]]; thenconditional logicto adjust the volume and by how much, but nothing in the code provided shows these steps; while I understand you're looking for a 'fast way' to parse the0002 0006lines, adding follow-onconditional logicand volume adjustments could further delay processing; if you want some ideas on how to speed up the entire process then you should also include yourconditional logicand sample system calls that adjust the volume; having said that ... you should also provide a few sample0002 0006lines with the desired matching system callsgrep+awk+conditional logiccode will likely be expensive ... while it may be possible to replace all 3 bits with a single piece of 'conditional logic' ... but we would need to see a complete set of code to know for sure