0

I am using and a do while loop in bash script and executing a python file with some arguments inside the loop, evetime I loop the arguments are updated and the python file is executed again.

The problem is the changes to the arguments are updated in python file.

Please have a look at my code and correct me... Thanks in advance.

mouse.sh

#!/bin/bash

DEVICE="192.168.0.100:5555"

while [ 0 ]
do
    adb -s $DEVICE pull /sdcard/motion.log 2>/dev/null
    adb -s $DEVICE shell  "rm /sdcard/motion.log 2>/dev/null" 2>/dev/null

    cat motion.log 2>/dev/null| while read line

    do
        x=`echo ${line}| cut -d ' ' -f 1`
        y=`echo ${line}| cut -d ' ' -f 2`
        ipython clicker.py ${x} ${y}

    done

        rm motion.log 2>/dev/null

done

clicker.py

import sys

while True:
    try:
        print (sys.argv[1], sys.argv[2])
    except:
        print ("some error")

motion.log

27 -1 -121
26 -1 -122
25 0 -122
24 0 -123
23 0 -124
22 0 -125
22 0 -126
21 0 -127
20 1 -128
19 1 -129
18 1 -130
17 2 -131
16 2 -132
15 2 -132
14 3 -134
13 3 -135
12 4 -136
11 4 -138
10 5 -139
9 5 -140
9 6 -141
8 6 -142
7 7 -144
7 8 -146
8 9 -148
9 9 -148
11 10 -150
13 10 -151
15 10 -151
16 11 -152
17 11 -152
19 11 -152
20 12 -151
21 12 -151
22 12 -150
23 13 -149
23 14 -148
23 15 -147
23 15 -146
24 16 -146
24 16 -145
24 17 -144
24 18 -144
24 18 -143
24 19 -142
24 20 -141
24 20 -140
24 21 -139
24 22 -139
24 22 -138
24 23 -137
24 23 -136
24 24 -136
24 24 -135
24 25 -134
23 26 -134
23 26 -133
23 26 -132
23 27 -131
23 28 -131
22 28 -130
22 28 -129
22 29 -128
22 29 -127
21 29 -127
21 30 -126
21 30 -125
20 30 -124
20 31 -123
19 31 -122
19 31 -121
19 32 -120
18 32 -120
18 32 -119
18 32 -118
17 33 -117
16 33 -116
16 33 -115
15 33 -114
15 34 -114
14 34 -113
14 34 -112
13 34 -111
13 34 -110
12 34 -110
12 34 -109
11 35 -108
10 35 -107
10 35 -106
9 35 -105
9 35 -104
8 35 -104
8 35 -103
7 35 -102
7 35 -101
6 35 -100
6 35 -99
5 35 -98
5 35 -97
4 35 -96
3 35 -95
3 35 -94
2 35 -93
1 35 -92
1 35 -91
0 35 -90
0 35 -89
-1 35 -88
-1 35 -87
-2 35 -85
-3 35 -84
-3 35 -83
-4 35 -82
-5 34 -81
-6 34 -80
-6 34 -79
-7 34 -78
-8 34 -77
-8 33 -76
-9 33 -75
-10 33 -74
-10 32 -73
-11 32 -72
-11 32 -71
-12 31 -70
-12 31 -69
-13 31 -68
-13 30 -67
-14 30 -67
-14 30 -66
-15 29 -65
-15 28 -64
-16 28 -63
-16 27 -62
-16 27 -61
-17 26 -60
-17 26 -59
-18 25 -59
-18 25 -58
-18 24 -57
-19 24 -56
-19 23 -56
-19 23 -55
-19 22 -54
-20 22 -54
-20 22 -53
-20 21 -53
-20 21 -52
-20 20 -52
-20 19 -51
-20 18 -51
-20 18 -50
-20 17 -50
-20 16 -50
-20 16 -49
-20 15 -49
-20 14 -49
-20 14 -48
-20 13 -48
-20 12 -48
-20 11 -47
-20 10 -47
-20 9 -47
-20 8 -47
-20 8 -46
-20 7 -46
-20 6 -46
-19 6 -46
-19 5 -46
-19 4 -46
-19 3 -46
-18 3 -46
-18 2 -46
-18 1 -46
-18 0 -46
-17 0 -46
-16 -2 -46
-16 -4 -46
-15 -4 -46
-14 -6 -46
-14 -8 -47
-13 -8 -47
-12 -10 -47
-12 -11 -48
-11 -12 -48
-11 -14 -49
-10 -15 -50
-10 -16 -51
-10 -17 -52
-9 -18 -53
-9 -19 -54
-9 -20 -55
-9 -20 -56
-9 -22 -58
-10 -23 -60
-10 -23 -62
-10 -24 -63
-11 -24 -63
-11 -24 -64
-11 -24 -66
-12 -24 -68
-13 -24 -70
-13 -23 -71
-13 -23 -72
-14 -23 -74
-14 -22 -75
-15 -22 -76
-15 -22 -78
-15 -21 -79
-16 -21 -80
-16 -20 -81
-16 -20 -82
-17 -20 -82
-17 -19 -83
-17 -19 -84
-17 -18 -85
-18 -18 -86
-18 -17 -87
-18 -17 -88
-18 -16 -88
-19 -16 -89
-19 -15 -89
-19 -15 -90
-19 -14 -91
-18 -14 -90

Output:

25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0
25 0

if the initial value is 25 0 it always outputs 25 0, if the initial value is 100 50 it always outputs 100 50. It is always showing bash script initial value its is not updating.

1
  • Why are you using ipython instead of just python? Commented Apr 22, 2017 at 9:04

2 Answers 2

3

First let's sort out the shell script, it uses some unnecessary external programs. I will only look at the for loop, the rest seems irrelevant to your problem:

while read x y unused
do
    python clicker.py $x $y
done < motion.log

No need for cat and those calls to the cut program.

Your python has an infinite loop in it. The command-line arguments will never change, so your python can be:

import sys

try:
    print(sys.argv[1], sys.argv[2])
except:
    print("some error")

However, we are still very inefficient, calling python once for each line. It would be far better if we did the whole lot in python and just passed the filename:

import sys

if len(sys.argv) != 2:
    print("some error with parameters", file=sys.stderr)
    sys.exit(1)

filename = sys.argv[1]

with open(filename) as fh:
    for line in fh:
        x, y = line.split()[:2]
        print(x, y)

Then in bash we replace the for loop with:

python clicker.py motion.log
Sign up to request clarification or add additional context in comments.

Comments

1

Since you're doing an infinity loop in your python script it will print only the first arguments on the first time you call the script. Change your python to:

import sys

try:
    print (sys.argv[1], sys.argv[2])
except:
    print ("some error")

1 Comment

[TerminalIPythonApp] CRITICAL | Bad config encountered during initialization: [TerminalIPythonApp] CRITICAL | Unrecognized flag: '-28' now i am getting this error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.