2

I have tried really hard but could not figure out a way to print an error if

find -name \"filename"

does not find any file. The code I am using so far is as follows:

    char *argv[];
       argv[0]="find";
       argv[1]="-name";
       argv[2]=strcat(str,"\abc.txt"); 
       argv[3]=NULL;
       pid_t pid;
       pid= fork();

   if(pid==0)
   {
    execvp(argv[0],argv);
   printf("file does not exist");
    }

But I can't print my print statement because find -name \"filename" never returns an error.

4

2 回答 2

1

您可以使用shell条件,例如:

[ "$(find /opt -name '*.txt')" ] && echo Found || echo Not found

这基本上与以下内容相同:

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

如果您使用的是 GNU find,请尝试使用-quit.

于 2015-09-22T22:10:38.857 回答
-2

尝试使用:

find -f PATTERN

使用此构造后,您将获得结果代码:

echo $?

如果 PATTERN 不存在,则此结果代码必须为 1(或在某些情况下不等于 0)

于 2013-04-17T09:48:17.693 回答