简体   繁体   English

在 Java 中运行多个 linux 命令

[英]Run multiple linux commands in Java

I'm pretty new to Java and I', currently trying to run multiple linux commands line through a function in Java.我对 Java 还很陌生,我目前正在尝试通过 Java 中的一个函数运行多个 linux 命令行。

For the function I am getting a string called "fileName".对于函数,我得到一个名为“fileName”的字符串。 After that I want to change to a certain directory, execute an .sh and write it into a .txt file, let the thread sleep for 3 seconds so that it can write the text file properly, and finally press "q" to end the .sh execution.之后我想切换到某个目录,执行一个.sh并写入一个.txt文件,让线程休眠3秒,以便它可以正常写入文本文件,最后按“q”结束.sh 执行。 The .sh file has been programmed by someone else and it only seems to exit properly when "q" is pressed into the shell. .sh 文件已被其他人编程,它似乎只有在将“q”按入外壳时才能正确退出。

To clarify a little bit, although my code isn't finished I will post what I have.为了澄清一点,虽然我的代码还没有完成,但我会发布我所拥有的。

public String getNum(String fileName) throws Exception{
Process p = null;
Runtime rt = Runtime.getRuntime();

try {
  p = rt.exec(new String[]{"cd /usr/local/bin", "./test_elanprv2.2.sh > {fileName}.txt", });

}catch (Exception e){

}

} }

Thank you in advance!!先感谢您!!

Process p = new ProcessBuilder("myCommand", "myArg").start();

在 Java 中运行 bash shell 脚本

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM