Skip to content

Instantly share code, notes, and snippets.

@jbadger3
Created November 29, 2022 20:42
Show Gist options
  • Save jbadger3/be66e2e64cc3084fb433252495a7df7a to your computer and use it in GitHub Desktop.
Save jbadger3/be66e2e64cc3084fb433252495a7df7a to your computer and use it in GitHub Desktop.
Demo using async-await to read from a FileHandle
import Foundation
let outputPipe = Pipe()
let process = Process()
process.executableURL = URL(fileURLWithPath:"/bin/bash")
process.arguments = ["-c", "ls"]
process.standardOutput = outputPipe
try? process.run()
for try await line in outputPipe.fileHandleForReading.bytes.lines {
print(line)
}
let standardInputFH = process.standardInput as! FileHandle
let standardErrorFH = process.standardError as! FileHandle
print("PID: \(process.processIdentifier)")
print("stdin FD: \(standardInputFH.fileDescriptor)")
print("stdout FD: \(outputPipe.fileHandleForReading.fileDescriptor)")
print("stderr FD: \(standardErrorFH.fileDescriptor)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment