Compare 2 strings


Login or Register for Dates, Times and to Reply

 
Thread Tools Search this Thread
# 1  
Compare 2 strings

I think there is a way to detect mouse movement.

Code:
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.


Code:
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

# 2  
How about:
Code:
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.

Last edited by wisecracker; 03-24-2019 at 07:44 AM.. Reason: See EDIT:
# 3  
I think this is working.

Code:
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.
# 4  
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.
# 5  
I am trying to understand. I am no guru.

Code:
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

# 6  
Try this as a starting point:
Code:
#!/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.
# 7  
I got this in the log.

Does it means it was in a race condition?

Code:
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

Login or Register for Dates, Times and to Reply

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

If statement to compare two strings

Hi, I am trying to do the following to see if "ip" is already present in a file. if ; then echo "hi" else echo "hello" fi I am seeing errors on the if statement. Can someone please correct the syntax for me? Thanks (2 Replies)
Discussion started by: waince
2 Replies

2. Shell Programming and Scripting

Compare 2 Strings

I have 2 values V_1_4_4_b1 and V_1_5_1_RC_b1. I would need to compare them and determine if the 1st value is greater, less or equal than the 2nd value. The result should need to have a return value. I have below code in bash function but it seems it is not comparing it correctly. Any help will... (8 Replies)
Discussion started by: aderamos12
8 Replies

3. Shell Programming and Scripting

How to compare two strings in a file

hello guyzz please help me out.. I have two file a.sh and b.sh it contains two string SD109 ,SD108 . I want to compaere these two string . If a.sh>b.sh do rebasing record time. else it shows no rebasing required. Thanks. (2 Replies)
Discussion started by: abhijtr
2 Replies

4. Shell Programming and Scripting

Compare two strings

hi.. i have a problem to compare two string my code is like that if ] then echo "both data are correct" elif ] echo "data is wrong" fi here $username1 is taking value from file.. (7 Replies)
Discussion started by: shubhig15
7 Replies

5. Shell Programming and Scripting

How to Compare 2 Strings ?

Hello , I want to Compare with 2 strings and get if they are True or not please would like some help on this #!bin/ksh echo "Enter Name 1" read Name1 echo "Enter Name 2" read Name2 echo "------------------------" echo "First Name: $Name1" echo "Second Name: $Name2" echo... (25 Replies)
Discussion started by: shatztal
25 Replies

6. Shell Programming and Scripting

Compare text strings.

Hi Im trying to write a script that compare a text string. But it fails, I think it adds a extra line feed to the result and fails beacuse of that. The script. DT=`date +'%Y%m%d%H%M%S'` #ALARM_BIN=/users/alarms/ssa/alarms/bin QUEUE_THR=10 #unset offset #offset="***Server reports data... (3 Replies)
Discussion started by: vettec3
3 Replies

7. Shell Programming and Scripting

How to compare two strings using if

Hi, Here is my script #!/bin/ksh echo $pick_typ if ];then echo "inside if" else echo "outside if" fi when ever i pass CUS as parameter to this script am getting the correct value CUS, however if i pass ORD as parameter it is not coming inside if it is echoing else "Outside... (12 Replies)
Discussion started by: bhargav20
12 Replies

8. Shell Programming and Scripting

compare strings, words in different order

Hi, Would anyone know how to compare two strings, and only throw an error if there were different words, not that the same words were in a different order? e.g "A B C" vs "B C A" ->OK "A B C" vs "A D C" -> BAD Thanks! (2 Replies)
Discussion started by: rebelbuttmunch
2 Replies

9. Shell Programming and Scripting

How to compare two strings

Hi all, I am trying to compare two strings/dates, but its throwing error::Syntax error at line 5: Please help !! Any alternate way to compare two dates is also fine.... logdate1=`date -u '+%Y.%m.%d %T'` sleep 5 logdate2=`date -u '+%Y.%m.%d %T'` if test... (5 Replies)
Discussion started by: prashant43
5 Replies

10. Shell Programming and Scripting

to compare two strings

hi all, i am new to unix. Actually i need to compare two string and print the result... suppose type='sun' if; then echo good morning else echo good night fi whether the comparison is right r we need to use eq???? help me please.... :confused: thanks in advance.... (1 Reply)
Discussion started by: ithirak17
1 Replies

Featured Tech Videos