Skip to content

Instantly share code, notes, and snippets.

@bill350
Created March 22, 2019 18:58
Show Gist options
  • Save bill350/594134f3d9a45570438960d44b5036dd to your computer and use it in GitHub Desktop.
Save bill350/594134f3d9a45570438960d44b5036dd to your computer and use it in GitHub Desktop.
// 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