簡體   English   中英

當命令無法執行“找不到命令”時,NSTask會收到通知

[英]NSTask get notified when a command can't execute “command not found”

我正在嘗試從我的應用程序運行終端命令/腳本,但是一切正常,但是當命令錯誤並且無法執行時,我得到如下信息:

在此處輸入圖片說明

但是這個“ / bin / bash:line ...”字符串不在我從任務中獲得的輸出字符串中,有什么方法可以在我的應用程序中獲取這些錯誤,也可以通過任何方式通知它們嗎?

我的代碼

    // Create a new task
    let task: Process = Process()
    task.environment = env
    task.launchPath = "/usr/bin/env"
    task.arguments = ["/bin/bash", "-c", command.scriptCode]

    // Assign output pipes
    let pipe: Pipe = Pipe()
    let outHandle: FileHandle = pipe.fileHandleForReading
    task.standardOutput = pipe

    outHandle.readabilityHandler = { pipe in
        if let line = String(data: pipe.availableData, encoding: String.Encoding.utf8) {
            if line.contains("command not found") {
                // never triggered 😩
            } else {
                print("New ouput: \(NSDate() )\(line)")
            }
        } else {
            print("Error decoding data: \(pipe.availableData)")
        }
    }

您正在尋找的是NSTaskProcess類的standardError屬性。 就像您對standardOutput一樣,將管道分配給該屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM