2010 02 21Quickest Way to Shell
NSTaskto run subprocesses. It can be used to run shell scripts, but all this manual pipe and file handling is a bit cumbersome. Fortunately, for a simple script, we can use the trusty old
systemand redirect its result to a file.
Here's a quick way to get the path to the last crash report of your app :
// We'll save the result in a temp file NSString* tempFilePath = [NSString stringWithFormat:@"%@/MyApplicationLastCrash.txt", NSTemporaryDirectory()]; // Run the shell script NSString* script = [NSString stringWithFormat: @"ls -1t /Users/mini/Library/Logs/CrashReporter/* | grep /MyApplication | head -1 | tr -d '\n' >%@", tempFilePath]; system([script UTF8String]); // Returns path to last crash report or empty string ( [lastCrash length == 0] ) NSString* lastCrash = [NSString stringWithContentsOfFile:tempFilePath encoding:NSUTF8StringEncoding error:nil];In this case,
lslists all crash reports from new to old,
grepkeeps only yours,
trextract the result by keeping the first line and removing the trailing newline. Getting the path is now only a matter of reading the temp file.