2012-10-09 23 views
1

如何在Objective-C中使用dscl并获取其输出?我想通过,就好像它是在终端的命令是:在Objective-C中启动dscutil

dscl . -readall /Users UniqueID | awk '/^RecordName:/ {name=$2}; /^UniqueID:/{if ($2 > 500) print name}' 

我知道如何启动system_profiler带参数如-xml等,但我无法弄清楚如何通过这么长的字符串在哪里是实际工作。

我知道system_profiler是/ usr/sbin/system_profiler,但dscl呢?

回答

1

最好的办法是启动它,就好像它是一个shell脚本。将它作为文件中的shell脚本粘贴到项目中,或者使用NSTask来组成调用/ bin/sh的命令行,将该命令作为字符串传递,以允许sh解析它。

I.e. /bin/sh -c ".... your command string ...."

+0

尝试:outFileData = [taskLauncher launchTask:@ “USR/sh” 的withArguments:[NSArray的arrayWithObjects:@ “ - C”,@“DSCL -readall /用户UNIQUEID | AWK“/^RecordName:/ {名称= $ 2};/^ UniqueID:/ {if($ 2> 500)print name}'“,nil]]; 没有工作 – John

+0

'usr/sh'?那是不对的。 – bbum

+0

试过:outFileData = [taskLauncher launchTask:@“bin/sh”withArguments:[NSArray arrayWithObjects:@“ - c”,@“dscl。-readall/Users UniqueID | awk'/^RecordName:/ {name = $ 2};/^ UniqueID:/ {if($ 2> 500)print name}'“,nil]];并没有奏效。也试过/ user/bin/sh – John