Compare 2 strings

I think there is a way to detect mouse movement.

valuator[0] changes if the mouse moves.

So I need to compare the two strings.

Not sure how to do that.

How could I send the valuator[0] string to a file ?
I would need to do it twice.

andy@7_~/Downloads$  xinput query-state 9
2 classes :
ButtonClass
    button[1]=up
    button[2]=up
    button[3]=up
    button[4]=up
    button[5]=up
    button[6]=up
    button[7]=up
    button[8]=up
    button[9]=up
    button[10]=up
    button[11]=up
    button[12]=up
    button[13]=up
    button[14]=up
    button[15]=up
    button[16]=up
    button[17]=up
    button[18]=up
    button[19]=up
    button[20]=up
ValuatorClass Mode=Relative Proximity=In
    valuator[0]=947
    valuator[1]=1060
    valuator[2]=0
    valuator[3]=-60

andy@7_~/Downloads$  xinput query-state 9
2 classes :
ButtonClass
    button[1]=up
    button[2]=up
    button[3]=up
    button[4]=up
    button[5]=up
    button[6]=up
    button[7]=up
    button[8]=up
    button[9]=up
    button[10]=up
    button[11]=up
    button[12]=up
    button[13]=up
    button[14]=up
    button[15]=up
    button[16]=up
    button[17]=up
    button[18]=up
    button[19]=up
    button[20]=up
ValuatorClass Mode=Relative Proximity=In
    valuator[0]=910
    valuator[1]=921
    valuator[2]=0
    valuator[3]=-60

How about:

mouse1=$( xinput query-state <your device name> )
# Give a short delay.
sleep 1
mouse2=$( xinput query-state <your device name> )
if [ "$mouse1" == "$mouse2" ]
......OR......
if [ "$mouse1" != "$mouse2" ]

Depending upon how you want to use the conditionals.
NOTE: UNTESTED as I am back in OSX 10.14.3 mode.

EDIT:
IMPORTANT! I forgot to add that there IS a flaw in this method, it is not a bug however.
As these things are learning curves when wanting to _interrupt_ something then see if you can realise what that flaw is from those few lines.

I think this is working.

mouse1=$( xinput query-state 9 )
# Give a short delay.
sleep 2
mouse2=$( xinput query-state 9 )

if [ "$mouse1" != "$mouse2" ]
then
echo "Mouse moved."

fi

Now I need to learn how to poll (Hope its the right word) say every 30 seconds.

You are jumping the gun at the moment; UNDERSTAND what is going on first!

I mentioned that there is a FLAW. Try to understand that when messing with HW you need to know what you are doing.
Read the URL and understand to get a grasp of what is going on; although the code will work 99.999% of the time there is a condition known as a "race condition".
Race condition - Wikipedia

Although not strictly a race condition it is a _pseudo_race condition.

An explanation:
If you move the mouse a tiny fraction of a second AFTER the 'if' line and before the condition is finalised then the two variables will be the same.
Assuming you have this _polling_ as you put it in a loop then this will be missed on the second variable and the next time in the loop the two values will be the same again and so it will seem no-one has moved the mouse. This, although NOT strictly a race condition per-se is close enough.
AFAIAC there is no easy cure using sequential code like a shell script so you HAVE to be aware of such situations.

So be aware; it is these subtleties that can cause headaches in the future.

As for _polling_ you could put the code inside a 'while' loop, while true and exit the loop via a break command using the conditional statement if that is what you want.

I am trying to understand. I am no guru.

test1.sh
/home/andy/bin/test1.sh: line 24: syntax error near unexpected token `done'
/home/andy/bin/test1.sh: line 24: `done'



mouse1=$( xinput query-state 9 )
# Give a short delay.
sleep 2
mouse2=$( xinput query-state 9 )

while [ "$mouse1" != "$mouse2" ]
do
echo "Mouse moved."
if [ "$mouse1" == "$mouse2" ]
then
break

done

#while [ condition ]
#do
   #statements1      #Executed as long as condition is true and/or, up to a disaster-condition if any.
   #statements2
  #if (disaster-condition)
  #then
    #break              #Abandon the while lopp.
  #fi
  #statements3          #While good and, no disaster-condition.
#done

Try this as a starting point:

#!/bin/bash
# Initialise a log file if you want one.
: > /tmp/my_mouse_log.txt

while true
do
        MOUSE1=$( xinput query-state 9 )
        # Use read's timer as the 2 second delay to allow quitting the program by pressing 'q' or 'Q'.
        read -r -n1 -t2 QUIT
        MOUSE2=$( xinput query-state 9 )
        # Remember a possible _race_condition here!
        if [ "$MOUSE1" != "$MOUSE2" ]
        then
                echo "Mouse moved!"
                # The line below will create a logfile as an option. It goes the the /tmp directory but could be anywhere inside your $HOME directory.
                echo "$( date ), mouse moved!" >> /tmp/my_mouse_log.txt
        fi
        if [ "$QUIT" == "q" ] || [ "$QUIT" == "Q" ]
        then
                break
        fi
done
# Exit to here when 'q' or 'Q' is pressed.
printf "\rYou pressed $QUIT to quit.\n"
cat /tmp/my_mouse_log.txt

This is a basic _poll_ of your mouse capturing mouse movement.

I got this in the log.

Does it means it was in a race condition?

Mon Mar 25 13:20:36 CDT 2019, mouse moved!
Mon Mar 25 13:20:38 CDT 2019, mouse moved!
Mon Mar 25 13:20:40 CDT 2019, mouse moved!
Mon Mar 25 13:20:42 CDT 2019, mouse moved!
Mon Mar 25 13:20:46 CDT 2019, mouse moved!
Mon Mar 25 13:20:48 CDT 2019, mouse moved!
Mon Mar 25 13:20:51 CDT 2019, mouse moved!
Mon Mar 25 13:20:53 CDT 2019, mouse moved!
Mon Mar 25 13:20:55 CDT 2019, mouse moved!
Mon Mar 25 13:20:57 CDT 2019, mouse moved!
Mon Mar 25 13:21:01 CDT 2019, mouse moved

Hi drew77...

NO!
It means you are moving the mouse which you have been doing and it is logging the fact. As you wanted mouse movement detection then that is exactly what the code does.

It was supposed to be part of your detection of mouse movement for your project and although works as expected it is not a finished piece of code as it was meant to be added to your original code, but you have changed the goalposts since then.

Your original requirements were that you wanted to check if there was no mouse movement and if there was it was to tell you.

Thus, this does exactly 'what it says on the tin', detect mouse movement!