Return to Snippet

Revision: 48795
at July 9, 2011 04:53 by zingo

Initial Code
// 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];

Initial URL

Initial Description
Cocoa provides NSTask to 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 system and redirect its result to a file.

Initial Title
Quickest Way to Shell - Use system() instead of NSTask

Initial Tags

Initial Language
Objective C