Android 在应用程序中运行 bash 命令

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

Android run bash command in app

javaandroidbash

提问by user577732

Hi i'm developing an application which requires me to run some bash code is there a way i can hard code the script into my app and then run it? For instance (this is a VERY simplified example)

嗨,我正在开发一个需要我运行一些 bash 代码的应用程序,有没有办法将脚本硬编码到我的应用程序中然后运行它?例如(这是一个非常简化的例子)

#!/system/bin/sh
#

if [ ! -f /sdcard/hello.txt ]
then
echo 'Hello World' >> /sdcard/hello.txt
else
echo 'Goodbye World' >> /sdcard/goodbye.txt
fi

I have the following method for running one line bash commands but need to run something like that that's on multiple lines. Again that above code is a very simplified example what I am actually doing must be run through a script and can't be done through java. I also want to have it hard coded I know could have the script stored on the phone and run it with the following but do not want the script just out there would rather it hard coded in the app.

我有以下方法来运行一行 bash 命令,但需要在多行上运行类似的东西。同样,上面的代码是一个非常简化的示例,我实际在做的事情必须通过脚本运行,而不能通过 java 来完成。我也想对其进行硬编码,我知道可以将脚本存储在手机上并使用以下命令运行它,但不希望脚本就在那里,而是在应用程序中对其进行硬编码。

public Boolean execCommand(String command) 
    {
        try {
            Runtime rt = Runtime.getRuntime();
            Process process = rt.exec("su");
            DataOutputStream os = new DataOutputStream(process.getOutputStream()); 
            os.writeBytes(command + "\n");
            os.flush();
            os.writeBytes("exit\n");
            os.flush();
            process.waitFor();
        } catch (IOException e) {
            return false;
        } catch (InterruptedException e) {
            return false;
        }
        return true; 
    }

Thank you for any help with my issue

感谢您对我的问题的任何帮助

回答by Edwardly

If I understand you correctly, all you have to do is change the one line example method to something which accepts and sends multiple lines, like so:

如果我理解正确,您所要做的就是将一行示例方法更改为接受和发送多行的方法,如下所示:

    public Boolean execCommands(String... command) {
    try {
        Runtime rt = Runtime.getRuntime();
        Process process = rt.exec("su");
        DataOutputStream os = new DataOutputStream(process.getOutputStream());

        for(int i = 0; i < command.length; i++) {
            os.writeBytes(command[i] + "\n");
            os.flush();
        }
        os.writeBytes("exit\n");
        os.flush();
        process.waitFor();
    } catch (IOException e) {
        return false;
    } catch (InterruptedException e) {
        return false;
    }
    return true; 
}

That way, you can call your multiline bash commands like so:

这样,您可以像这样调用多行 bash 命令:

    String[] commands = {
            "echo 'test' >> /sdcard/test1.txt",
            "echo 'test2' >>/sdcard/test1.txt"
    };

    execCommands(commands);

    String commandText = "echo 'foo' >> /sdcard/foo.txt\necho 'bar' >> /sdcard/foo.txt";

    execCommands(commandText.split("\n"));