忽略xargs的空结果 [英] Ignore empty result for xargs

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

问题描述

考虑一下这个命令:

ls /mydir/*.txt | xargs chown root

其目的是要改变所有文本文件的所有者 MYDIR 来根

问题是,如果在 MYDIR 没有 .TXT 文件,然后xargs的thows一个错误说有没有路径指定。这是一种无害的例子,因为被抛出的错误,但是在某些情况下,如在我需要在这里使用的脚本中,假设一个空​​白路径为当前目录。所以,如果我从运行该命令的/ home /嗵/ 然后如果有结果 LS /mydir/*.txt,并在 /家庭/嗵/ 有自己的所有者更改为根。

The issue is that if there are no .txt files in mydir then xargs thows an error saying there is no path specified. This is a harmless example because an error is being thrown, but in some cases, like in the script that i need to use here, a blank path is assumed to be the current directory. So if I run that command from /home/tom/ then if there is no result for ls /mydir/*.txt and all files under /home/tom/ have their owners changed to root.

所以,我怎么能有xargs的忽视了一个空的结果?

So how can I have xargs ignore an empty result?

推荐答案

有关GNU 的xargs ,您可以使用 -r - 无运行,如果空选项:

For GNU xargs, you can use the -r or --no-run-if-empty option:

- 无运行,如果空结果
          -r 结果如果标准输入不包含任何非空白,不要运行该命令。正常情况下,该命令运行一次,即使没有输入。这个选项是GNU扩展。

--no-run-if-empty
-r
If the standard input does not contain any nonblanks, do not run the command. Normally, the command is run once even if there is no input. This option is a GNU extension.

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

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