Get result from shell script objective-c

試著忘記壹切 提交于 2019-12-04 19:10:06

There is no way to pass a whole command line to NSTask.

For good reason; doing so is rife with security holes if you have any kind of string composition going on. Your string composition code would have to be fully cognizant of all of the rules of parsing a shell command line and would have to escape every possible combination of characters that might lead to arbitrary command execution.

The system() C API lets you execute arbitrary commands, but has no mechanism for capturing output directly. It would be easy to add something to your command line that spews the output into a temporary file that you later read, but doing so just adds more security holes above and beyond passing down a whole command line as a single string.

Wait... Looks like you have a straightforward bug:

[task setArguments: [NSArray arrayWithObjects: first_cmd_pt, nil]];
[task setArguments: first_cmd_pt];

Why are you setting and then re-setting the task's arguments?

Given that your mount_idisk() function is effectively composing the individual arguments and concatenating them together into a single string, why don't you simply stuff all the args into an NSArray and modify doshellscript() to take an array as the second parameter; the array of arguments?


You aren't creating the array of arguments correctly.

Namely:

NSArray *finished_path = [NSArray arrayWithObjects:@"http://idisk.mac.com/", mac_username, @"/ /Volumes/", mac_username, nil];

That line is creating an array contain 4 objects which are then treated as 4 separate arguments in the doshellscript() function and not the two arguments that you need.

Maybe something like:

NSString *mobileMeUserURL = [@"http://idisk.mac.com/" stringByAppendingString: mac_username];
NSString *localMountPath = [ @"/ /Volumes/" stringByAppendingString:  mac_username];
NSArray *arguments = [NSArray arrayWithObjects: mobileMeUserURL, localMountPath, nil];
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!