我试图合并多个linux命令在一行执行部署操作。 例如

cd /my_folder
rm *.jar
svn co path to repo
mvn compile package install

当前回答

我发现这类问题的很多答案都具有误导性

修改自此文: https://www.webmasterworld.com/linux/3613813.htm

下面的代码将创建bash窗口,并与bash窗口完全相同。希望这能有所帮助。有太多错误的答案了……

            Process proc;
            try {
                //create a bash window
                proc = Runtime.getRuntime().exec("/bin/bash");
                if (proc != null) {
                       BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                       PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
                       BufferedReader err = new BufferedReader(new InputStreamReader(
                       proc.getErrorStream()));
                       //input into the bash window
                       out.println("cd /my_folder");
                       out.println("rm *.jar");
                       out.println("svn co path to repo");
                       out.println("mvn compile package install");
                       out.println("exit");
                       String line;
                        System.out.println("----printing output-----");
                          while ((line = in.readLine()) != null) {
                             System.out.println(line);
                          }
                          while((line = err.readLine()) != null) {
                             //read errors
                          }
                          proc.waitFor();
                          in.close();
                          out.close();
                          err.close();
                          proc.destroy();
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

其他回答

使用;

不管第一个命令cmd1是否成功运行,总是运行第二个命令cmd2: $ CD myfolder;不管CD到我的文件夹是否成功,运行Ls

使用& &

当第一个命令cmd1执行成功后,再执行第二个命令cmd2: $ CD myfolder && ls #只在CD到myfolder后运行ls

使用| |

当第一个命令cmd1执行失败时,执行第二个命令cmd2: $ CD myfolder || ls #如果CD到myfolder失败,' ls '将运行

要一次性运行它们,你可以使用管道键“|”,如下所示:

$ cd /my_folder | rm *.jar | svn co path to repo | mvn compile package install

我发现这类问题的很多答案都具有误导性

修改自此文: https://www.webmasterworld.com/linux/3613813.htm

下面的代码将创建bash窗口,并与bash窗口完全相同。希望这能有所帮助。有太多错误的答案了……

            Process proc;
            try {
                //create a bash window
                proc = Runtime.getRuntime().exec("/bin/bash");
                if (proc != null) {
                       BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
                       PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
                       BufferedReader err = new BufferedReader(new InputStreamReader(
                       proc.getErrorStream()));
                       //input into the bash window
                       out.println("cd /my_folder");
                       out.println("rm *.jar");
                       out.println("svn co path to repo");
                       out.println("mvn compile package install");
                       out.println("exit");
                       String line;
                        System.out.println("----printing output-----");
                          while ((line = in.readLine()) != null) {
                             System.out.println(line);
                          }
                          while((line = err.readLine()) != null) {
                             //read errors
                          }
                          proc.waitFor();
                          in.close();
                          out.close();
                          err.close();
                          proc.destroy();
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

通知你, 如果在If -else中需要在一行中同时执行多条命令,可以使用圆括号将命令组合在一起。详情请参阅本页。

您可以根据需要将分号(;)替换为&&或||。请注意,括号创建了一个子shell来执行命令。

如果存在一个名为file_name的文件,以下命令将执行cmd1、cmd2和cmd3命令;否则为cmd4、cmd5、cmd6。

[ -f file_name ] && (cmd1; cmd2; cmd3) || (cmd4; cmd5; cmd6)
cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install