How to run BASH script in my Android?

2019-03-09 11:26发布

My same BASH script is working in Fedora/CentOS.

But I am testing one Android eee pad transformer. enter image description here

Where i have terminal access and i wrote a small test script. But its not working, how can i fix it? what am i doing wrong?

/data/data/berserker.android.apps.sshdroid/home $ cat test.sh 
#!/bin/bash
var=`ifconfig -a`;
echo $var;

/data/data/berserker.android.apps.sshdroid/home $ chmod +x test.sh 
/data/data/berserker.android.apps.sshdroid/home $ ./test.sh 
sh: ./test.sh: not found
/data/data/berserker.android.apps.sshdroid/home $ uname -a
Linux localhost 2.6.36.3-00004-g069b8b5 #1 SMP PREEMPT Wed May 11 22:14:22 CST 2011 armv7l GNU/Linux

/data/data/berserker.android.apps.sshdroid/home $ bash ./test.sh 
sh: bash: Permission denied

/data/data/berserker.android.apps.sshdroid/home $ ls -l /bin/bash
ls: /bin/bash: No such file or directory

/data/data/berserker.android.apps.sshdroid/home $ find / -name "bash"
find: /config: Permission denied
lots more.......
find: /proc/595/task/598/fd: Permission denied
......
find: /data: Permission denied
find: /root: Permission denied

Follow up:

This is my script now which works:

#!/bin/sh
echo "hello wassup, run me simply as './test.sh'";

or

#!/bin/bash
echo "hello wassup, run me using 'sh ./test.sh'";

5条回答
Ridiculous、
2楼-- · 2019-03-09 11:54

Most Android devices don't have a bash interpreter installed. If you really need to run the script across Linux and Android, you could try using BusyBox but that will require rooting your device (and potentially voiding your warranty). Even then though, I don't know if the ifconfig utility is included in BusyBox.

I would strongly recommend using the Android SDK to write an app to do whatever your trying to accomplish.

查看更多
我想做一个坏孩纸
3楼-- · 2019-03-09 12:02

May be it will work when calling interpreter with a script?

$ bash ./test.sh

I saw, that although it is specified #!/bin/bash error was posted by sh - may be it do wrong.

UPD

$ sh ./test.sh
查看更多
Melony?
4楼-- · 2019-03-09 12:02

You can install Busybox, which provides you with many utilities such as awk, file, etc... and Terminal Emulator.

  1. Create a shell file with #!/system/bin/sh as the first line (shebang)
  2. Now place the completed script under /system/xbin or /system/bin and run it from the Terminal Emulator

The information is an excerpt from this article : HOW TO RUN SHELL SCRIPTS ON ANDROID DEVICES

查看更多
神经病院院长
5楼-- · 2019-03-09 12:10

in Android the shell is located in /system/bin/sh not /bin/sh like it is on most Unix-like systems. So even if you change #!/bin/bash to #!/bin/sh it will still not work. you'll have to use #!/system/bin/sh

Android is not a GNU/Linux distribution so you can't expect that all scripts that run on GNU/Linux to also work on Android.

查看更多
孤傲高冷的网名
6楼-- · 2019-03-09 12:11

As was stated, the Android OS (up to and including 4.0) does not include the BASH interpreter (just shell). While BusyBox is a great tool, I believe it's only a single executable that combines stripped-down-functionality-for-size versions of common UNIX utilities, but doesn't actually include the BASH interpreter.

For an Android compiled version of the BASH interpreter, refer to this Forum thread: http://forum.xda-developers.com/showthread.php?t=537827

查看更多
登录 后发表回答