执行在一行中组合多个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

#1楼

参考:https://stackoom.com/question/srzF/执行在一行中组合多个Linux命令


#2楼

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

#3楼

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? 那是你的意思吗?


#4楼

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

#5楼

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. 在您的情况下,连续命令的执行似乎取决于以前的命令,因此使用第二个示例,即使用&&来加入命令。


#6楼

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) & - 将在后台执行命令链,即使任何步骤失败也是如此。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值