执行在一行中组合多个 Linux 命令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13077241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Execute combine multiple Linux commands in one line
提问by d-man
I am trying to merge multiple linux commands in one line to perform deployment operation. For example
我试图在一行中合并多个 linux 命令来执行部署操作。例如
cd /my_folder
rm *.jar
svn co path to repo
mvn compile package install
采纳答案by Nikos C.
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. If it's in the current directory, you execute it with:
您现在可以像机器上的其他程序一样执行该脚本。但是,如果您没有将它放在PATH
环境变量中列出的目录中(例如/usr/local/bin
,或在某些 Linux 发行版上~/bin
),那么您将需要指定该脚本的路径。如果它在当前目录中,则使用以下命令执行它:
./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
回答by Mark Stevens
cd /my_folder && rm *.jar && svn co path to repo && mvn compile package install
回答by andrux
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?
那是你的意思吗?
回答by Prabhu
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 i.e. use && to join the commands.
在您的情况下,连续命令的执行似乎取决于前面的命令,因此请使用第二个示例,即使用 && 连接命令。
回答by Dean
I've found that using ; to separate commands only works in the foreground. eg :
我发现使用 ; 分隔命令仅适用于前台。例如:
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) &
- 将在后台执行命令链,即使任何步骤失败。
回答by Nettlebay AP
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. I.E., 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...
它现在可以像我想要的那样工作......
回答by Deniz Güzel
You can use as the following code;
您可以使用以下代码;
cd /my_folder && \
rm *.jar && \
svn co path to repo && \
mvn compile package install
It works...
有用...
回答by Kelly Poore
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
回答by wwjih123
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. Hope this helps. Too many wrong/not-working answers out there...
以下代码将创建 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();
}