如何在我的可可应用程序中运行此命令?
我用 NSTask 找到了一些东西来执行命令,但我无法让它工作。
这是我的命令:
ioreg -c BNBMouseDevice | grep BatteryPercent
如何在我的可可应用程序中运行它? (当然以编程方式)
非常感谢您的帮助。
我已经尝试过这个:
NSTask *server = [NSTask new];
[server setLaunchPath:@"/bin/sh"];
[server setArguments:[NSArray arrayWithObject:@"ioreg -c BNBMouseDevice | grep BatteryPercent"]];
NSPipe *outputPipe = [NSPipe pipe];
[server setStandardInput:[NSPipe pipe]];
[server setStandardOutput:outputPipe];
[server launch];
[server waitUntilExit];
[server release];
NSData *outputData = [[outputPipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
但它说没有这样的文件或目录,但如果我在终端中执行此操作:
ioreg -c BNBMouseDevice | grep BatteryPercent
它可以工作!
怎么了?
I found something with NSTask to execute a command but I can't get it work.
This is my command:
ioreg -c BNBMouseDevice | grep BatteryPercent
How can I run THIS in my cocoa app? (programmatically of course)
Thank you very much for your help.
I have tried this:
NSTask *server = [NSTask new];
[server setLaunchPath:@"/bin/sh"];
[server setArguments:[NSArray arrayWithObject:@"ioreg -c BNBMouseDevice | grep BatteryPercent"]];
NSPipe *outputPipe = [NSPipe pipe];
[server setStandardInput:[NSPipe pipe]];
[server setStandardOutput:outputPipe];
[server launch];
[server waitUntilExit];
[server release];
NSData *outputData = [[outputPipe fileHandleForReading] readDataToEndOfFile];
NSString *outputString = [[[NSString alloc] initWithData:outputData encoding:NSUTF8StringEncoding] autorelease];
but it says no such file or directory but if i do this in terminal:
ioreg -c BNBMouseDevice | grep BatteryPercent
it works!
what's wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
NSTask 启动一个命令,该命令将一组参数作为字符串数组。
NSTask
不会执行任何 shell 解析魔法,您也不能将字符串作为参数传递给sh
并让它以这种方式解析它。可能有一种方法可以做到这一点,但还有更好的解决方案。只需通过 NSTask 运行 ioreg 命令,通过标准方式获取输出,然后对返回的字符串进行简单枚举以查找所需的行。虽然多了几行代码,但与尝试通过 shell 进行泵送相比,它的脆弱性要小得多(如果您提供某种参数,安全漏洞也更少)。而且这很容易做到。
请参阅 此文档了解更多信息。
NSTask
launches a single command that takes a set of arguments as an array of strings.NSTask
does not do any of the shell parsing magic, nor can you pass the string tosh
as an argument and have it parse it in that fashion. There is probably a way to do so, but there is a better solution.Simply run the
ioreg
command viaNSTask
, grab the output via the standard means, then do a simple enumeration of the returned string to find the line(s) you want. While a few more lines of code, it'll be considerably less fragile (and less of a security hole if you ever supply arguments of some kind) than trying to pump it through the shell. And it is easy to do.See this document for more information.
当调用
/bin/sh
时,如果你想让shell运行命令,你需要传递-c
。它应该相当于:替换来做到这一点
您可以通过
请注意,我做了两个更改:参数是一个包含两个元素的数组,即“-c”和“/usr/sbin/ioreg -c BNBMouseDevice | /usr /bin/grep BatteryPercent”,我还用
/usr/sbin/ioreg
替换了ioreg
(并将 grep 替换为/usr/bin/grep
code>) 以明确表明您对这些特定程序感兴趣。When invoking
/bin/sh
, you need to pass-c
if you want the shell to run a command. It should be equivalent to:You can do this by replacing
with
Notice that I’ve made two changes: the arguments are an array with two elements, namely "-c" and "/usr/sbin/ioreg -c BNBMouseDevice | /usr/bin/grep BatteryPercent", and I’ve also replaced
ioreg
with/usr/sbin/ioreg
(and grep with/usr/bin/grep
) to make it explicit that you’re interested in those particular programs.