从可可(obj.c)与响应执行shell脚本 [英] Execute shell script from cocoa (obj.c) with response

查看:143
本文介绍了从可可(obj.c)与响应执行shell脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的:

- (NSString *)unixSinglePathCommandWithReturn:(NSString *)command
{
NSPipe *newPipe = [NSPipe pipe];
NSFileHandle *readHandle = [newPipe fileHandleForReading];

NSTask *unixTask = [[NSTask alloc] init];
[unixTask setStandardOutput:newPipe];
[unixTask setLaunchPath:@"/bin/sh"];
[unixTask setArguments:[NSArray arrayWithObjects:@"-c", command , nil]]; 
[unixTask launch];
[unixTask waitUntilExit];

NSString *output = [[NSString alloc] initWithData:[readHandle readDataToEndOfFile] encoding:NSUTF8StringEncoding];

return output;
}

但预期这是行不通的。我把它当我点击一个按钮。如果我删除符合waitUntilExit它正常工作,但只有一次。当它的存在,这是行不通的。
我也尝试了一些基本的命令如ls'的'平-C1 google.com'之类的东西,但我不能让它以某种方式工作。如果你有一些不同的方法来可可与接收响应运行shell脚本,请立即让我。谢谢大家:)

But it doesn't work as expected. I call it when i click a button. If i remove the line with 'waitUntilExit' it works as expected, but only once. When it's there, it doesn't work. I tried also some basic commands like 'ls' 'ping -c1 google.com' and stuff like that, but i can't get it to work somehow. If you have some different approach to run shell scripts in cocoa with receiving the response, please let me now. Thank you all :)

推荐答案

嘿,Kukosk。有一个关于的NSLog()问题时 CocoaDev 评论运行NSTask。解决方法是在启动任务前设置为标准输入管道

Hey, Kukosk. There’s a comment on CocoaDev about NSLog() issues when running an NSTask. The fix is to set a pipe for stdin before launching the task:

[task setStandardInput:[NSPipe pipe]];

如果你依靠的NSLog()只检查任务是否已经运行,这可能会解决您的问题。或者,你可以尝试present 输出在你的GUI而不是使用的的NSLog()

If you’re relying on NSLog() only to check whether the task has run, this might fix your problem. Alternatively, you could try to present output in your GUI instead of using NSLog().

这篇关于从可可(obj.c)与响应执行shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆