在java中运行shell文件
在在java中运行shell文件,java,linux,shell,Java,Linux,Shell,在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 ProcessBuil
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);
java -cp experiments-1.0-SNAPSHOT.jar ConsoleReader < commands.txt
对。但是我有一个shell,它在执行之后给出一个参数。
我怎样才能通过那个论点?我不希望用户键入任何内容来运行此shell文件。换句话说,我想完全自动化shell文件。
Runtime.getRuntime().exec(“src/[file LOCATION]”)代码>
我想这就是你要找的命令。让我知道它是否有效 如果您想用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);
java -cp experiments-1.0-SNAPSHOT.jar ConsoleReader < commands.txt
或者,您可以使用相同的程序允许用户在命令行上执行命令
下面您可以找到一个示例程序,您可以按照上述方式编译和运行该程序
它有什么作用
它将java.util.Scanner
挂接到控制台输入并使用每一行李>
然后它生成两个线程,它们侦听错误和输入流,并将其写入stderr
或stdin
控制台上的空行将被忽略
如果您键入“read”,它将在该文件上执行命令
资料来源:
public class ConsoleReader {
public static void main(String[] args) throws IOException, DatatypeConfigurationException {
try(Scanner scanner = new Scanner(new BufferedInputStream(System.in), "UTF-8")) {
readFromScanner(scanner);
}
}
private static final Pattern FILE_INPUT_PAT = Pattern.compile("read\\s*([^\\s]+)");
private static void readFromScanner(Scanner scanner) {
while (scanner.hasNextLine()) {
try {
String command = scanner.nextLine();
if(command != null && !command.trim().isEmpty()) {
command = command.trim();
if("exit".equals(command)) {
break; // exit shell
}
else if(command.startsWith("read")) { // read from file whilst in the shell.
readFile(command);
}
else {
Process p = Runtime.getRuntime().exec(command);
Thread stdout = readFromStream(p.getInputStream(), System.out, "in");
Thread stderr = readFromStream(p.getErrorStream(), System.err, "err");
stdout.join(200);
stderr.join(200);
}
}
}
catch(Exception e) {
Logger.getLogger("ConsoleReader").log(Level.SEVERE, String.format("Failed to execute command %s", e));
}
}
}
private static void readFile(String command) throws FileNotFoundException {
Matcher m = FILE_INPUT_PAT.matcher(command);
if(m.matches()) {
String file = m.group(1);
File f = new File(file);
if (f.exists()) {
try (Scanner subScanner = new Scanner(f)) {
readFromScanner(subScanner);
}
}
}
else {
System.err.printf("Oops, could not find '%s'%n", command);
}
}
private static Thread readFromStream(InputStream stdin, PrintStream out, String name) throws IOException {
Thread thread = new Thread(() -> {
try (BufferedReader in = new BufferedReader(new InputStreamReader(stdin))) {
String line;
while ((line = in.readLine()) != null) {
out.println(line);
}
} catch (IOException e) {
Logger.getLogger("ConsoleReader").log(Level.SEVERE, "Failed to read from stream.", e);
}
}, name);
thread.setDaemon(true);
thread.start();
return thread;
}
}
你是说shell脚本需要在STDIN上输入?顺便说一句,如果“苏”这样工作,我会感到惊讶。它实际上只从实际终端(或命令行)获取密码。如果这是你想要的,我找到了一个解决方案(让Java“su”成为它自己的副本)。你看到公众免费的在线文档了吗?我认为link是错误的,它将我引向Locale.classe你能用sudo
而不是su
来运行你的任务吗?不,我不想只运行一个shell文件,因为我说过,我的shell在执行shell文件之前会有两种参数,在执行shell文件之后会有另一种参数,我的问题是第二种类型。在运行shell之后如何放置参数运行shell之后的参数是什么意思?