2015-10-20 127 views
1

我创建了一个“Shebang”swift脚本。Swift - 读取管道输入

我解析使用Process.arguments

如何我可以读出“管道”的情况下,信息有人管道输送到脚本的输入参数?

#!/usr/bin/env xcrun swift 
import Foundation 
for argument in Process.arguments { 
// parse the input arguments 
} 

// if the input is piped into the script?: 
// if PIPE??? { 
print("Handling pipe") 
let pipe = NSPipe() 
let handle = pipe.fileHandleForReading 
let data = handle.readDataToEndOfFile() 
print("End of pip") 
} 
+1

使用标准输入? http://stackoverflow.com/q/24046952/2088135 –

+0

谢谢,我试过了,但它冻结了。 –

+0

打印( “处理管道”) 设管= NSPipe() 让手柄= pipe.fileHandleForReading 让数据= handle.availableData 打印 打印(数据) –

回答

1

NSPipe改为 NSFileHandle.fileHandleWithStandardInput() let data = pipe.availableData 似乎这样的伎俩。 感谢 @Tom Fenech