如何在我的可可应用程序中运行此命令?

发布于 2024-10-10 17:41:33 字数 892 浏览 6 评论 0原文

我用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

野味少女 2024-10-17 17:41:33

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 to sh 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 via NSTask, 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.

反目相谮 2024-10-17 17:41:33

当调用/bin/sh时,如果你想让shell运行命令,你需要传递-c。它应该相当于:

/bin/sh -c "/usr/sbin/ioreg -c BNBMouseDevice | /usr/bin/grep BatteryPercent"

替换来做到这一点

[server setArguments:[NSArray arrayWithObject:@"ioreg -c BNBMouseDevice | grep BatteryPercent"]];

您可以通过

[server setArguments:[NSArray arrayWithObjects:@"-c", @"/usr/sbin/ioreg -c BNBMouseDevice | /usr/bin/grep BatteryPercent", nil]];

请注意,我做了两个更改:参数是一个包含两个元素的数组,即“-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:

/bin/sh -c "/usr/sbin/ioreg -c BNBMouseDevice | /usr/bin/grep BatteryPercent"

You can do this by replacing

[server setArguments:[NSArray arrayWithObject:@"ioreg -c BNBMouseDevice | grep BatteryPercent"]];

with

[server setArguments:[NSArray arrayWithObjects:@"-c", @"/usr/sbin/ioreg -c BNBMouseDevice | /usr/bin/grep BatteryPercent", nil]];

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文