Created
March 22, 2019 18:58
-
-
Save bill350/594134f3d9a45570438960d44b5036dd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MARK:- Scripting | |
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.count > 0 { | |
let lastIndex = output.index(before: output.endIndex) | |
return String(output[output.startIndex ..< lastIndex]) | |
} | |
return output | |
} | |
@discardableResult | |
func bash(command: String, arguments: [String]) -> String { | |
let whichPathForCommand = shell(launchPath: "/bin/bash", arguments: [ "-l", "-c", "which \(command)" ]) | |
return shell(launchPath: whichPathForCommand, arguments: arguments) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment