Skip to content

Instantly share code, notes, and snippets.

@MainasuK
Forked from andreacipriani/Bash.swift
Last active May 21, 2020 00:54
Show Gist options
  • Save MainasuK/b7a0eb837ba46951187945b7f789f717 to your computer and use it in GitHub Desktop.
Save MainasuK/b7a0eb837ba46951187945b7f789f717 to your computer and use it in GitHub Desktop.
Execute shell/bash commands from Swift
#!/usr/bin/swift
import Foundation
class Bash {
static var debugEnabled = false
// save command search time
static var commandCache: [String: String] = [:]
@discardableResult
func run(_ command: String, arguments: [String] = [], environment: [String: String]? = ProcessInfo.processInfo.environment, _line: Int = #line) throws -> String {
let _command: String
if let cache = Bash.commandCache[command] {
_command = cache
} else {
var theCommand = try run(command: "/bin/bash" , arguments: ["-l", "-c", "which \(command)"], environment: environment)
theCommand = theCommand.trimmingCharacters(in: .whitespacesAndNewlines)
_command = theCommand
Bash.commandCache[command] = theCommand
}
let arguments = arguments.map { $0.trimmingCharacters(in: .whitespacesAndNewlines) }
let result = try run(command: _command, arguments: arguments, environment: environment)
if Bash.debugEnabled {
print("+\((#file as NSString).lastPathComponent):\(_line)> \(_command) \(arguments.joined(separator: " "))")
print(result)
}
return result
}
private func run(command: String, arguments: [String] = [], environment: [String: String]? = nil) throws -> String {
let process = Process()
process.executableURL = URL(fileURLWithPath: command)
process.arguments = arguments
if let environment = environment { process.environment = environment }
let outputPipe = Pipe()
process.standardOutput = outputPipe
try process.run()
let outputData = outputPipe.fileHandleForReading.readDataToEndOfFile()
var output = String(decoding: outputData, as: UTF8.self)
process.waitUntilExit()
if output.hasSuffix("\n") {
output.removeLast(1)
}
if process.terminationStatus != 0 { fatalError("shell execute coccus fail") }
return output
}
}
Bash.debugEnabled = true
let bash = Bash()
try bash.run("ls", arguments: [])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment