Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

java - Android run bash command in app

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

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 + "
");
            os.flush();
            os.writeBytes("exit
");
            os.flush();
            process.waitFor();
        } catch (IOException e) {
            return false;
        } catch (InterruptedException e) {
            return false;
        }
        return true; 
    }

Thank you for any help with my issue

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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] + "
");
            os.flush();
        }
        os.writeBytes("exit
");
        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:

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

    execCommands(commands);

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

    execCommands(commandText.split("
"));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...