Domanda

I am trying to get the latest modification date in a directory tree. This works fine in terminal:

find . ! -path "*.git*" -exec stat -f "%m" \{} \; | sort -n -r | head -1

But when I try this with NSTask I get

find: -exec: no terminating ";" or "+"

In the args array for NSTask which I am using, I escaped the backslashes:

NSArray *args = @[@".", 
                  @"!", 
                  @"-path", 
                  @"*.git*", 
                  @"-exec", 
                  @"stat", 
                  @"-f", 
                  @"%m", 
                  @"\\{}", 
                  @"\\;", 
                  @"|", 
                  @"sort", 
                  @"-n", 
                  @"-r", 
                  @"|", 
                  @"head", 
                  @"-1"];

So, what am I missing here? I also tried to remove the backslahes all together, but that gave me a "find: |: unknown primary or operator" error.

È stato utile?

Soluzione

Thanks for the hints guys. I've split it up and get more response now. When running the command without backslashes & without the sort & head I do get results.

NSArray *args = @[@".", 
              @"!", 
              @"-path", 
              @"*.git*", 
              @"-exec", 
              @"stat", 
              @"-f", 
              @"%m", 
              @"{}", 
              @";"];

So it looks like NSTask should not be fed '|' characters. I'll just so the sorting etc. in objc.

Altri suggerimenti

You actually have three command line programs that you call with

find . ! -path "*.git*" -exec stat -f "%m" \{} \; | sort -n -r | head -1

find, sort, and head.

You should make an nstask for each of theses commands--link the stdout of find to the stdin of sort and then do the same with sort and head.

According to the docs, you should be able the set an NSPipe as the out of one and the in of another, if this doesn't work you could just pull the results back and pass them to next one manually.

Though I have not verified it, I don't think you need the backslash escapes because this does not go through your terminal to launch these programs.

Figured out another way of doing this.

I now run an NSTask command /bin/sh with the arguments:

NSArray *args = @[@"-c", 
                  @"/usr/bin/find . ! -path \"*.git*\" -exec stat -f %%m \\{} \\; | sort -n -r | head -1"];

Credits to http://borkware.com/quickies/single?id=130

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top