2016-09-29 3 views
7

以下の関数は、MacOSのSwift 3でプロセスを実行します。しかし、Ubuntuで同じコードを実行すると、Processが未解決の識別子であるというエラーが表示されます。Swift 3 for LinuxでProcess()を使用するには?

私はUbuntuのSwift 3でプロセス/タスクを実行し、その出力を取得するにはどうすればよいですか?

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 "" 
} 

答えて

2

CommandLineProcessを交換してみてください。私は現在、自分自身をそれをテストすることはできません。このgithubのissueこのSO question

+0

これは別のことです。 Swift 3では、(NS)TaskはProcessに、ProcessはCommandLineにそれぞれ変更されました。この質問は、私が理解しているように、前者に関するものです。 –

関連する問題