Execute a shell command

 Problem

I want to execute a shell command (as in the terminal) and get its output.



 Recipe


// Create a Task instance
let task = NSTask()

// Set the task parameters
task.launchPath = "/usr/bin/env"
task.arguments = ["pwd"]
 
// Create a Pipe and make the task
// put all the output there
let pipe = NSPipe()
task.standardOutput = pipe

// Launch the task
task.launch()
 
// Get the data
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output = NSString(data: data, encoding: NSUTF8StringEncoding)

print(output!)
				

Heard the news?

The Swift Cookbook for Swift 3 is at last here! For iBooks, Kindle and Paperback.
Hurry up - and Check it out! ;-)

 

 Playground