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