2014年5月22日 星期四

Using Shell Script on Android

Preface

Shell Script is a good tool to control Android device. You can run the script either by connecting PC or by disconnecting.PC. In order to enter Android's console mode, the ADB is required. Therefore, you can start to use Shell Script on Android. Shell Script is well known on Linux PC to do some scenario control. The kernel of Android is Linux Based. Hence, you can use similar script to control your Android device.

Introduction

Let's learn how to run a Shell Script on Android at first. I'll give an example.
In begging, you need a edit tool for shell script on PC. GEdit is a good choice for you. Note that you need to save your shell script on Linux Format rather than Windows.

Let's get start on writing shell script to run on your Android.
=================================================
#!/bin/sh
#The comments you can write here follows #
#Assign Value 1 to variable named val
val=1
#Assign a String "Hello World" to a variable str1 by export command
export str1="Hello World"
echo "$str1"
#Append a String to its variable by ":"
export str1=$str1:" Hello World again"

#The Screen will show you "Hello World Hello World again"
echo "$str1"

#The Screen will show you "Hello World Hello World again" by infinite loop
while [ true ]
do
   echo "$str1"
done
=================================================

Save this file as Linux format and push this file to Android device by following command.
adb push filename /data/local/tmp/
Execute this shell by following command.
adb shell sh /data/local/tmp/filenmae

The output would be as follows,
Hello World
Hello World: Hello World again
Hello World: Hello World again
Hello World: Hello World again
...

Note
It's unnecessary to have root permission to run the shell script if you place files in the folder of /data/local/tmp/ and don't use the none permitted command.

Keyword: Shell Script, Android

沒有留言:

張貼留言