简体   繁体   English

NSTask通过/ bin / sh或/ bin / bash问题发送自变量Cocoa Objective-C

[英]NSTask sending an argument through /bin/sh or /bin/bash problem Cocoa Objective-C

Basically I've passed a script in the terminal through /bin/bash "[path]" or /bin/sh "[path]", and I've passed commands in a terminal through /bin/bash -c "[command]" or /bin/sh -c "[command]". 基本上,我已经通过/ bin / bash“ [path]”或/ bin / sh“ [path]”在终端中传递了脚本,并且我已经通过/ bin / bash -c“ [command ]”或/ bin / sh -c“ [命令]”。 Both ways work properly. 两种方式均能正常工作。 But in my Cocoa App, when I try to do the exact same thing with NSTask (using /bin/bash or /bin/sh), the app seems to never respond. 但是在我的Cocoa应用程序中,当我尝试使用NSTask做完全相同的事情(使用/ bin / bash或/ bin / sh)时,该应用程序似乎永远不会响应。 It is as if the program is stuck inside of bash or sh. 就像程序被卡在bash或sh内一样。 I've tried to do this with and without waitUntilExit, and I've also tried to use terminate. 我尝试使用和不使用waitUntilExit来执行此操作,并且还尝试使用终止。 Still no luck. 仍然没有运气。 Has anyone else had this issue or have a clue as to why this is happening? 是否还有其他人遇到过这个问题或是否知道为什么会发生这种情况?

Thanks! 谢谢!

PS I'm not on my work computer right now, but, if needed, I can provide code later. PS:我现在不在我的工作计算机上,但是,如果需要,我可以稍后提供代码。

Ok. 好。 So I found the solution for anyone else that might need it. 因此,我为其他可能需要的人找到了解决方案。 Basically, there is a problem is the standardInput. 基本上,有一个问题是standardInput。 The problem only shows up in the Xcode console and is not an error/bug with your app. 该问题仅在Xcode控制台中显示,而不是您的应用程序中的错误/错误。 The fix is to add the following line (basically set the standardInput to something random): 解决方法是添加以下行(基本上将standardInput设置为random):

[task setStandardInput: [NSPipe pipe]];

In the solution above, task is the variable name of the NSTask being used. 在上述解决方案中,task是所使用的NSTask的变量名。

Also see: 另请参阅:

http://www.cocoadev.com/index.pl?HowToPipeCommandsWithNSTask http://www.cocoadev.com/index.pl?HowToPipeCommandsWithNSTask

[proc setLaunchPath:@"/bin/bash"];
[proc setArguments:[NSArray arrayWithObjects: @"-c", @"/usr/sbin/netstat -rn | /usr/bin/grep default", nil]];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM