निम्न फ़ंक्शन मैकोज़ पर स्विफ्ट 3 में एक प्रक्रिया निष्पादित करता है। लेकिन अगर मैं उबंटू में एक ही कोड चलाता हूं तो मुझे त्रुटि मिलती है कि Process
एक अनसुलझा पहचानकर्ता है।लिनक्स के लिए स्विफ्ट 3 में प्रक्रिया() का उपयोग कैसे करें?
मैं उबंटू के लिए स्विफ्ट 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 ""
}
मैंने 'कार्य' के साथ 'प्रक्रिया 'को बदल दिया और अब यह काम करता है। धन्यवाद! – Sebastian