忽略在Mac OS X xargs的空结果 [英] Ignore empty results for xargs in Mac OS X

查看:784
本文介绍了忽略在Mac OS X xargs的空结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code,我的网站使用这块code的服务器(Ubuntu的)自动部署。

The code for my website uses this piece of code for automatic deployment on the server (Ubuntu).

CMD =CD+ checkout_dir +'和;&安培; '+ svn_command +ST | awk的'{打印$ 2}'| grep的-v ^部署| TAC | xargs的-r+ svn_command +恢复-R&放大器;&安培;+ svn_command +'了-r'+选项。修订

这是什么命令所做的就是 CD 进入结帐目录,运行 SVN状态,然后提取的文件名( $ 2 ),删除部署目录,并从该名单内的所有文件(我不想要恢复它) 。如果没有说法不运行了svn revert命令,否则它。

What this command does is it cd into checkout directory, runs svn status, and then extracts the filename ($2), removes the deploy directory and all its files from the list (I don't want to revert it). If there are no argument it does not run the svn revert command, else it does.

不幸的是,的xargs -r 没有我的机器上工作(的Mac OS X 10.8)。所以我坚持在这里,任何人都可以帮忙吗?

Unfortunately the xargs -r does not work on my machine (Mac OS X 10.8). So I am stuck here, can anyone help?

推荐答案

事实上,BSD实施的xargs 不具有 -r 标记( - 无运行,如果空)。 GNU版本在Linux中有它。

Indeed, the BSD implementation of xargs doesn't have the -r flag (--no-run-if-empty). The GNU version in Linux has it.

下面是要解决的问题,在Linux和工作方式的一种方法BSD:

Here's one way to work around the issue in a way that works in both Linux and BSD:

... | (grep -v ^deploy || echo :) | xargs svn revert

的grep ... ||回声:中间将生成一个 A线:在它的情况下输出的grep 是空的。这是一个有点的的,因为的xargs 仍然会运行命令 SVN还原:。如果您的存储库不包含文件那么这应该是无效的,所以可以接受的。在:因为有你的库中没有这样的文件可以是任何东西,只要

The grep ... || echo : in the middle will generate a line with a : in it in case the output of grep is empty. It's a bit dirty, because xargs will still run the command svn revert :. If your repository doesn't contain the file : then this should have no effect, so it can be acceptable. The : could be anything else, as long as there is no such file in your repository.

最后, @tripleee 指出的那样,的grep ... ||回声:必须在封闭(.​​..),这是因为:

Finally, as @tripleee pointed out, the grep ... || echo : must be enclosed within (...), because:

|| 具有较高的precedence比 | ,从而终止(第一个)的管道。

the || has higher precedence than |, and thus terminates the (first) pipeline.

您code看起来像一个Python字符串。这将是更具可读性这种方式:

Your code looks like a Python string. It will be more readable this way:

kwargs = {
  'svn': svn_command,
  'dir': checkout_dir,
  'revno': options.revision,
}
cmd = "cd {dir} && {svn} st | awk -v r=1 '$2 ! ~ /deploy/ {{ print $2; r=0 }} END {{ r || print \":\" }}' | xargs {svn} revert && {svn} up -r {revno}".format(**kwargs)

我做了一些改变你原来的:

I made some changes to your original:


  • 感动的的grep 逻辑里面 AWK ,为的 @ tripleee 建议。请注意,因为的grep 黑客已经不再需要,也不再需要在包裹(...)

  • 扔下 TAC ,因为我不明白这一点在它

  • 扔下 -R SVN还原,因为我不认为你需要它

  • Moved the logic of the grep inside awk, as @tripleee suggested. Notice that since the grep hack is not needed anymore, there is also no more need to wrap within (...)
  • Dropped the tac, as I don't see the point in it
  • Dropped the -R from svn revert, because I don't think you need it

这篇关于忽略在Mac OS X xargs的空结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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