执行在一行中组合多个 Linux 命令 [英] Execute combine multiple Linux commands in one line

查看:36
本文介绍了执行在一行中组合多个 Linux 命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to merge multiple linux commands in one line to perform deployment operation. 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

行尾的反斜杠是为了防止shell认为下一行是新命令;如果省略反斜杠,则需要将整个命令写在一行中.

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 退出脚本.您可以使用带有 -e 参数的 set 内置函数来执行此操作.有了它,您可以以更自然的方式编写脚本:

A more convenient way compared to using backslashes and && everywhere is to instruct the shell to exit the script if any of the commands fail. You do that using the set built-in function with the -e argument. With that, you can write a script in a much more natural way:

#! /bin/sh
set -e

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

将其保存到一个文件中,例如 myscript,并使其可执行:

Save that to a file, for example myscript, and make it executable:

chmod +x myscript

您现在可以像执行机器上的其他程序一样执行该脚本.但是如果你不把它放在你的 PATH 环境变量中列出的目录中(例如 /usr/local/bin,或者在一些 Linux 发行版上~/bin),那么您需要指定该脚本的路径.如果它在当前目录中,则使用以下命令执行它:

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:

./myscript

脚本中的命令与第一个示例中的命令的工作方式相同;仅当前一个命令成功时,才会执行下一个命令.对于无条件执行所有命令,只需不要调用 set -e:

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 don't call set -e:

#! /bin/sh

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

这篇关于执行在一行中组合多个 Linux 命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆