在应用程序的Andr​​oid运行bash命令 [英] Android run bash command in app

查看:92
本文介绍了在应用程序的Andr​​oid运行bash命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我叫发展,需要我来运行一些基本的bash code的应用程序有没有方法可以让我很难code中的脚本到我的应用程序,然后运行它?例如(这是一个非常简单的例子)

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)

#!/system/bin/sh
#

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

我对运行一行的bash命令下面的方法,但需要运行类似的东西那是多条线路上。此外,以上code是一个非常简单的例子什么,我实际上做必须通过脚本运行,无法通过Java来完成。我也希望有难以codeD我知道可以有存储在手机上的脚本,并具有以下运行它,但不希望脚本只是在那里宁愿很难codeD上的应用程序。

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.

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

推荐答案

如果我理解正确的话,你所要做的就是改变一行示例方法的东西,它接受和发送多条线路,像这样:

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; 
}

这样的话,你可以打电话给你多行的bash命令,像这样:

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

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

这篇关于在应用程序的Andr​​oid运行bash命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆