shell test和find命令实例解析

shell test和find命令实例解析

 

下面以uildcoreproduct.mk相关部分来学习

define _find-android-products-files
$(shell test -ddevice && find device -maxdepth 6 -name AndroidProducts.mk) 
 $(shell test -d vendor && find vendor -maxdepth 6 -nameAndroidProducts.mk) 
 $(SRC_TARGET_DIR)/product/AndroidProducts.mk
Endef

Shell中的 test 命令用于检查某个条件是否成立,它可以进行数值、字符和文件三个方面的测试,这方面参考http://www.runoob.com/linux/linux-shell-test.html

 

上面例子同时也用到了find命令

test -d device&& find device -maxdepth 6 -name AndroidProducts.mk

test -d device表示如果device存在且为目录则为真,则会执行find命令find命令在device目录下,最大深入6层子目录来查找名字为AndroidProducts.mk的文件

 

对于find命令,在-之前的字符串表示path(目录,这里是device),之后是expression(表达式,这里是-maxdepth 6 -name AndroidProducts.mk),

 

-maxdepth 6  设定递归搜索的目录层级,1为在当前目录下,即不递归搜索。

-name name, -iname name : 文件名称符合 name 的文件。iname 会忽略大小写

 

find命令找到AndroidProducts.mk后将查找到的子目录和文件全部进行显示,这就是find命令的输出,比如我在device下find的结果



图1

 

 

这里shell是函数,它的参数是操作系统Shell的命令,比如test,shell函数把执行命令后的输出作为函数返回,上面命令包的作用是在device、vendor目录下查AndroidProducts.mk,把device、vendor和(SRC_TARGET_DIR)/product/AndroidProducts.mk所在子目录和文件全部进行显示

 

命令包_find-android-products-files的值是AndroidProducts.mk文件列表,见上图

原文地址:https://www.cnblogs.com/liang123/p/6325149.html