如何在 Android 设备上运行 Linux 命令?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10277709/
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-20 03:06:41  来源:igfitidea点击:

How can I run Linux commands on an Android device?

androidshelladb

提问by Wendy Chen

On some Android devices, in the ADB shell, I can only run echo, cd, ls. When I run:

在某些 Android 设备上,在 ADB shell 中,我只能运行echo, cd, ls. 当我运行时:

tar -cvf //mnt/sdcard/BackUp1669/apk/test.tar /mnt/sdcard/test.apk

Or the command cp, it returns:

或者命令cp,它返回:

sh: tar: not found

Why can I not run these commands? Some devices support these commands. My end goal is to copy a file from the /data/datafolder to SD card. I got suand I got the following code:

为什么我不能运行这些命令?一些设备支持这些命令。我的最终目标是将文件从/data/data文件夹复制到 SD 卡。我得到了su并且得到了以下代码:

int timeout = 1000;
String command = "tar -cvf /" + Environment.getExternalStorageDirectory() + "/cp/"
        + packageName + ".tar" + " " + path;
DataOutputStream os = new DataOutputStream(process.getOutputStream());
BufferedReader is = new BufferedReader(new InputStreamReader(new DataInputStream(
        process.getInputStream())), 64);

String inLine;
try {
    StringBuilder sbCommand = new StringBuilder();
    sbCommand.append(command).append(" ");
    sbCommand.append("\n");
    os.writeBytes(command.toString());
    if (is != null) {
        for (int i = 0; i < timeout; i++) {
            if (is.ready())
                break;
            try {
                Thread.sleep(5);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        if (is.ready()) {
            inLine = is.readLine();
        } else {
        }
    }

} catch (IOException e) {
    e.printStackTrace();
}

It always stops in is.ready(), and when I changed it to process.waitfor()it also stopped. Why?

它总是停在is.ready(),当我改变它时,process.waitfor()它也停了下来。为什么?

回答by waqaslam

As far as i know, the only way to run shell commands is:

据我所知,运行 shell 命令的唯一方法是:

Process proc = Runtime.getRuntime().exec("your command");

回答by keyboardsurfer

You can run Linux commands on Android. But there are usually just very few pre-installed. If you want to add more commands you might want to rootyour device and install busyboxon it.

您可以在 Android 上运行 Linux 命令。但通常只有很少的预装。如果您想添加更多命令,您可能需要root设备并在其上安装busybox

This is not for productive use within an application but can help you to work with your device.

这不适用于应用程序中的生产用途,但可以帮助您使用您的设备。

回答by onur güng?r

If you have the binaries for your system, you can run anything on your system.

如果你有系统的二进制文件,你可以在你的系统上运行任何东西。

Saying that you have to understand that you have to find the binaries for tar.

说你必须明白你必须找到tar.

Look here http://forum.xda-developers.com/showthread.php?t=872438

看这里http://forum.xda-developers.com/showthread.php?t=872438

And possibly other places..

可能还有其他地方..

回答by CorgiButt Studios

You can probably get this done by using a Terminal Emulator app. As you wrote above, I don't know how well DOS commands will work. But, a Terminal Emulator works without root.

您可能可以通过使用终端模拟器应用程序来完成此操作。正如你上面写的,我不知道 DOS 命令的效果如何。但是,终端模拟器无需 root 即可工作。

回答by MahaRaja

Install busybox, then type the command in the following format:

安装busybox,然后按以下格式键入命令:

busybox [linux command]

busybox [linux 命令]

You cannot use all the linux commands without busybox, because Android doesn't have all the binaries that are available in a standard linux operating system.

没有busybox,你不能使用所有的linux命令,因为Android没有标准linux操作系统中可用的所有二进制文件。

FYI, a binary is just a file that contains compiled code. A lot of the default binaries are stored in /system/bin/sh directory. All these commands like 'cp' 'ls' 'get' etc, are actually binaries. You can view them through:

仅供参考,二进制文件只是一个包含编译代码的文件。许多默认二进制文件存储在 /system/bin/sh 目录中。所有这些命令,如 'cp' 'ls' 'get' 等,实际上都是二进制文件。您可以通过以下方式查看它们:

ls -a /system/bin/sh

ls -a /system/bin/sh

Hope this helps.

希望这可以帮助。

回答by Don Giovanni

In reply to Igor Ganapolsky, you would have to have a database set up for locate.

在回复 Igor Ganapolsky 时,您必须为 locate 设置一个数据库。

Probably 'find' would be adequate for your needs.

可能'find'就足以满足您的需求。

exmample:

例子:

find -name *.apk

find -name *.apk