के लिए
यदि आप कमांड कॉल करने के लिए बैश पर्यावरण का उपयोग करना चाहते हैं तो निम्न बैश फ़ंक्शन का उपयोग करें जो Legoless के एक निश्चित संस्करण का उपयोग करता है। मुझे शैल फ़ंक्शन के परिणाम से पिछली नई लाइन को हटाना पड़ा।
स्विफ्ट 3.0: (Xcode8)
import Foundation
func shell(launchPath: String, arguments: [String]) -> String
{
let task = Process()
task.launchPath = launchPath
task.arguments = arguments
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = String(data: data, encoding: String.Encoding.utf8)!
if output.characters.count > 0 {
//remove newline character.
let lastIndex = output.index(before: output.endIndex)
return output[output.startIndex ..< lastIndex]
}
return output
}
func bash(command: String, arguments: [String]) -> String {
let whichPathForCommand = shell(launchPath: "/bin/bash", arguments: [ "-l", "-c", "which \(command)" ])
return shell(launchPath: whichPathForCommand, arguments: arguments)
}
उदाहरण के लिए वर्तमान कार्यशील निर्देशिका की वर्तमान काम कर Git शाखा पाने के लिए:
let currentBranch = bash("git", arguments: ["describe", "--contains", "--all", "HEAD"])
print("current branch:\(currentBranch)")
यह वास्तव में बहुत साफ दिखता है। – Robert
'एनएसटीस्क' का नाम बदलकर 'प्रक्रिया' – Mateusz
कर रहा है, मैं इसका उपयोग कैसे कर सकता हूं (जैसे सफारी)?/open -a applicationname कभी काम नहीं करता है। – dylan