linux脚本android,<转> 如何在安卓设备上跑shell脚本

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-working-on-android.jpg

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.

android-shell-script-as-command.jpg

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值