获取Cocoa中shell脚本的输出

获取Cocoa中shell脚本的输出,cocoa,nstextview,shell,Cocoa,Nstextview,Shell,是否有一种方法可以让我运行shell脚本,并在NSTextView中显示输出?我根本不希望用户对shell脚本进行任何输入,因为调用is只是为了编译一堆文件。到目前为止,shell脚本部分工作正常,但我就是不知道如何运行它并在NSTextView中显示输出。我知道可以使用system()和NSTask运行shell脚本,但如何将其输出到NSTextView中呢 NSTask具有setStandardOutput方法,该方法接受NSFileHandle或NSPipe对象。因此,如果您创建NSPip

是否有一种方法可以让我运行shell脚本,并在NSTextView中显示输出?我根本不希望用户对shell脚本进行任何输入,因为调用is只是为了编译一堆文件。到目前为止,shell脚本部分工作正常,但我就是不知道如何运行它并在NSTextView中显示输出。我知道可以使用system()和NSTask运行shell脚本,但如何将其输出到NSTextView中呢

NSTask
具有
setStandardOutput
方法,该方法接受
NSFileHandle
NSPipe
对象。因此,如果您创建
NSPipe
对象并将其设置为
NSTask
standard output
,那么您可以使用
NSPipe
fileHandleForReading
来获取
NSFileHandle
,反过来,您可以从中
读取数据到文件
读取数据长度:
。这样做(代码未经测试):


如果需要通配符扩展,请将unix命令传递给/bin/sh

- (NSString *)unixSinglePathCommandWithReturn:(NSString *) command {
    // performs a unix command by sending it to /bin/sh and returns stdout.
    // trims trailing carriage return
    // not as efficient as running command directly, but provides wildcard expansion

    NSPipe *newPipe = [NSPipe pipe];
    NSFileHandle *readHandle = [newPipe fileHandleForReading];
    NSData *inData = nil;
    NSString* returnValue = nil;

    NSTask * unixTask = [[NSTask alloc] init];
    [unixTask setStandardOutput:newPipe];
    [unixTask setLaunchPath:@"/bin/csh"];
    [unixTask setArguments:[NSArray arrayWithObjects:@"-c", command , nil]]; 
    [unixTask launch];
    [unixTask waitUntilExit];
    int status = [unixTask terminationStatus];

    while ((inData = [readHandle availableData]) && [inData length]) {

        returnValue= [[NSString alloc] 
                      initWithData:inData encoding:[NSString defaultCStringEncoding]];

        returnValue = [returnValue substringToIndex:[returnValue length]-1];

        NSLog(@"%@",returnValue);
    }

    return returnValue;

}

根据任务将运行多长时间,最好在后台读取数据,使用NSFileHandle的方法之一。我基本上按照上面所说的做,并在后台使用线程运行update output字段,任务运行,但我在textview中从未看到任何内容。它接线正确。以下是在后台运行的代码:“[NSThread detachNewThreadSelector:@selector(updateOutputField:)toTarget:self withObject:nil];”它在调试器中显示输出,但在文本字段中显示on。按如下方式启动:[myTask setLaunchPath:@”/bin/sh];[myTask setArguments:[NSArray arrayWithObjects:scr],nil]];[myTask setStandardOutput:standardOutputPipe];[我的任务启动];
- (NSString *)unixSinglePathCommandWithReturn:(NSString *) command {
    // performs a unix command by sending it to /bin/sh and returns stdout.
    // trims trailing carriage return
    // not as efficient as running command directly, but provides wildcard expansion

    NSPipe *newPipe = [NSPipe pipe];
    NSFileHandle *readHandle = [newPipe fileHandleForReading];
    NSData *inData = nil;
    NSString* returnValue = nil;

    NSTask * unixTask = [[NSTask alloc] init];
    [unixTask setStandardOutput:newPipe];
    [unixTask setLaunchPath:@"/bin/csh"];
    [unixTask setArguments:[NSArray arrayWithObjects:@"-c", command , nil]]; 
    [unixTask launch];
    [unixTask waitUntilExit];
    int status = [unixTask terminationStatus];

    while ((inData = [readHandle availableData]) && [inData length]) {

        returnValue= [[NSString alloc] 
                      initWithData:inData encoding:[NSString defaultCStringEncoding]];

        returnValue = [returnValue substringToIndex:[returnValue length]-1];

        NSLog(@"%@",returnValue);
    }

    return returnValue;

}