Easiest way to run shell script objective-c
I am a objective-c newbe and am wondering how to run a shell script in objective-c easiest way possible. I don't care about any of the output. I have tried system(), exec() and execl() and an NSTask. Those methods don't work for some reason... This is the shell script I am trying to run:
"mount_webdav http://idisk.mac.com/idisk_username/ /Volumes/idisk_username"
(Basically m开发者_高级运维ounting my iDisk). Also, no password-boxes or any indication of it working shows. It will work in applescript, and I have created the mount-point directory. None of the methods above do anything at all, or crash the application for some weird reason.
NSTask supplies everything you need. This should work:
NSArray *args=[NSArray arrayWithObjects:@"http://idisk.mac.com/idisk_username/",@"/Volumes/idisk_username",nil];
NSTask *mountTask=[[NSTask alloc] init];
[mountTask setLaunchPath:@"/sbin/mount_webdav"];
[mountTask launch];
Test all the complete command path and args from the command line first. I haven't used mount_webdav but I think it requires more args than you supplied.
Don't use shell scripts when you can help it. They're a good way to introduce serious bugs when you parameterize the script (e.g., mount_webdav %@ %@
).
Instead, use either NSTask, fork
/exec
directly, or (in Python) the subprocess module. You'll pass the three arguments as an array, rather than as a single string.
NSTask
would be the way to do this in Cocoa. You might want to go over your code again, read up on the documentation for it (also look at the sample code there), and post specific questions about its usage here if you run into problems.
精彩评论