Have you ever wanted to be free from performing tedious tasks in Android? Have you ever wanted to write android shell scripts? Well you are not the only one. This article shows you how to write android shell scripts, which helps you in easing your jobs.
Busy Box & Terminal Emulator for Android Shell Scripting
In your rooted android device, install an app called BusyBox. It provides most of the *nix utilities such as awk, sed, find, sort, head, etc., which will be useful when you try to write a shell script to run on Android. Then install Terminal Emulator (You can also use other similar tools, but I use Terminal Emulator).
BUSYBOX ON GOOGLE PLAY
Simple Shell Scripting in Android
This method does not require to root your android device. Just the Terminal Emulator is required to run the shell scripts you created. Refer the link above for getting Terminal Emulator.Writing Shell Scripts in Android
You can write Shell Scripts in your PC and transfer it to your Android device, or else, write them directly on your Android device. Start writing your android shell script by using the following as the first line (Also known as Shebang)
#!/system/bin/sh
Running Shell Scripts in Android
Then save your entire script and open Terminal Emulator. Now run the script using sh command.
sh /
Here is the script that I created:
#!/system/bin/sh
echo "Voila!! Shell script works on Android"
And here is what I get in the terminal emulator:
Shell Script in Android
Only for rooted devices: If you want the script to be run directly as a command, place the script in /system/bin (or) /system/xbin. Then make the file executable by running the following command:
su
mount -o rw,remount /system
cp / /system/bin/
chmod 555 /system/bin/
For example I have copied the script "testmsg.sh" to /system/bin, renamed it to "testmsg" using the following commands:
su
/mount -o rw,remount /system
cp /storage/sdcard0/sh/testmsg.sh /system/bin/testmsg
chmod 555 /system/bin/testmsg (chmod +x did not work, giving me a bad mode error)
Now in the Terminal Emulator, just enter the name of the file and the script will execute.
Shell Script as a command in Android
I hope that wasn't a long winded approach for writing the shell scripts in Android. If you've got other ways for attaining the same result, kindly share them in the comments.