I would like to run a shell script, from a file or from an objective-c string (within the code). I would also like the shell script's result to be stored to a variable. I would not like the shell script to be split into arguments (such as setLaunchPath when I run it). For example: running this shell script "mount_webdav idisk.mac.com/mac_username /Volumes/mac_username" instead of "/bin/mount_webdav" then the arguments. Is there anyway to do this? I am using NSTask right now, but it has caused me some errors when I try to put the arguments with it. Here is the posed code:
(some of the .m file)
NSString *doshellscript(NSString *cmd_launch_path, NSString *first_cmd_pt) {
NSTask *task = [[NSTask alloc] init]; // Make a new task
[task setLaunchPath: cmd_launch_path]; // Tell which command we are running
[task setArguments: [NSArray arrayWithObjects: first_cmd_pt, nil]];
[task setArguments: first_cmd_pt];
NSPipe *pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
[task launch];
NSData *data = [[pipe fileHandleForReading] readDataToEndOfFile];
NSString *string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
[task release]; //Release the task into the world, thus destroying it.
return string;
}
NSString *mount_idisk(NSString *mac_username) {
doshellscript(@"/bin/mkdir", [@"/Volumes/" stringByAppendingString:mac_username]);
NSString *path_tmp = [mac_username stringByAppendingString: @"/ /Volumes/"];
NSString *idisk_path = [path_tmp stringByAppendingString:mac_username];
//NSLog(@"%@", [@" http://idisk.mac.com/" stringByAppendingString: idisk_path]);
NSString *finished_path = [@"http://idisk.mac.com/" stringByAppendingString: idisk_path];
doshellscript(@"/sbin/mount_webdav", finished_path);
}
...
Here is the line I am using to run it:
mount_idisk("username");