壳牌 - 在一行中的多个命令 [英] Shell - Multiple commands in one line

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

问题描述

说我有一个文件 /模板/苹果,我想


  1. 把它在两个不同的地方,然后

  2. 删除原始的。

因此​​, /模板/苹果将被复制到 /模板/使用 /模板/ INUSE
再之后,我想删除原来的。

So, /templates/apple will be copied to /templates/used AND /templates/inuse and then after that I’d like to remove the original.

CP 来做到这一点的最好方式,其次是 RM ?还是有更好的办法?

Is cp the best way to do this, followed by rm? Or is there a better way?

我想要做的这一切在同一行,所以我想它看起来是这样的:

I want to do it all in one line so I’m thinking it would look something like:

cp /templates/apple /templates/used | cp /templates/apple /templates/inuse | rm /templates/apple

这是正确的语法?

推荐答案

您正在使用 | (管)直接命令的输出到另一个命令。你所寻找的是&放大器;&安培; 运营商执行只有previous 1得手下一个命令:

You are using | (pipe) to direct the output of a command into another command. What you are looking for is && operator to execute the next command only if the previous one succeeded:

cp /templates/apple /templates/used && cp /templates/apple /templates/inuse && rm /templates/apple

或者

cp /templates/apple /templates/used && mv /templates/apple /templates/inuse


要总结(非详尽)的bash的命令运营商/分离器:


To summarize (non-exhaustively) bash's command operators/separators:


  • | 管道(管道)标准输出(标准输出)一个命令到另一个的标准输入。需要注意的是标准错误仍进入其默认的目标,无论是碰巧。

  • |&安培; 管道既标准输出标准错误一个命令到另一个的标准输入。非常有用的,可在bash 4及更高版本。

  • &放大器;&安培; 执行的右手命令&放大器;&安培; 只有previous 1成功。

  • || 执行 || 的右手命令只它previous一次失败。

  • ;总是不分是否previous命令成功或失败; 执行的右手命令。除非设置-e 是pviously调用$ P $,这将导致庆典失败上的错误。

  • | pipes (pipelines) the standard output (stdout) of one command into the standard input of another one. Note that stderr still goes into its default destination, whatever that happen to be.
  • |&pipes both stdout and stderr of one command into the standard input of another one. Very useful, available in bash version 4 and above.
  • && executes the right-hand command of && only if the previous one succeeded.
  • || executes the right-hand command of || only it the previous one failed.
  • ; executes the right-hand command of ; always regardless whether the previous command succeeded or failed. Unless set -e was previously invoked, which causes bash to fail on an error.

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

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