从Cocoa应用程序执行终端命令

时间:2009-01-05 08:21:32

标签: objective-c cocoa macos

如何从Objective-C Cocoa应用程序执行终端命令(如grep)?

12 个答案:

答案 0 :(得分:282)

您可以使用NSTask。这是一个运行“/usr/bin/grep foo bar.txt”的示例。

int pid = [[NSProcessInfo processInfo] processIdentifier];
NSPipe *pipe = [NSPipe pipe];
NSFileHandle *file = pipe.fileHandleForReading;

NSTask *task = [[NSTask alloc] init];
task.launchPath = @"/usr/bin/grep";
task.arguments = @[@"foo", @"bar.txt"];
task.standardOutput = pipe;

[task launch];

NSData *data = [file readDataToEndOfFile];
[file closeFile];

NSString *grepOutput = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog (@"grep returned:\n%@", grepOutput);

NSPipeNSFileHandle用于重定向任务的标准输出。

有关从Objective-C应用程序中与操作系统交互的更多详细信息,您可以在Apple的开发中心上查看此文档:Interacting with the Operating System

编辑:包含NSLog问题的修复

如果您使用NSTask通过bash运行命令行实用程序,那么您需要包含此魔术线以保持NSLog正常工作:

//The magic line that keeps your log where it belongs
task.standardOutput = pipe;

这里有一个解释:https://web.archive.org/web/20141121094204/https://cocoadev.com/HowToPipeCommandsWithNSTask

答案 1 :(得分:40)

本着共享的精神......这是我经常用来运行shell脚本的方法。 您可以将脚本添加到产品包中(在构建的复制阶段)然后 让脚本在运行时读取并运行。注意:此代码在privateFrameworks子路径中查找脚本。 警告:这可能会对部署的产品造成安全风险,但对于我们的内部开发,它是一种简单的方法来定制简单的东西(比如rsync到哪个主机......)而无需重新编译应用程序,只需编辑捆绑包中的shell脚本。

//------------------------------------------------------
-(void) runScript:(NSString*)scriptName
{
    NSTask *task;
    task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];

    NSArray *arguments;
    NSString* newpath = [NSString stringWithFormat:@"%@/%@",[[NSBundle mainBundle] privateFrameworksPath], scriptName];
    NSLog(@"shell script path: %@",newpath);
    arguments = [NSArray arrayWithObjects:newpath, nil];
    [task setArguments: arguments];

    NSPipe *pipe;
    pipe = [NSPipe pipe];
    [task setStandardOutput: pipe];

    NSFileHandle *file;
    file = [pipe fileHandleForReading];

    [task launch];

    NSData *data;
    data = [file readDataToEndOfFile];

    NSString *string;
    string = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
    NSLog (@"script returned:\n%@", string);    
}
//------------------------------------------------------

编辑:包含NSLog问题的修复

如果您使用NSTask通过bash运行命令行实用程序,那么您需要包含此魔术线以保持NSLog正常工作:

//The magic line that keeps your log where it belongs
[task setStandardInput:[NSPipe pipe]];

在上下文中:

NSPipe *pipe;
pipe = [NSPipe pipe];
[task setStandardOutput: pipe];
//The magic line that keeps your log where it belongs
[task setStandardInput:[NSPipe pipe]];

这里有一个解释:http://www.cocoadev.com/index.pl?NSTask

答案 2 :(得分:38)

肯特的文章给了我一个新的想法。这个runCommand方法不需要脚本文件,只需按行运行命令:

- (NSString *)runCommand:(NSString *)commandToRun
{
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/sh"];

    NSArray *arguments = [NSArray arrayWithObjects:
                          @"-c" ,
                          [NSString stringWithFormat:@"%@", commandToRun],
                          nil];
    NSLog(@"run command:%@", commandToRun);
    [task setArguments:arguments];

    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];

    NSFileHandle *file = [pipe fileHandleForReading];

    [task launch];

    NSData *data = [file readDataToEndOfFile];

    NSString *output = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    return output;
}

您可以像这样使用此方法:

NSString *output = runCommand(@"ps -A | grep mysql");

答案 3 :(得分:24)

以下是如何在Swift中执行此操作

  

Swift 3.0的变化:

     
      
  • NSPipe已重命名为Pipe

  •   
  • NSTask已重命名为Process

  •   

这是基于上面的墨水的Objective-C答案。他在NSString上将其写为 类别 - 对于Swift,它变为String 扩展名

扩展String.runAsCommand() - >串

extension String {
    func runAsCommand() -> String {
        let pipe = Pipe()
        let task = Process()
        task.launchPath = "/bin/sh"
        task.arguments = ["-c", String(format:"%@", self)]
        task.standardOutput = pipe
        let file = pipe.fileHandleForReading
        task.launch()
        if let result = NSString(data: file.readDataToEndOfFile(), encoding: String.Encoding.utf8.rawValue) {
            return result as String
        }
        else {
            return "--- Error running command - Unable to initialize string from file data ---"
        }
    }
}

用法:

let input = "echo hello"
let output = input.runAsCommand()
print(output)                        // prints "hello"

或只是:

print("echo hello".runAsCommand())   // prints "hello" 

实施例

@IBAction func toggleFinderShowAllFiles(_ sender: AnyObject) {

    var newSetting = ""
    let readDefaultsCommand = "defaults read com.apple.finder AppleShowAllFiles"

    let oldSetting = readDefaultsCommand.runAsCommand()

    // Note: the Command results are terminated with a newline character

    if (oldSetting == "0\n") { newSetting = "1" }
    else { newSetting = "0" }

    let writeDefaultsCommand = "defaults write com.apple.finder AppleShowAllFiles \(newSetting) ; killall Finder"

    _ = writeDefaultsCommand.runAsCommand()

}

请注意,从Process读取的Pipe结果是NSString对象。它可能是一个错误字符串,它也可以是一个空字符串,但它应该始终是NSString

所以,只要它不是nil,结果就可以转换为Swift String并返回。

如果出于某种原因,可以从文件数据初始化NSString,则该函数返回错误消息。该函数可能已经被编写为返回一个可选的String?,但这样使用起来很难,并且不会起到有用的作用,因为它不太可能发生。

答案 4 :(得分:15)

Objective-C(参见下面的Swift)

清理顶部答案中的代码,使其更具可读性,更少冗余,添加了the one-line method的好处并将其纳入NSString类别

@interface NSString (ShellExecution)
- (NSString*)runAsCommand;
@end

实现:

@implementation NSString (ShellExecution)

- (NSString*)runAsCommand {
    NSPipe* pipe = [NSPipe pipe];

    NSTask* task = [[NSTask alloc] init];
    [task setLaunchPath: @"/bin/sh"];
    [task setArguments:@[@"-c", [NSString stringWithFormat:@"%@", self]]];
    [task setStandardOutput:pipe];

    NSFileHandle* file = [pipe fileHandleForReading];
    [task launch];

    return [[NSString alloc] initWithData:[file readDataToEndOfFile] encoding:NSUTF8StringEncoding];
}

@end

用法:

NSString* output = [@"echo hello" runAsCommand];

如果您遇到输出编码问题:

// Had problems with `lsof` output and Japanese-named files, this fixed it
NSString* output = [@"export LANG=en_US.UTF-8;echo hello" runAsCommand];

希望它对你和对我未来都有用。 (嗨,你!)


Swift 4

以下是使用PipeProcessString

的Swift示例
extension String {
    func run() -> String? {
        let pipe = Pipe()
        let process = Process()
        process.launchPath = "/bin/sh"
        process.arguments = ["-c", self]
        process.standardOutput = pipe

        let fileHandle = pipe.fileHandleForReading
        process.launch()

        return String(data: fileHandle.readDataToEndOfFile(), encoding: .utf8)
    }
}

用法:

let output = "echo hello".run()

答案 5 :(得分:14)

forkexecwait应该可行,如果您不是真的在寻找Objective-C特定方式。 fork创建当前正在运行的程序的副本,exec用新的程序替换当前正在运行的程序,wait等待子进程退出。例如(没有任何错误检查):

#include <stdlib.h>
#include <unistd.h>


pid_t p = fork();
if (p == 0) {
    /* fork returns 0 in the child process. */
    execl("/other/program/to/run", "/other/program/to/run", "foo", NULL);
} else {
    /* fork returns the child's PID in the parent. */
    int status;
    wait(&status);
    /* The child has exited, and status contains the way it exited. */
}

/* The child has run and exited by the time execution gets to here. */

还有system,它运行命令就像从shell的命令行输入命令一样。它更简单,但您对情况的控制较少。

我假设您正在使用Mac应用程序,因此这些链接是针对这些函数的Apple文档,但它们都是POSIX,因此您应该在任何符合POSIX标准的情况下使用它们系统

答案 6 :(得分:11)

还有很好的旧POSIX system(“echo -en'\ 007'”);

答案 7 :(得分:7)

我写了这个“C”函数,因为NSTask是令人讨厌的......

NSString * runCommand(NSString* c) {

    NSString* outP; FILE *read_fp;  char buffer[BUFSIZ + 1];
    int chars_read; memset(buffer, '\0', sizeof(buffer));
    read_fp = popen(c.UTF8String, "r");
    if (read_fp != NULL) {
        chars_read = fread(buffer, sizeof(char), BUFSIZ, read_fp);
        if (chars_read > 0) outP = $UTF8(buffer);
        pclose(read_fp);
    }   
    return outP;
}

NSLog(@"%@", runCommand(@"ls -la /")); 

total 16751
drwxrwxr-x+ 60 root        wheel     2108 May 24 15:19 .
drwxrwxr-x+ 60 root        wheel     2108 May 24 15:19 ..
…
哦,为了完整/明确......

#define $UTF8(A) ((NSString*)[NSS stringWithUTF8String:A])

多年以后,C对我来说仍然是一个令人眼花缭乱的混乱......并且对我纠正上述严重缺点的能力缺乏信心 - 我提供的唯一橄榄枝是@ inket的答案的重新版本对于我的同伴纯粹主义者/冗长的憎恨者来说, barest of bones

id _system(id cmd) { 
   return !cmd ? nil : ({ NSPipe* pipe; NSTask * task;
  [task = NSTask.new setValuesForKeysWithDictionary: 
    @{ @"launchPath" : @"/bin/sh", 
        @"arguments" : @[@"-c", cmd],
   @"standardOutput" : pipe = NSPipe.pipe}]; [task launch];
  [NSString.alloc initWithData:
     pipe.fileHandleForReading.readDataToEndOfFile
                      encoding:NSUTF8StringEncoding]; });
}

答案 8 :(得分:3)

Custos Mortem说:

  

我很惊讶没有人真正遇到阻止/阻止呼叫问题

有关NSTask的阻止/非阻止呼叫问题,请阅读以下内容:

  

asynctask.m - 示例代码,演示如何实现异步stdin,stdout&amp;用于使用NSTask

处理数据的stderr流

asynctask.m的源代码可在GitHub获得。

答案 9 :(得分:2)

或者由于Objective C只是C,顶部有一些OO层,你可以使用posix conterparts:

int execl(const char *path, const char *arg0, ..., const char *argn, (char *)0);
int execle(const char *path, const char *arg0, ..., const char *argn, (char *)0, char *const envp[]);
int execlp(const char *file, const char *arg0, ..., const char *argn, (char *)0);
int execlpe(const char *file, const char *arg0, ..., const char *argn, (char *)0, char *const envp[]);
int execv(const char *path, char *const argv[]);
int execve(const char *path, char *const argv[], char *const envp[]);
int execvp(const char *file, char *const argv[]);
int execvpe(const char *file, char *const argv[], char *const envp[]); 

它们包含在unistd.h头文件中。

答案 10 :(得分:2)

如果终端命令需要管理员权限(又名sudo),请改用AuthorizationExecuteWithPrivileges。 下面将创建一个名为“com.stackoverflow.test”的文件,它是根目录“/ System / Library / Caches”。

AuthorizationRef authorizationRef;
FILE *pipe = NULL;
OSStatus err = AuthorizationCreate(nil,
                                   kAuthorizationEmptyEnvironment,
                                   kAuthorizationFlagDefaults,
                                   &authorizationRef);

char *command= "/usr/bin/touch";
char *args[] = {"/System/Library/Caches/com.stackoverflow.test", nil};

err = AuthorizationExecuteWithPrivileges(authorizationRef,
                                         command,
                                         kAuthorizationFlagDefaults,
                                         args,
                                         &pipe); 

答案 11 :(得分:2)

除了上述几个出色的答案外,我还使用以下代码在后台处理命令的输出,并避免了[file readDataToEndOfFile]的阻塞机制。

- (NSString *)runCommand:(NSString *)commandToRun
{
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath:@"/bin/sh"];

    NSArray *arguments = [NSArray arrayWithObjects:
                          @"-c" ,
                          [NSString stringWithFormat:@"%@", commandToRun],
                          nil];
    NSLog(@"run command:%@", commandToRun);
    [task setArguments:arguments];

    NSPipe *pipe = [NSPipe pipe];
    [task setStandardOutput:pipe];

    NSFileHandle *file = [pipe fileHandleForReading];

    [task launch];

    [self performSelectorInBackground:@selector(collectTaskOutput:) withObject:file];
}

- (void)collectTaskOutput:(NSFileHandle *)file
{
    NSData      *data;
    do
    {
        data = [file availableData];
        NSLog(@"%@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] );

    } while ([data length] > 0); // [file availableData] Returns empty data when the pipe was closed

    // Task has stopped
    [file closeFile];
}