如何在我的 Android 中运行 BASH 脚本?
我的相同 BASH 脚本在 Fedora/CentOS 中运行。
但我正在测试一个 Android eee pad Transformer
。
我有终端访问权限并编写了一个小测试脚本。但它不起作用,我该如何修复它?我做错了什么?
/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
跟进:
这是我的脚本,现在可以运行:
#!/bin/sh
echo "hello wassup, run me simply as './test.sh'";
or
#!/bin/bash
echo "hello wassup, run me using 'sh ./test.sh'";
My same BASH script is working in Fedora/CentOS.
But I am testing one 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'";
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
用脚本调用解释器时可能会起作用吗?
我看到,虽然指定了
#!/bin/bash
错误是由sh
发布的 - 可能是它做错了。UPD
May be it will work when calling interpreter with a script?
I saw, that although it is specified
#!/bin/bash
error was posted bysh
- may be it do wrong.UPD
大多数 Android 设备没有安装 bash 解释器。如果您确实需要在 Linux 和 Android 上运行脚本,您可以尝试使用 BusyBox 但这需要对您的设备进行root(并可能使您的保修失效)。尽管如此,我还是不知道 BusyBox 中是否包含
ifconfig
实用程序。我强烈建议使用 Android SDK 编写一个应用程序来完成您想要完成的任何任务。
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.
如前所述,Android 操作系统(4.0 及之前版本)不包含 BASH 解释器(仅 shell)。虽然 BusyBox 是一个很棒的工具,但我相信它只是一个单一的可执行文件,结合了常见 UNIX 实用程序的精简功能版本,但实际上并不包含 BASH 解释器。
有关 BASH 解释器的 Android 编译版本,请参阅此论坛主题:
http://forum.xda-developers.com/showthread.php?t=537827
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
您可以安装Busybox,它为您提供了许多实用程序,例如如 awk、文件等...以及终端模拟器。
#!/system/bin/sh
作为第一行 (shebang)/system/xbin
或/system 下/bin
并从终端仿真器运行它该信息摘自本文:如何在 Android 设备上运行 shell 脚本
You can install Busybox, which provides you with many utilities such as awk, file, etc... and Terminal Emulator.
#!/system/bin/sh
as the first line (shebang)/system/xbin
or/system/bin
and run it from the Terminal EmulatorThe information is an excerpt from this article : HOW TO RUN SHELL SCRIPTS ON ANDROID DEVICES
让 bash 脚本运行只是其中最重要的一步。问题是如何在不打开终端模拟器并输入脚本路径的情况下执行此操作。唯一的方法是使用 TCL/TK:
1-) 在 https://www.androwish 下载并安装 AndroWish .org/home/home
2-) 从 Google Store 安装 Busybox
编写启动器:
launcher.tcl
yourscript.sh
通过键入以下内容将桌面快捷方式添加到 AndroWish 终端:
borg快捷方式添加“MyScript”文件://mnt/sdcard/launcher.tcl
Making the bash script run is the least of it. The problem is how to do it without opening some terminal emulator and typing the path to the script. The only way is with TCL/TK:
1-) Download and install AndroWish at https://www.androwish.org/home/home
2-) Install Busybox from Google Store
Write a launcher:
launcher.tcl
yourscript.sh
Add a desktop shortcut to the AndroWish terminal by typing:
borg shortcut add "MyScript" file://mnt/sdcard/launcher.tcl
可以使用Termux。 Termux 是适用于 Android 设备的终端模拟器和 Linux 环境。
Termux can be used. Termux is terminal emulator and Linux environment for Android device.
在 Android 中,shell 位于
/system/bin/sh
中,而不是像大多数类 Unix 系统上的/bin/sh
中一样。因此,即使将#!/bin/bash
更改为#!/bin/sh
它仍然无法工作。您必须使用#!/system/bin/sh
Android 不是 GNU/Linux 发行版,因此您不能指望在 GNU/Linux 上运行的所有脚本也能在 Android 上运行。
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.