当找不到文件时,从0findcommand + exe status diff

如何启用查找命令打印EXE状态差异从0当查找命令没有find文件?

根据我的例子,当我试图findtest1.txt文件,$? 设置为0,但我期望从0获得值差异,因为没有find文件

[root@om-1 tmp]# touch test.txt [root@om-1 tmp]# find /var/tmp -name test.txt /var/tmp/test.txt [root@om-1 tmp]# echo $? 0 [root@om-1 tmp]# find /var/tmp -name test1.txt (test1.txt not under /var/tmp) [root@om-1 tmp]# echo $? 0 

你不能。

find手册页:

  EXIT STATUS find exits with status 0 if all files are processed successfully, greater than 0 if errors occur. This is deliberately a very broad description, but if the return value is non-zero, you should not rely on the correctness of the results of find. 

退出状态0只是说:我设法处理所有文件没有错误(例如权限问题)。

一个解决scheme

 COUNT=`find / -type f -name "thisfiledoesnnotexist" -print | wc -l` ECHO $COUNT 0 

而在数据库中找不到该文件时,定位返回1

尝试以下shell条件:

 [ "$(find /var/tmp -name test.txt)" ] && echo Found || echo Not found 

这基本上是一样的:

 [ "$(find /var/tmp -name test.txt)" '!=' '' ] && echo Found || echo Not found 

在两种情况下find的退出状态都应为零,因为find命令没有失败。 如果没有find任何匹配,它只是正常存在,不打印任何输出。

作为一个解决方法,你可以使用像这样的东西:

 if [ `find . -name test1.txt | grep test1.txt | wc -l` -eq 0 ] ; then echo 1 ; else echo 0; fi 

但是,我不确定错误的结果。

是使用强制查找你吗? 你真的知道一些文件应该在哪里吗? 然后例如

 stat /var/tmp/test.txt; echo $? stat /var/tmp/test1.txt; echo $? 

应该为你工作。