swift 执行 shell 脚本

创建CommondLineTool 项目

@discardableResult
func shell(_ args: [String]) -> String {
    let task = Process()
    task.launchPath = "/usr/bin/env"
    task.arguments = args
    let pipe = Pipe()
    task.standardOutput = pipe
    task.launch()
    task.waitUntilExit()
    let data = pipe.fileHandleForReading.readDataToEndOfFile()
    let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
    return output;
}
print(shell(["pwd"]));

你可能感兴趣的:(swift 执行 shell 脚本)