小编典典

快速执行命令后获取终端输出

swift

我在终端中使用以下代码运行一些命令:

system("the command here")

然后,我想知道运行此命令的结果是什么,例如,如果运行

system("git status")

我想阅读有关回购中更改的实际信息。有什么办法可以迅速做到这一点?


阅读 382

收藏
2020-07-07

共1个答案

小编典典

NSTask是用于将另一个程序作为子进程运行的类。您可以捕获程序的输出,错误输出,退出状态等等。

扩展我对xcode 6 swiftsystem()命令的回答,这是一个简单的实用函数,用于同步运行命令,并返回输出,错误输出和退出代码(现已针对Swift 2更新):

func runCommand(cmd : String, args : String...) -> (output: [String], error: [String], exitCode: Int32) {

    var output : [String] = []
    var error : [String] = []

    let task = NSTask()
    task.launchPath = cmd
    task.arguments = args

    let outpipe = NSPipe()
    task.standardOutput = outpipe
    let errpipe = NSPipe()
    task.standardError = errpipe

    task.launch()

    let outdata = outpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String.fromCString(UnsafePointer(outdata.bytes)) {
        string = string.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
        output = string.componentsSeparatedByString("\n")
    }

    let errdata = errpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String.fromCString(UnsafePointer(errdata.bytes)) {
        string = string.stringByTrimmingCharactersInSet(NSCharacterSet.newlineCharacterSet())
        error = string.componentsSeparatedByString("\n")
    }

    task.waitUntilExit()
    let status = task.terminationStatus

    return (output, error, status)
}

用法示例:

let (output, error, status) = runCommand("/usr/bin/git", args: "status")
print("program exited with status \(status)")
if output.count > 0 {
    print("program output:")
    print(output)
}
if error.count > 0 {
    print("error output:")
    print(error)
}

或者,如果您仅对输出感兴趣,而不对错误消息或退出代码感兴趣:

let output = runCommand("/usr/bin/git", args: "status").output

输出和错误输出以字符串数组形式返回,每行一个字符串。

的第一个参数runCommand()必须是可执行文件的完整路径,例如"/usr/bin/git"。您可以使用外壳程序启动程序(system()也可以这样做):

let (output, error, status) = runCommand("/bin/sh", args: "-c", "git status")

优点是可以通过默认搜索路径自动找到“ git”可执行文件。缺点是,如果参数包含空格或其他在Shell中具有特殊含义的字符,则必须正确引用/转义参数。


Swift 3 更新

func runCommand(cmd : String, args : String...) -> (output: [String], error: [String], exitCode: Int32) {

    var output : [String] = []
    var error : [String] = []

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

    let outpipe = Pipe()
    task.standardOutput = outpipe
    let errpipe = Pipe()
    task.standardError = errpipe

    task.launch()

    let outdata = outpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String(data: outdata, encoding: .utf8) {
        string = string.trimmingCharacters(in: .newlines)
        output = string.components(separatedBy: "\n")
    }

    let errdata = errpipe.fileHandleForReading.readDataToEndOfFile()
    if var string = String(data: errdata, encoding: .utf8) {
        string = string.trimmingCharacters(in: .newlines)
        error = string.components(separatedBy: "\n")
    }

    task.waitUntilExit()
    let status = task.terminationStatus

    return (output, error, status)
}
2020-07-07