How can I execute a terminal command (like grep) from my Objective-C Cocoa application?
If the Terminal command requires Administrator Privilege (aka sudo), use AuthorizationExecuteWithPrivileges instead.
The following will create a file named "com.stackoverflow.test" is the root directory "/System/Library/Caches".
AuthorizationRef authorizationRef;
FILE *pipe = NULL;
OSStatus err = AuthorizationCreate(nil,
kAuthorizationEmptyEnvironment,
kAuthorizationFlagDefaults,
&authorizationRef);
char *command= "/usr/bin/touch";
char *args[] = {"/System/Library/Caches/com.stackoverflow.test", nil};
err = AuthorizationExecuteWithPrivileges(authorizationRef,
command,
kAuthorizationFlagDefaults,
args,
&pipe);