如何在Swift 3 for Linux中使用Process()?

Seb*_*ian 7 ubuntu swift swift3

以下函数在macOS上的Swift 3中执行一个进程.但是如果我在Ubuntu中运行相同的代码,我会收到错误,这Process是一个未解析的标识符.

如何在Swift 3中为Ubuntu运行进程/任务并获取其输出?

import Foundation

// runs a Shell command with arguments and returns the output or ""
class func shell(_ command: String, args: [String] = []) -> String {

    let task = Process()
    task.launchPath = command
    task.arguments = args

    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()

    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String? = String(data: data,
                                 encoding: String.Encoding.utf8)
    task.waitUntilExit()

    if let output = output {
        if !output.isEmpty {
            // remove whitespaces and newline from start and end
            return output.trimmingCharacters(in: .whitespacesAndNewlines)
        }
    }
    return ""
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*n R 6

我目前无法自己测试,但根据源代码 https://github.com/apple/swift-corelibs-foundation/blob/master/Foundation/NSTask.swift,Task在Linux上调用相应的类(仍然)不像Process Apple平台那样.