Linux 如何在我的 Android 中运行 BASH 脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8094562/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 07:08:43  来源:igfitidea点击:

How to run BASH script in my Android?

androidlinuxbashfedorabash4

提问by

My same BASH script is working in Fedora/CentOS.

我的同一个 BASH 脚本在 Fedora/CentOS 中运行。

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

但我正在测试一个Android eee pad transformer在此处输入图片说明

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'";

采纳答案by ASten

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

使用脚本调用解释器时可能会起作用吗?

$ bash ./test.sh

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

我看到,虽然它被指定为#!/bin/bash错误发布者sh- 可能是它做错了。

UPD

UPD

$ sh ./test.sh

回答by Wesley Wiser

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 BusyBoxbut that will require rooting your device (and potentially voiding your warranty). Even then though, I don't know if the ifconfigutility is included in BusyBox.

大多数 Android 设备都没有安装 bash 解释器。如果您确实需要在 Linux 和 Android 上运行脚本,您可以尝试使用BusyBox,但这需要对您的设备进行 root 操作(并且可能会使您的保修失效)。尽管如此,我不知道该ifconfig实用程序是否包含在 BusyBox 中。

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

我强烈建议使用 Android SDK 编写一个应用程序来完成你想要完成的任何事情。

回答by tbg

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

在Android的壳位于/system/bin/sh/bin/sh喜欢它是在大多数Unix类系统。因此,即使您更改#!/bin/bash#!/bin/sh它仍然无法正常工作。你必须使用#!/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.

Android 不是 GNU/Linux 发行版,因此您不能期望在 GNU/Linux 上运行的所有脚本也可以在 Android 上运行。

回答by Namuna

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.

如前所述,Android 操作系统(直到并包括 4.0)不包括 BASH 解释器(只是 shell)。虽然 BusyBox 是一个很棒的工具,但我相信它只是一个单一的可执行文件,它结合了常见 UNIX 实用程序的按大小精简版本的功能,但实际上并不包含 BASH 解释器。

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

对于 BASH 解释器的 Android 编译版本,请参阅此论坛线程:http: //forum.xda-developers.com/showthread.php?t=537827

回答by Kenpachi

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

您可以安装Busybox,它为您提供了许多实用程序,例如 awk、文件等...和Terminal Emulator

  1. Create a shell file with #!/system/bin/shas the first line (shebang)
  2. Now place the completed script under /system/xbinor /system/binand run it from the Terminal Emulator
  1. 创建一个 shell 文件#!/system/bin/sh作为第一行 (shebang)
  2. 现在将完成的脚本放在/system/xbin或下/system/bin并从终端仿真器运行它

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

该信息摘自本文:如何在 Android 设备上运行 Shell 脚本