Reputation: 787
func exec(_ path: String, _ args: String...) -> Int32 {
let task = Process()
task.launchPath = path
task.arguments = args
task.launch()
task.waitUntilExit()
return task.terminationStatus
}
exec("/bin/echo", "toto", ">>", "pathToFile")
Hi everyone! Someone could explain me why this code print into xcode terminal instead of write to the file ? Thanks!
Upvotes: 2
Views: 1307
Reputation: 53000
Multiple commands, pipes, redirection, etc., etc. are all handled by the shell, not by individual commands themselves. If you want to run "echo" and redirect its output you must run the shell and pass it the command line to parse and execute. Try:
exec("/bin/sh", "-c", "echo toto >> /tmp/pathToFile")
Upvotes: 2