loop in DOS and Linux shell scripts

A couple of friends was asking how to do loop in shell scripts. Here is some simple code.
To loop in DOS:
@set /p start="From:"
@set /p end="To:"


@REM test condition
@IF %start% GTR %end% (GOTO wend)

@REM procedure where condition is "true"
@REM replace with real operations
@echo %start%

@REM set new test value
@SET /a start=start+1

@REM loop
@GOTO while


This will print the first number to the last number you input on the screen. Replace @echo %start% with your real operation.

However, DOS script does not handle real numbers. So if I want to loop from 1 to 10, and print the number + 0.5, this will not work. One solution is using Perl. Second, using Linux. Here is the code for Linux.

echo -n "Enter starting code> "
read start
echo -n "Enter ending code> "
read end

#start loop
for ((i=start; i<=end;i++))
echo "$i+0.1" | bc

If you input 1 and 5, this is print 1.5, 2.5, 3.5, 4.5, 5.5.

Note: I did not create these codes. They are from Google search. However, they are from different sources (especially the use of bc) so I lost the source link.


Android, adb command to send “touch” screen events to ICS devices (and now Jelly Bean)

Thanks to SoftTeco. I found how to send touch event directly to Android devices. However, his code works for 2.3.3, but not ICS and Jelly Bean. (I did not test 3.x).

However, his great blog not only gives you the code, but also tells you how to get it. After searching valid code for ICS without any result, I just catch the codes using
adb shell getevent | grep event2
Then convert Hex to decimal.
Here is the code to touch the center of my GS2 (240, 400) running AOKP ICS 4.0.X ( I am lazy, this is NOT the newest version of AOKP)
adb shell sendevent /dev/input/event2 3 57 29
adb shell sendevent /dev/input/event2 3 53 240
adb shell sendevent /dev/input/event2 3 54 400
adb shell sendevent /dev/input/event2 3 48 29
adb shell sendevent /dev/input/event2 3 58 2
adb shell sendevent /dev/input/event2 0 0 0
adb shell sendevent /dev/input/event2 3 57 4294967295
adb shell sendevent /dev/input/event2 0 0 0

As you can see, only 8 lines of code is needed now, versus 16 lines for 2.x.

Also, if you run this on DOS cmd, copy all lines and add one more line of something, and paste it all to execute all 8 lines. If you don’t add one more line, you only run 7 lines and result is unpredictable.

You can also put all of them in one line with no new line break:
adb shell "sendevent /dev/input/event2 3 57 29;sendevent /dev/input/event2 3 53 240;sendevent /dev/input/event2 3 54 400;sendevent /dev/input/event2 3 48 29;sendevent /dev/input/event2 3 58 2;sendevent /dev/input/event2 0 0 0;sendevent /dev/input/event2 3 57 4294967295;sendevent /dev/input/event2 0 0 0;"

I did not test it on Jelly Bean, but I think it should be same.
Again, the credit goes to SOFTTE, I am merely using his method to get the code for a different version of Android.

Revision: Tested on Jelly Bean. Different code. So your best shot is getevent.

Spinner triggered by code?

Did you want to add listener to your spinner to do something, say, play one of the listed sounds? The problem is, when you enter this activity, your code will set the spinner as well. Guess what? It will trigger the listener as well!

Zordid at stackoverflow has a great work around.  Here is how:

  • keep the selected positions in field variables. (like currentPos1, currentPos2)
  • the listeners onItemSelectedListener() call a method like refreshMyResult() or whatever.
  • when setting positions programmatically, set the spinners and call your own refresh method manually right after that.

The refreshMyResult() method looks like this:

int newPos1 = mySpinner1.getSelectedItemPosition();

int newPos2 = mySpinner2.getSelectedItemPosition();

// only do something if update is not done yet

if (newPos1 != currentPos1 || newPos2 != currentPos2) {
              currentPos1 = newPos1;
        currentPos2 = newPos2;
         // do whatever has to be done to update things! 
         //This would be in the "old" listener 

Or, may be customize your spinner. For example, to play a sound in a spinner item, add a play button in the customized spinner item, and use listener for that.