1

I created a "Shebang" swift script.

I am parsing the input arguments using Process.arguments

How can I read the "piped" information in the case it was piped into the script?

#!/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")
}
Avba
  • 14,822
  • 20
  • 92
  • 192
  • 1
    Use standard input? http://stackoverflow.com/q/24046952/2088135 – Tom Fenech Oct 20 '15 at 14:47
  • Thanks,I tried that but it freezes up. – Avba Oct 20 '15 at 14:50
  • print("Handling pipe") let pipe = NSPipe() let handle = pipe.fileHandleForReading let data = handle.availableData print("End of pip") print(data) – Avba Oct 20 '15 at 14:50
  • Note the difference between `fileHandleForReading` in your attempt and `fileHandleWithStandardInput` as suggested in the linked answer. – Tom Fenech Oct 20 '15 at 14:52

1 Answers1

2

Changed from NSPipe to NSFileHandle.fileHandleWithStandardInput() let data = pipe.availableData seems to do the trick. Thanks to @Tom Fenech

Avba
  • 14,822
  • 20
  • 92
  • 192