简体   繁体   English

执行在一行中组合多个 Linux 命令

[英]Execute combine multiple Linux commands in one line

I am trying to merge multiple linux commands in one line to perform deployment operation.我试图在一行中合并多个 linux 命令来执行部署操作。 For example例如

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

If you want to execute each command only if the previous one succeeded, then combine them using the && operator:如果您只想在前一个命令成功时执行每个命令,请使用&&运算符组合它们:

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

If one of the commands fails, then all other commands following it won't be executed.如果其中一个命令失败,则不会执行其后的所有其他命令。

If you want to execute all commands regardless of whether the previous ones failed or not, separate them with semicolons:如果要执行所有命令而不管前面的命令是否失败,请用分号分隔它们:

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

In your case, I think you want the first case where execution of the next command depends on the success of the previous one.在您的情况下,我认为您想要第一种情况,即下一个命令的执行取决于上一个命令的成功。

You can also put all commands in a script and execute that instead:您还可以将所有命令放在一个脚本中并执行它:

#! /bin/sh
cd /my_folder \
&& rm *.jar \
&& svn co path to repo \
&& mvn compile package install

(The backslashes at the end of the line are there to prevent the shell from thinking that the next line is a new command; if you omit the backslashes, you would need to write the whole command in a single line.) (行尾的反斜杠是为了防止 shell 认为下一行是新命令;如果省略反斜杠,则需要将整个命令写在一行中。)

Save that to a file, for example myscript , and make it executable:将其保存到一个文件中,例如myscript ,并使其可执行:

chmod +x myscript

You can now execute that script like other programs on the machine.您现在可以像机器上的其他程序一样执行该脚本。 But if you don't place it inside a directory listed in your PATH environment variable (for example /usr/local/bin , or on some Linux distributions ~/bin ), then you will need to specify the path to that script.但是,如果您没有将它放在PATH环境变量中列出的目录中(例如/usr/local/bin ,或在某些 Linux 发行版上~/bin ),那么您将需要指定该脚本的路径。 If it's in the current directory, you execute it with:如果它在当前目录中,则使用以下命令执行它:

./myscript

The commands in the script work the same way as the commands in the first example;脚本中的命令与第一个示例中的命令的工作方式相同; the next command only executes if the previous one succeeded.仅当前一个命令成功时,才会执行下一个命令。 For unconditional execution of all commands, simply list each command on its own line:对于无条件执行所有命令,只需在其自己的行中列出每个命令:

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

I've found that using ;我发现使用 ; to separate commands only works in the foreground.分隔命令仅适用于前台。 eg :例如:

cmd1; cmd2; cmd3 & cmd1; cmd2; cmd3 & - will only execute cmd3 in the background, whereas cmd1 && cmd2 && cmd3 & - will execute the entire chain in the background IF there are no errors. cmd1; cmd2; cmd3 & - 只会在后台执行cmd3 ,而cmd1 && cmd2 && cmd3 & - 将在后台执行整个链,如果没有错误。

To cater for unconditional execution, using parenthesis solves this :为了满足无条件执行,使用括号解决了这个问题:

(cmd1; cmd2; cmd3) & - will execute the chain of commands in the background, even if any step fails. (cmd1; cmd2; cmd3) & - 将在后台执行命令链,即使任何步骤失败。

You can separate your commands using a semi colon:您可以使用分号分隔命令:

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

Was that what you mean?那是你的意思吗?

To run them all at once, you can use the pipe line key "|"要一次运行它们,您可以使用管道键“|” like so:像这样:

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

If you want to execute all the commands, whether the previous one executes or not, you can use semicolon (;) to separate the commands.如果要执行所有命令,无论前一个是否执行,都可以使用分号(;) 分隔命令。

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

If you want to execute the next command only if the previous command succeeds, then you can use && to separate the commands.如果你想只有在上一条命令成功时才执行下一条命令,那么你可以使用&&来分隔命令。

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

In your case, the execution of consecutive commands seems to depend upon the previous commands, so use the second example ie use && to join the commands.在您的情况下,连续命令的执行似乎取决于前面的命令,因此请使用第二个示例,即使用 && 连接命令。

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

What is the utility of an only one Ampersand?只有一个与号有什么用? This morning, I made a launcher in the XFCE panel (in Manjaro+XFCE) to launch 2 passwords managers simultaneously:今天早上,我在 XFCE 面板(在 Manjaro+XFCE)中做了一个启动器来同时启动 2 个密码管理器:

sh -c "keepassx && password-gorilla"
or
sh -c "keepassx; password-gorilla"

But it does not work as I want.但它不像我想要的那样工作。 IE, the first app starts but the second starts only when the previous is closed IE,第一个应用程序启动,但第二个应用程序仅在前一个应用程序关闭时启动

However, I found that (with only one ampersand):但是,我发现(只有一个&符号):

sh -c "keepassx & password-gorilla"

and it works as I want now...它现在可以像我想要的那样工作......

  1. Use ;使用;
  • No matter the first command cmd1 run successfully or not, always run the second command cmd2:无论第一个命令cmd1运行成功与否,始终运行第二个命令cmd2:
    • $ cd myfolder; ls # no matter cd to myfolder successfully, run ls
  1. Use &&使用&&
  • Only when the first command cmd1 run successfully, run the second command cmd2:只有当第一条命令cmd1运行成功后,才运行第二条命令cmd2:

    • $ cd myfolder && ls # run ls only after cd to myfolder
  1. Use ||使用||
  • Only when the first command cmd1 failed to run, run the second command cmd2:只有当第一条命令cmd1运行失败时,才运行第二条命令cmd2:

    • $ cd myfolder || ls # if failed cd to myfolder, `ls` will run

You can use as the following code;您可以使用以下代码;

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

It works...有用...

I find lots of answer for this kind of question misleading我发现这类问题的答案很多误导

Modified from this post: https://www.webmasterworld.com/linux/3613813.htm修改自这篇文章: https : //www.webmasterworld.com/linux/3613813.htm

The following code will create bash window and works exactly as a bash window.以下代码将创建 bash 窗口并与 bash 窗口完全相同。 Hope this helps.希望这可以帮助。 Too many wrong/not-working answers out there...那里有太多错误/无效的答案......

            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();
            }

FYI, if you need to run multiple commands in one-line in if-else, you can group the commands with parenthesis.仅供参考,如果您需要在 if-else 中的一行中运行多个命令,您可以用括号将这些命令分组。 See this page for more details.有关更多详细信息,请参阅此页面

You can replace the semicolon ( ; ) with && or ||您可以将分号 ( ; ) 替换为&&|| depending on what you need.取决于你需要什么。 Please note that parenthesis creates a sub-shell to execute the commands.请注意,括号会创建一个子 shell 来执行命令。

Following command executes the cmd1, cmd2, and cmd3 commands if a file called file_name exists;如果存在名为file_name的文件,则以下命令执行 cmd1、cmd2 和 cmd3 命令; otherwise, cmd4, cmd5, and cmd6.否则,cmd4、cmd5 和 cmd6。

[ -f file_name ] && (cmd1; cmd2; cmd3) || (cmd4; cmd5; cmd6)

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

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