【发布时间】:2017-12-04 15:24:19
【问题描述】:
在Java 你可以这样调用一个shell文件:
public class Shell {
private static Shell rootShell = null;
private final Process proc;
private final OutputStreamWriter writer;
private Shell(String cmd) throws IOException {
this.proc = new ProcessBuilder(cmd).redirectErrorStream(true).start();
this.writer = new OutputStreamWriter(this.proc.getOutputStream(), "UTF-8");
}
public void cmd(String command) {
try {
writer.write(command+'\n');
writer.flush();
} catch (IOException e) { }
}
public void close() {
try {
if (writer != null) {
writer.close();
if(proc != null) {
proc.destroy();
}
}
} catch (IOException ignore) {}
}
public static void exec(String command) {
Shell.get().cmd(command);
}
public static Shell get() {
if (Shell.rootShell == null) {
while (Shell.rootShell == null) {
try {
Shell.rootShell = new Shell("su"); //Open with Root Privileges
} catch (IOException e) { }
}
}
return Shell.rootShell;
}
}
Shell.exec("echo " + bt.getLevel() + " > "+ flashfile);
对。
但我有一个 shell,它在执行后给出一个参数。
我怎样才能通过那个论点?我不希望用户键入任何内容来运行此 shell 文件。换句话说,我想完全自动化一个 shell 文件。
【问题讨论】:
-
您的意思是 shell 脚本需要在 STDIN 上输入?顺便说一句,如果“su”像这样工作,我会感到惊讶。它实际上只从实际终端(或命令行)获取密码。如果这是你想要的,我找到了一个解决方案(让 Java “su”成为它自己的副本)。
-
你看到公众免费的在线文档了吗? docs.oracle.com/javase/7/docs/api/index.html?java/util/…
-
我认为链接错误,它将我引导至 Locale.class
-
您能否使用
sudo而不是su来运行您的任务?