Created
February 14, 2019 01:24
-
-
Save theangelperalta/7b8ad82ae38f75f9533ea3b7e7a1944c to your computer and use it in GitHub Desktop.
Run Shell Commands from Swift
This file contains 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
#!/usr/bin/env swift | |
import Foundation | |
@discardableResult | |
func shell(_ command: String) -> String { | |
let task = Process() | |
task.launchPath = "/bin/bash" | |
task.arguments = ["-c", command] | |
let pipe = Pipe() | |
task.standardOutput = pipe | |
task.launch() | |
let data = pipe.fileHandleForReading.readDataToEndOfFile() | |
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String | |
return output | |
} | |
shell("ls") | |
shell("open -a safari") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment