My first Autohotkey runs too slow [closed] - performance

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
So it's been about 4 years since i've touched any sort of scripting and I wanted to get back into it for the sake of scripting. I love the problem solving that comes with it. Annnyyyyway.
I was making a little bot to test out how far I could go for a first attempt. Now I know the code is just plain terrible and long, I know I can probably make it way shorter and more efficient(so any and all tips on that are welcome).
So my question is, what am I doing wrong in terms of speed in this one? Because at the part:
;Select first company. Any missions?
LabelMission1:
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k.bmp
{
sleep 223
Its just slow as hell, as in it takes a full 3 seconds to switch between checking for a available mission and 'going to the next company' by pressing 's' and 'space'. I tried speeding up the code by lowering the sleep times from an average of 600 to an average of 200 but that didn't seem to do much, it sped up some parts but not the switching between companies and checking for a mission.
So any help is welcome, if u wanna completely break up my code and write it super efficient, I'm ok with that, just explain to me what you did. I would like to learn as much as I can from you guys :)
EDIT: This is C++ written with Scite4Autohotkey
Greetz,
Dusan
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
#Warn ; Enable warnings to assist with detecting common errors.
SetKeyDelay, 20, 20
SetMouseDelay, 200
#MaxThreads 255
;What gamemode is the game in? Solo or private?
;send {Escape}
; ImageSearch FoundX, FoundY, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Mode_Solo_Play.bmp
; {
; If ErrorLevel = 0
; return
; {
; If ErrorLevel = 1
;
--------------------------------------------------------------------------------------------------------------------------
;Is the game loaded and in bulletin board? ((If not can we see starport service?)) -> ((If not can we see mission board?))
labelstart: ;this label is the startingpoint of the script
sleep 4500
ImageSearch pointX, pointY, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Starport_services.bmp ;Can we see starport services?
{
sleep 150
If ErrorLevel = 1 ; If it CAN'T find Starport Services, it will look for Mission Board instead.
{
sleep 150
ImageSearch pointX2, pointY2, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Mission_Board.bmp ;Can we see Mission Board?
if ErrorLevel = 1 ;If it CAN'T find mission board, it will ask for it.
{
MsgBox, 48, Something went wrong!, I can't find "Mission Board" or "Starport Services" anywhere.
MsgBox, 4132, Something went wrong!, Do you wish to retry?
IfMsgBox Yes
{
MsgBox, 4160, Restarting, Click OK and switch back to game with "Starport Services" Open!! `n(YOU GET 15 SECONDS TIME)
sleep 15000
gosub labelstart
}
else IfMsgBox no
ExitApp
}
If ErrorLevel = 0 ;If it finds Mission Board it will navigate to the Mission Board.
{
Sleep 143
SendEvent s
Sleep 177
SendEvent {Space}
sleep 10000
gosub LabelMissionBoard
}
}
If ErrorLevel = 0 ;If it finds Starport services it will navigate to the Mission Board.
{
Sleep 200
SendEvent {space}
sleep 15234
SendEvent s
sleep 134
SendEvent {Space}
sleep 10000
gosub LabelMissionBoard
}
}
;Are we in the mission board?
LabelMissionBoard: ;Jump here to check for missionboard
ImageSearch pointX3, pointY3, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Mission_Board_Open.bmp ; Is the mission board open?
{
sleep 233
if ErrorLevel = 1 ;If it CAN'T find the Mission Board Open.
{
MsgBox, 16, Something went wrong!, I can't find the "Mission Board" Opened`, did I open it? `n- If I didn't`, please open it and press OK. (10 seconds before I retry.) `n- If I did please restart the script (end it with ctrl+R)
sleep 9989
gosub LabelMissionBoard
}
if ErrorLevel = 0 ;If it CAN find the Mission Board open.
{
sleep 50
gosub LabelEnoughCredits
}
}
;Do we have enough credits? (over 1,000,000) ((Probably pixel search here))
LabelEnoughCredits:
;LINE IS TEMPORARLEY DISABLED
;PixelSearch, Px, Py, 246, 190, 246, 190, 0xD1CECD, 10, Fast ;Looking for credits over 1,000,000 ((Credits shift to the left when below 1kk, so it should no longer be there.))!!UNTESTED!!
;LINE IS TEMPORARLEY DISABLED
ImageSearch pointX3, pointY3, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Above_1_Million.bmp ;Looking for credits over 1,000,000 ((Credits shift to the left when below 1kk, so it should no longer be there.))!!UNTESTED!!
Sleep 80
{
Sleep 91
If ErrorLevel = 1 ;If it finds out you DON'T have above 1,000,000.
{
MsgBox, 0, MONEY PROBLEM!, Unfortunately you seem to have ran out of funds. If I go further you won't be able to pay your insurance!`n- Please Make sure you have above 1`,000`,000.
sleep 98
ExitApp
}
If Errorlevel = 0 ;If it find out you DO have above 1,000,000.
{
sleep 100
gosub LabelAID
}
}
;Is the filter "Aid" selected?
LabelAID:
ImageSearch pointX4, pointY4, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Aid_Selected.bmp ;Can we find Aid?
{
sleep 123
If Errorlevel = 1 ;If it ISN'T selected.
{
sleep 170
send a
sleep 163
send a
sleep 133
send a
sleep 125
send a
sleep 160
send a
sleep 163
send a
sleep 148
send a
sleep 151
send a
sleep 140
Send w
Sleep 142
Send w
Sleep 136
Send w
Sleep 132
Send w
Sleep 147
Send w
Sleep 146
Send w
Sleep 154
Send w
Sleep 137
Send w
Sleep 124
Send {Space} ;Selecting filter
Sleep 1107
send s
Sleep 113
send s
Sleep 118
send s
Sleep 118
send s
Sleep 116
send s
Sleep 139
Send {Space} ;AID filter is selected.
Sleep 123
send s
sleep 143
send s
sleep 154
send {Space} ;First mission is selected.
Sleep 162
Gosub labelMission1
}
If Errorlevel = 0 ;If aid IS selected.
{
Sleep 143
send s
sleep 143
send s
sleep 154
send {Space}
Sleep 162
Gosub labelMission1 ;If found select first Group to check for mission
}
}
;Select first company. Any missions?
LabelMission1:
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k.bmp
{
sleep 223
if Errorlevel = 1 ; If mission ISN'T found.
{
Sleep 150
Send s
sleep 160
send {Space}
Sleep 113
gosub LabelMission2
}
if Errorlevel = 0 ; If missions IS Found.
{
sleep 143
Send D
Sleep 160
Send w
Sleep 166
Send w
Sleep 129
Send w
Sleep 177
Send w
Sleep 167
Send w
Sleep 146
Send w
Sleep 172
Send w
Sleep 112 ;Top of mission list selected
Label25kSelected1:
Sleep 266
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k_selected.bmp ;Did it select the 25k Mission?
{
sleep 50
If Errorlevel = 1 ;If Mission ISN'T selected.
{
sleep 376
send s
sleep 185
gosub Label25kSelected1 ;If it CAN'T find the mission by pressing 'S' once, it will retry untill it does.
}
If ErrorLevel = 0 ;If Mission IS selected.
{
send w
sleep 50
send w
sleep 50
send w
sleep 50
send {Space}
sleep 360
Send D
Sleep 260
send {Space}
Sleep 700
send {Space}
Sleep 700
send {Space}
sleep 831
send {backspace}
sleep 322
Send {backspace}
sleep 241
send {backspace}
sleep 951
gosub Labelstart ;Mission is turned in, going back to start to see the refresh if there is anymore.
}
}
}
}
;Select 2nd company. Any missions?
LabelMission2:
ImageSearch pointX6, pointY6, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k.bmp
{
sleep 143
if Errorlevel = 1 ; If mission ISN'T found.
{
Sleep 550
Send s
sleep 660
send {Space}
Sleep 613
gosub LabelMission3
}
if Errorlevel = 0 ; If missions IS Found.
{
sleep 143
Send D
Sleep 160
Send w
Sleep 166
Send w
Sleep 129
Send w
Sleep 177
Send w
Sleep 167
Send w
Sleep 146
Send w
Sleep 172
Send w
Sleep 112 ;Top of mission list selected
Label25kSelected2:
Sleep 266
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k_selected.bmp ;Did it select the 25k Mission?
{
sleep 50
If Errorlevel = 1 ;If Mission ISN'T selected.
{
sleep 376
send s
sleep 185
gosub Label25kSelected2 ;If it CAN'T find the mission by pressing 'S' once, it will retry untill it does.
}
If ErrorLevel = 0 ;If Mission IS selected.
{
send w
sleep 50
send w
sleep 50
send w
sleep 50
send {Space}
sleep 360
Send D
Sleep 260
send {Space}
Sleep 700
send {Space}
Sleep 700
send {Space}
sleep 1231
send {backspace}
sleep 322
Send {backspace}
sleep 241
send {backspace}
sleep 3251
gosub Labelstart ;Mission is turned in, going back to start to see the refresh if there is anymore.
}
}
}
}
;Select 3rd company. Any missions?
LabelMission3:
ImageSearch pointX7, pointY7, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k.bmp
{
Sleep 163
if Errorlevel = 1 ; If mission ISN'T found.
{
Sleep 450
Send s
sleep 460
send {Space}
Sleep 513
gosub LabelMission4
}
if Errorlevel = 0 ; If missions IS Found.
{
sleep 143
Send D
Sleep 160
Send w
Sleep 166
Send w
Sleep 129
Send w
Sleep 177
Send w
Sleep 167
Send w
Sleep 146
Send w
Sleep 172
Send w
Sleep 112 ;Top of mission list selected
Label25kSelected3:
Sleep 66
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k_selected.bmp ;Did it select the 25k Mission?
{
sleep 250
If Errorlevel = 1 ;If Mission ISN'T selected.
{
sleep 376
send s
sleep 185
gosub Label25kSelected3 ;If it CAN'T find the mission by pressing 'S' once, it will retry untill it does.
}
If ErrorLevel = 0 ;If Mission IS selected.
{
send w
sleep 50
send w
sleep 50
send w
sleep 50
send {Space}
sleep 360
Send D
Sleep 260
send {Space}
Sleep 700
send {Space}
Sleep 700
send {Space}
sleep 1231
send {backspace}
sleep 322
Send {backspace}
sleep 241
send {backspace}
sleep 3251
gosub Labelstart ;Mission is turned in, going back to start to see the refresh if there is anymore.
}
}
}
}
;Select 4th company. Any missions?
LabelMission4:
ImageSearch pointX8, pointY8, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k.bmp
{
Sleep 196
if Errorlevel = 1 ; If mission ISN'T found.
{
Sleep 150
Send s
sleep 260
send {Space}
Sleep 113
gosub LabelMission5
}
if Errorlevel = 0 ; If missions IS Found.
{
sleep 143
Send D
Sleep 160
Send w
Sleep 166
Send w
Sleep 129
Send w
Sleep 177
Send w
Sleep 167
Send w
Sleep 146
Send w
Sleep 172
Send w
Sleep 112 ;Top of mission list selected
Label25kSelected4:
Sleep 66
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k_selected.bmp ;Did it select the 25k Mission?
{
sleep 250
If Errorlevel = 1 ;If Mission ISN'T selected.
{
sleep 376
send s
sleep 185
gosub Label25kSelected4 ;If it CAN'T find the mission by pressing 'S' once, it will retry untill it does.
}
If ErrorLevel = 0 ;If Mission IS selected.
{
send w
sleep 50
send w
sleep 50
send w
sleep 50
send {Space}
sleep 360
Send D
Sleep 260
send {Space}
Sleep 700
send {Space}
Sleep 700
send {Space}
sleep 1231
send {backspace}
sleep 322
Send {backspace}
sleep 241
send {backspace}
sleep 3251
gosub Labelstart ;Mission is turned in, going back to start to see the refresh if there is anymore.
}
}
}
}
;Select 5th company. Any missions?
LabelMission5:
ImageSearch pointX9, pointY9, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k.bmp
{
Sleep 131
if Errorlevel = 1 ; If mission ISN'T found.
{
Sleep 350
Send s
sleep 360
send {Space}
Sleep 313
gosub LabelMission6
}
if Errorlevel = 0 ; If missions IS Found.
{
sleep 143
Send D
Sleep 160
Send w
Sleep 166
Send w
Sleep 129
Send w
Sleep 177
Send w
Sleep 167
Send w
Sleep 146
Send w
Sleep 172
Send w
Sleep 112 ;Top of mission list selected
Label25kSelected5:
Sleep 66
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k_selected.bmp ;Did it select the 25k Mission?
{
sleep 250
If Errorlevel = 1 ;If Mission ISN'T selected.
{
sleep 376
send s
sleep 185
gosub Label25kSelected5 ;If it CAN'T find the mission by pressing 'S' once, it will retry untill it does.
}
If ErrorLevel = 0 ;If Mission IS selected.
{
send w
sleep 50
send w
sleep 50
send w
sleep 50
send {Space}
sleep 360
Send D
Sleep 260
send {Space}
Sleep 700
send {Space}
Sleep 700
send {Space}
sleep 1231
send {backspace}
sleep 322
Send {backspace}
sleep 241
send {backspace}
sleep 3251
gosub Labelstart ;Mission is turned in, going back to start to see the refresh if there is anymore.
}
}
}
}
;Select 6th company. Any missions?
LabelMission6:
ImageSearch pointX11, pointY11, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k.bmp
{
sleep 385
if Errorlevel = 1 ; If mission ISN'T found.
{
sleep 273
gosub LabelSoloPrivate ;If at this point it CAN'T find anymore missions to turn in, it will go to gamemode check.
}
if Errorlevel = 0 ; If missions IS Found.
{
sleep 143
Send D
Sleep 160
Send w
Sleep 166
Send w
Sleep 129
Send w
Sleep 177
Send w
Sleep 167
Send w
Sleep 146
Send w
Sleep 172
Send w
Sleep 112 ;Top of mission list selected
Label25kSelected6:
Sleep 66
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Donate25k_selected.bmp ;Did it select the 25k Mission?
{
sleep 250
If Errorlevel = 1 ;If Mission ISN'T selected.
{
sleep 376
send s
sleep 185
gosub Label25kSelected6 ;If it CAN'T find the mission by pressing 'S' once, it will retry untill it does.
}
If ErrorLevel = 0 ;If Mission IS selected.
{
send w
sleep 50
send w
sleep 50
send w
sleep 50
send {Space}
sleep 360
Send D
Sleep 260
send {Space}
Sleep 300
send {Space}
Sleep 300
send {Space}
sleep 231
send {backspace}
sleep 822
Send {backspace}
sleep 841
send {backspace}
sleep 851
gosub Labelstart ;Mission is turned in, going back to start to see the refresh if there is anymore.
}
}
}
}
;Back to Main Menu, Were we in Solo or Private?
LabelSoloPrivate:
sleep 678
send {Escape}
sleep 700
ImageSearch pointX5, pointY5, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Mode_Solo_Play.bmp ;Is the game mode set on Solo play?
{
Sleep 284
If ErrorLevel = 1 ;If game mode ISN'T solo play
{
sleep 786
gosub LabelModeIsPrivate
MsgBox, 0, Current Gamemode, Gamemode is private!
sleep 6000
}
If ErrorLevel = 0 ;If game mode IS Solo Play
{
Sleep 600
gosub LabelModeIsSolo
MsgBox, 0, Current Gamemode, Gamemode is solo!
Sleep 6000
}
}
;If solo, select private. -- If Private, select solo.
LabelModeIsSolo:
{
Sleep 256
send w
Sleep 612
send w
sleep 302
send {space}
sleep 630
send d
sleep 234
send {space}
sleep 15000 ;Waiting for Main Menu to load.
send s
sleep 953
send {space} ;Selecting start.
Sleep 943
send s
Sleep 823
send {space} ;Selecting Private group.
Sleep 723
send {space} ;Starting Private group.
Sleep 18213 ;Waiting for game to start.
gosub labelstart
}
LabelModeIsPrivate:
{
Sleep 502
send w
Sleep 682
send w
sleep 511
send {space}
sleep 687
send d
sleep 799
send {space}
sleep 17631 ;Waiting for Main Menu to load.
send s
sleep 653
send {space} ;Selecting start.
Sleep 943
send s
Sleep 723
send s
Sleep 623
send {space} ;Selecting solo.
Sleep 16347 ;Waiting for game to start.
gosub labelstart
}
;Eat, sleep, rave. Repeat.
^r::ExitApp ;Kills the script, if it goes haywire.

Nevermind, I completely screwed up and I see now why. Thanks for the help guys....
EDIT: VERY WELL.
The whole script is just plain terrible like i said in the original post. As for what is wrong with it, not with the help of anyone here, even though I know there is people out there who have seen it and seen the mistakes and decided to just not help.
The IF statements and Searchimage were set within the wrong { }. As in the whole build of the script made no sense, its a miracle it even worked to begin with.
This is for starters what it should have looked like.
ImageSearch pointX, pointY, 0, 0, 1920, 1080, *125 C:\Users\ICECOLD\Desktop\new_test\Starport_services.bmp ;Can we see starport services?
{
sleep 150
If ErrorLevel = 1 ; If it CAN'T find Starport Services, it will look for Mission Board instead.
{
sleep 150
ImageSearch pointX2, pointY2, 0, 0, 1920, 1080, *100 C:\Users\ICECOLD\Desktop\new_test\Mission_Board.bmp ;Can we see Mission Board?
if ErrorLevel = 1 ;If it CAN'T find mission board, it will ask for it.
{
MsgBox, 48, Something went wrong!, I can't find "Mission Board" or "Starport Services" anywhere.
MsgBox, 4132, Something went wrong!, Do you wish to retry?
IfMsgBox Yes
{
MsgBox, 4160, Restarting, Click OK and switch back to game with "Starport Services" Open!! `n(YOU GET 15 SECONDS TIME)
sleep 15000
gosub labelstart
ExitApp
If ErrorLevel = 0 ;If it finds Mission Board it will navigate to the Mission Board.
{
Sleep 143
SendEvent s
Sleep 177
SendEvent {Space}
sleep 10000
gosub LabelMissionBoard
If ErrorLevel = 0 ;If it finds Starport services it will navigate to the Mission Board.
{
Sleep 200
SendEvent {space}
sleep 15234
SendEvent s
sleep 134
SendEvent {Space}
sleep 10000
} gosub LabelMissionBoard
}
}
}
}
}
As you can see in the code above as opposed to what the start of the code I posted originally. Which has multiple things happening at the same time, so instead of stopping after having found a image it just kept on going regardless, it did NOT however press the buttons only count the 'sleep' for whatever reason it did that, I have no f&^*( clue, because I'm basically a beginner at this point. A salty one nonetheless. So there's your darn answer, now excuse me while I never come back here again.

Related

Inverse modulo operations in bash

I am trying to write a bash script that will be able to do inverse modulo operations.
Currently the program can do regular mod calculations but trying to do inverse modulo operations leaves me with wrong answers (either prints 0's or wrong answers).
Is it even possible to do inverse calculations, if so what would be the formula?
Expected Result:
28 14 22 30 18 32 30 12 25 37 8 31 18 4 37 3 33 35 27 2 4 3 28
Gotten Results:
-23 -4 -29 -27 -17 -10 -27 -25 -24 -11 -37 -5 -17 -32 -11 -15 -6 -35 -39 -22
#!/bin/bash
flag_enc=(268 413 110 190 426 419 108 229 310 379 323 373 385 236 92 96 169 321 284 185 154 137 186) #inputs
for i in "${flag_enc[#]}"
do
mod=$((1 / $i)) # input^(-1)
modus=$(($mod % 41)) #mod41 calculation
echo "$modus"
done
Oguz Ismail gave the right mathematical expression for calculating inverse mod in bash. I am attaching the modified the script bellow:
flag_enc=(268 413 110 190 426 419 108 229 310 379 323 373 385 236 92 96 169 321 284 185 154 137 186)
m=41
for i in "${flag_enc[#]}"
do
for ((x = 1; x < m; x++)); do
if ((($i % m) * (x % m) % m == 1)); then
echo $x
fi
done
done
I don't know if your algorithm is correct or not, but bash doesn't support floating point arithmetic intrinsically.
So either use bc, or define a function like below
mmi() {
a=$1
m=$2
for ((x = 1; x < m; x++)); do
if (((a % m) * (x % m) % m == 1)); then
echo $x
return 0
fi
done
return 1
}
and use it like so:
$ mmi 268 41
28
Better use a dedicated tool or programming language. Example with the gmpy2 module of python (without error handling, this is left as an exercise):
for i in "${flag_enc[#]}"; do
mod=$(printf 'import gmpy2\nprint(int(gmpy2.invert(%d, %d)))\n' "$i" "41" | python)
echo "$mod"
done

oracle v$session - inactive and waiting

I have read that the STATUS INACTIVE is harmless and not locking any further queries. but at the same time the STATE says WAITING. And my program is actually keeps waiting for a query to execute. I do not have privilage to Kill any queries.
So how can this queries get terminated? Or how to check the max time out for these queries?. Also can anyone tell me how much time is this ? 8147476214 is it milliseconds ? i get insane hours if devided by 60 3 times
WAIT_TIME_MICRO TIME_REMAINING_MICRO SID SERIAL# STATUS STATE
8147476214 -1 363 17919 INACTIVE WAITING
8147060408 -1 600 30087 INACTIVE WAITING
1288463255 -1 1522 31615 INACTIVE WAITING
1288463255 -1 1433 19943 INACTIVE WAITING
1288463239 -1 204 3751 INACTIVE WAITING
1288463217 -1 203 17423 INACTIVE WAITING
1288463217 -1 300 25717 INACTIVE WAITING
1288463204 -1 703 12277 INACTIVE WAITING
1288463193 -1 361 10209 INACTIVE WAITING
1288463144 -1 457 35447 INACTIVE WAITING
1288082277 -1 102 49705 INACTIVE WAITING
1288082274 -1 216 14829 INACTIVE WAITING
1288082274 -1 58 62561 INACTIVE WAITING
1288082273 -1 308 11485 INACTIVE WAITING
1288082273 -1 497 45153 INACTIVE WAITING
1288082258 -1 1289 15497 INACTIVE WAITING
1288082243 -1 1526 48083 INACTIVE WAITING
1288082224 -1 992 34411 INACTIVE WAITING
300678 -1 451 11433 ACTIVE WAITING
261893 -1 304 30921 ACTIVE WAITING
241839 -1 50 32641 ACTIVE WAITING
228894 -1 11 42861 ACTIVE WAITING
79480 -1 996 19657 ACTIVE WAITING
40677 -1 896 28523 ACTIVE WAITING
25609 -1 845 55041 ACTIVE WAITING
12517 -1 115 25819 ACTIVE WAITING
2 54 47749 ACTIVE WAITED SHORT TIME
2 1283 50745 ACTIVE WAITED SHORT TIME
0 -1 548 51351 ACTIVE WAITING
0 0 405 44925 ACTIVE WAITING

Why is the output of this bash script different every time it is run?

I have the following short bash script:
#!/bin/bash
cp /dev/null out.sh
while IFS=' ' read -r b c d e
do
echo -ne "sendevent " >> out.sh
echo -ne "/dev/input/event1 " >> out.sh
echo -ne "$(( 0x$c )) " >> out.sh
echo -ne "$(( 0x$d )) " >> out.sh
echo -ne "$(( 0x$e )) " >> out.sh
echo >> out.sh
done < "in.sh"
It takes in commands read from another script called in.sh in the form of /dev/input/event1: 0003 0039 0000006d, and I need it to output to another script called out.sh in the form of sendevent /dev/input/event1 3 57 109, where it converts the latter three terms from hexadecimal to decimal.
However, when I run the script, I get a quasi-broken output, which changes each time I run it. The correct output should be along the lines of:
sendevent /dev/input/event1 3 57 109
sendevent /dev/input/event1 3 53 40
sendevent /dev/input/event1 3 54 620
#and so on
Here's a picture of my actual output:
Every time I run the script, the output is slightly different, and never uniform as I want it. Why does it output differently every time? How do I fix it so it outputs in the form I want it to?
I've tried adding in sleep .01 after the last echo statement, and it did not fix the problem.
EDIT: As requested, here is a snippet of my in.sh:
/dev/input/event1: 0003 0039 0000006d
/dev/input/event1: 0003 0035 00000028
/dev/input/event1: 0003 0036 0000026c
/dev/input/event1: 0001 014a 00000001
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0003 0039 ffffffff
/dev/input/event1: 0001 014a 00000000
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0003 0039 0000006e
/dev/input/event1: 0003 0035 0000020f
/dev/input/event1: 0003 0036 000003dd
/dev/input/event1: 0001 014a 00000001
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0003 0039 ffffffff
/dev/input/event1: 0001 014a 00000000
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0003 0039 0000006f
/dev/input/event1: 0003 0035 000001b6
/dev/input/event1: 0003 0036 00000076
/dev/input/event1: 0001 014a 00000001
/dev/input/event1: 0000 0000 00000000
/dev/input/event1: 0003 0039 ffffffff
And here are two different outputs, run one after another, with the same in.sh:
sendevent /dev/input/event1 3 109
sendevent /dev/input/event1 3 54 620
sendevent /dev/input/event1 1 330 1
sendevent /dev/input/event1 0 0 0
sendevent /dev/input/event1 3 57 4294967295
/dev/input/event1 1 330 0
sendevent /dev/input/event1 0 0
sendevent /dev/input/event1 3 57 110
3 53 527
sendevent /dev/inp3 54 989
/dev/input/event1 1 330 1
sendevent 3 57 4294967295
sendevent /dev/input/event1 /event1 1 330 0
sendevent /dev/input/event1 0 0 0
sendevent /dev/input/event1 57 111
sendevent /dev/input/event1 3 53 438 sendevent /dev/input/event1 3 54
sendevent /dev/input/event1 1 1
sendevent
sendevent /dev/input/event1 57 4294967295
/dev/input/event1 1 330 /dev/input/event1 0 0 0
sendevent /dev/input/event1 3 57 112
sendevent /dev/input/event1 3 53
sendevent /dev/input/event1 3 54 881
sendevent /dev/input/event1
sendevent /dev/input/event1 3 57 t1 0 0 0 4294967295
sendevent 1 330 0
sendevent/dev/input/event1 0 0 0
113
sendevent /dev/input/event1 3 53
sendevent /dev/input/event1 54 901
/dev/input/event1 1 330 1
/dev/input/event1 0 0 0 sendevent /dev/input/event1 3 57 4294967295
330 0
And the second output:
sendevent /dev/input/event1 3 57 109
sendevent /dev/input/event1 3 53 620
sendevent /dev/input/event1 54 1 330 1 /dev/input/event1 0 0 0 sendevent /dev/input/event1 3 57
sendevent /dev/input/event1 1 330
sendevent /dev/input/event1 0 0 0
sendevent /dev/input/event1 3 57 110
/dev/input/event1 3 53
sendevent /dev/input/event1 54 989
/dev/input/event1 1 330
sendevent /dev/input/event1 0 0
sendevent 3 57 4294967295
sendevent /dev/input/event1 1 0
sendevent 0 0 0
put/event1 sendevent /dev/input/event1 3 111
sendevent 3 53 438
/event1 sendevent /dev/input/event1 3 54
sendevent /dev/input/event1 330 1
0 0 0
nt /dev/input/event1 sendevent /dev/input/event1 3 4294967295
sendevent 0
sendevent /dev/input/event1 0 0 0
sendevent /dev/input/event1 3 57 112
sendevent /dev/input/event1 3 53 247
sendevent /dev/input/event1 3 54 881 sendevent /dev/input/event1 1 330 1
/dev/input/event1 0 0
sendevent /dev/input/event1 57 4294967295
sendevent /dev/input/event1 1 330
sendevent /dev/input/event1 0 0 0
sendevent 3 57 113
sendevent 3 53 246
/event1 sendevent /dev/input/event1 3 54
sendevent /dev/input/event1 330 1
/dev/input/event1 0 0 0
/dev/input/event1 3 57 4294967295 sendevent /dev/input/event1 1 330
sendevent /dev/input/event1 0 0
sendevent 3 57 114
/event1 sendevent /dev/input/event1 3 53
sendevent /dev/input/event1 3 54 882 sendevent /dev/input/event1 1 1
sendevent /dev/input/event1 0 0 0 sendevent /dev/input/event1 3 sendevent /dev/input/event1 1 0
sendevent 0 0 0
The below uses an initial grep pass to sanitize input, avoiding potential shell injection attacks:
#!/bin/sh
grep '^[^ ]\+ [0-9a-f ]\+$' <in.sh | while IFS=' ' read -r _ c d e; do
printf 'sendevent /dev/input/event1 %d %d %d\n' \
"$(( 0x$c ))" "$(( 0x$d ))" "$(( 0x$e ))"
done >out.sh
Note:
out.sh is only opened a single time, rather than reopened before every command that wants to write to it, and closed after the end of that individual command (as happens with a >> operator on each write).
Using printf once per line ensures that each write happens as an atomic syscall (when short enough lines are in use, as will reliably be the case here), and also avoids depending on behavior which the POSIX specification for echo explicitly declines to specify; the output of echo -n is explicitly undefined by the standard, whereas treating -e as an option rather than argument is actually contrary to the letter of the standard (and bash's default behavior doing so can be turned off, as with the combination of set -o posix and shopt -s xpg_echo).

Bash $RANDOM exceeding bounds

I'm having a little bit of trouble with a bash script that is intended to produce a random number between 100 and 999.
My script is as follows:
for i in {1..1000}
do
pin=$((( $RANDOM % 999) +100))
echo $pin
done
Below is a sample of the output. As you can see, some of the numbers exceed 999. What might be going on here?
626
901
1094
456
290
1047
265
221
483
626
848
198
879
474
993
205
200
229
391
325
306
201
800
1004
694
While ($RANDOM % 999) is in range 0 to 999, your output is (($RANDOM % 999) + 100), which is in range 100 to 1099. Is (($RANDOM + 100) % 999) instead of (($RANDOM % 999) + 100) what you want?
To emit 1000 numbers between 100 and 999:
for ((i=0; i<1000; i++)); do
echo "$(( ( RANDOM % 900 ) + 100 ))"
done
Keep in mind that 900 % 900 is 0, meaning that the maximum value possible as output is 899, so if you want your values (after addition) to be through 999 inclusive, you use 900, not 899.

can anyone explain this command fully adb shell sendevent [device] [type] [code] [value]? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
Can anyone explain the following command fully:
adb shell sendevent [device] [type] [code] [value]
I am trying to write a script for touch events using send event command.
First you need to find out the name of the touchscreen device on your phone or tablet. You can use this command in adb shell session:
getevent -pl 2>&1 | sed -n '/^add/{h}/ABS_MT_TOUCH/{x;s/[^/]*//p}'
Let's say the input device name is /dev/input/event0 and you want to emulate a quick tap at coordinates x=300, y=400:
sendevent /dev/input/event0 3 53 300
sendevent /dev/input/event0 3 54 400
sendevent /dev/input/event0 3 48 5
sendevent /dev/input/event0 3 58 50
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
The long touch (let's say 1sec long) at the same coordinates would be:
sendevent /dev/input/event0 3 53 300
sendevent /dev/input/event0 3 54 400
sendevent /dev/input/event0 3 48 5
sendevent /dev/input/event0 3 58 50
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
sleep 1
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
For the explanation what those commands mean and do exactly please read Emulating touchscreen interaction with sendevent in Android.
Im using ZTE blade(1.gen) CyanagenMod 7. The screen resolution is 480x800 After some trial and error I figured out that in order to press coordinates 240x 725y I actually had to implement the script like this: 988 = 240x, 2768 = 725y
sendevent /dev/input/event0 3 53 988
sendevent /dev/input/event0 3 54 2768
sendevent /dev/input/event0 3 48 5
sendevent /dev/input/event0 3 58 50
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
sendevent /dev/input/event0 0 2 0
sendevent /dev/input/event0 0 0 0
I was previously recording the events with get event and values it was giving were correct I just did not know how to interpret them = ).

Resources