Skip to content

Instantly share code, notes, and snippets.

@asus4
Last active September 20, 2015 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asus4/8c7bac3ea607c638b16b to your computer and use it in GitHub Desktop.
Save asus4/8c7bac3ea607c638b16b to your computer and use it in GitHub Desktop.
Execute shell command from swift
//
// ShellCommand.swift
//
// Created by Koki Ibukuro on 9/19/15.
// Copyright © 2015 asus4. All rights reserved.
//
import Foundation
public struct ShellCommand {
// Export command sync
static public func execute(cmdPath: String, args: String...) -> String {
let task = NSTask()
task.launchPath = cmdPath
task.arguments = args
let outPipe = NSPipe()
task.standardOutput = outPipe
task.launch()
let output = outPipe.fileHandleForReading.readDataToEndOfFile()
guard let outstr: String =
NSString(data: output, encoding: NSUTF8StringEncoding) as String? else {
return ""
}
return outstr
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment