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

查看:307
本文介绍了在一行中执行结合多个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.)

将其保存到文件,例如 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

脚本中的命令的工作方式与第一个示例中的命令相同;下一个命令仅在前一个成功时执行。对于所有命令的无条件执行,只需在其自己的行上列出每个命令:

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

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

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