如何在objective-c中调用linux命令

2024-12-05 09:49:41
推荐回答(2个)
回答(1):

void runls()
{
[[NSTask launchedTaskWithLaunchPath:@"/bin/ls"
arguments:[NSArray array]] waitUntilExit];
}

如果你希望调用系统命令,先执行shell:

void runSystemCommand(NSString *cmd)
{
[[NSTask launchedTaskWithLaunchPath:@"/bin/sh"
arguments:[NSArray arrayWithObjects:@"-c", cmd, nil]]
waitUntilExit];

回答(2):

支持版本:苹果公司的GCC version 4.0.1

void runls()
{
[[NSTask launchedTaskWithLaunchPath:@"/bin/ls"
arguments:[NSArray array]] waitUntilExit];
}

如果希望调用系统命令,先执行shell:

void runSystemCommand(NSString *cmd)
{
[[NSTask launchedTaskWithLaunchPath:@"/bin/sh"
arguments:[NSArray arrayWithObjects:@"-c", cmd, nil]]
waitUntilExit];