Last active
September 20, 2015 06:56
-
-
Save asus4/8c7bac3ea607c638b16b to your computer and use it in GitHub Desktop.
Execute shell command from swift
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
// | |
// 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