Bash-查找执行程序返回值 [英] Bash - find exec return value

查看:54
本文介绍了Bash-查找执行程序返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一种方法来判断grep是否确实找到了某些东西,并且最好将返回值传递给if语句.

I need a way to tell if grep does find something, and ideally pass that return value to an if statement.

比方说,我有一个 tmp 文件夹(当前文件夹),其中有几个文件和子文件夹.我想在所有名为 abc 的文件中搜索模式 xyz .如果发现 xyz 的任何出现,则假定搜索成功(与找到 xyz 的次数无关).如果未找到 xyz ,则搜索失败.

Let's say I have a tmp folder (current folder), in that there are several files and sub-folders. I want to search all files named abc for a pattern xyz. The search is assumed to be successful if it finds any occurrence of xyz (it does not matter how many times xyz is found). The search fails if no occurrence of xyz is found.

在bash中,可以这样完成:

In bash, it can be done like this:

find . -name "abc" -exec grep "xyz" {} \;

这将显示是否找到了 xyz .但是我不确定如何将结果(成功与否)传递回 if 语句.

That would show if xyz is found at all. But I'm not sure how pass the result (successful or not) back to an if statement.

任何帮助将不胜感激.

推荐答案

您可以尝试

x=`find . -name abc | xargs grep xyz`
echo $x

也就是说,x包含您的返回值.没有匹配项时为空白.

That is, x contains your return value. It is blank when there is no match.

这篇关于Bash-查找执行程序返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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