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

执行在一行中组合多个Linux命令相关推荐

  1. linux一行多个命令行,如何在一行中运行多个Linux命令

    对于每个Linux管理员来说,熟练使用各种命令行是他们的特性.但对于普通用户来说,可能还是有难度,您需要继续练习Linux命令,并找到使该任务更有效的方法.实现这个特定目标的一种方法是学习一些技巧,这 ...

  2. linux中date使用方法,linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例)...

    linux命令详解date使用方法(计算母亲节和父亲节日期脚本示例) 发布于 2016-02-07 15:58:40 | 108 次阅读 | 评论: 0 | 来源: 网友投递 LinuxLinux是一 ...

  3. linux在home中新建目录temp,Linux命令之目录操作命令

    目录操作命令 cd cd命令功能 在每一个操作系统中,都需要有改变目录的工作,Linux也不例外,在Linux操作系统中,是通过cd命令来改变工作目录的. cd命令的命令格式及用法 命令格式:cd 命 ...

  4. linux中脚本退出函数,Linux 命令 shell 脚本之09(函数)

    1.使用函数 [oracle@XAG143 myshell]$ cat test_fun1.sh #!/bin/bash # using a function in a script function ...

  5. unity延迟执行下一行代码_Python代码在Linux环境下执行错误异常

    import happybase import time from multiprocessing import Pool# 设置IP地址 hbase_ip = '20.88.0.84' start_ ...

  6. linux buffer cache 过高_工作中经常用的linux命令 free

    free命令可以显示当前系统未使用的和已使用的内存数目,还可以显示被内核使用的内存缓冲区,不带参数默认将会以kb单位展示 CentOS 7.6上显示如下 free -m 以M为单位显示 total:表 ...

  7. [置顶] 总结工作中常用到的linux命令

    常用解压命令 tar.bz2 命令: tar -jxvf  *.tar.bz2 tar.z   命令: tar -zxvf  *.tar.z tar.gz   命令: tar -Zxvf  *.tar ...

  8. 工作中常用到的Linux命令操作

    如何获取java进程的pid: pgrep -l java 如何获取某个进程的网络端口号: netstat/lsof netstat命令用于显示与IP.TCP.UDP和ICMP协议相关的统计数据,一般 ...

  9. 工作中常用到的Linux命令

    https://mp.weixin.qq.com/s/wPBILQSl8-OVNYcJN9cBRg 转载于:https://www.cnblogs.com/linliquan/p/11236598.h ...

最新文章

  1. Python入门基础--雨敲窗视频系列
  2. latex中使用bibtex显示paragraph ended before \mule@arg was complete 的解决方法
  3. POJ 2226 Muddy Fields(最小点覆盖)题解
  4. docker镜像下载到本地,并导入其他服务器
  5. qemu中vCPU对应的线程
  6. 程序员最喜欢的15款文本编辑器推荐
  7. 办公office 2019软件有哪些
  8. siamfc-pytorch代码讲解(三):demotrack
  9. 什么蓝牙耳机好用又不贵?好用不贵的蓝牙耳机推荐
  10. Linux新世纪五笔
  11. 机器学习模型评估及性能评价(超全)
  12. 4.5 函数最佳逼近
  13. B2G商城APP解决方案开发
  14. SpringBoot实现微信小程序登录功能
  15. CPU(Central Processing Unit,中央处理器)
  16. 判断一个数是否为Sky数
  17. 科技云报道原创:没有一个行业,能拒绝“通信中台”的诱惑
  18. Android源码的Binder权限是如何控制,附超全教程文档
  19. 什么叫五口POE交换机 五口POE交换机使用方法
  20. [附源码]计算机毕业设计springboot居家养老服务系统小程序

热门文章

  1. C++语言代码检查工具PC-Lint简介
  2. c++socket模型之我见
  3. 算法---------至少有K个重复字符的最长子串(Java版本)
  4. Android Studio你不知道的调试技巧
  5. android 收起下拉菜单,Android 展开/折叠 系统下拉通知栏
  6. Android之集成微信登录
  7. (0067)iOS开发之iOS新增类UILayoutGuide的用途
  8. webpack打包后自动弹出浏览器查看效果
  9. 火星坐标、百度坐标、WGS-84坐标相互转换及墨卡托投影坐标转经纬度JavaScript版...
  10. python 函数(二)