I have been working on a command line tool in swift but due to excessive size of the executable(around 10 mb) I need to re-write it in objective-c. Now I am not a fan of objective-c, thanks to it's lengthy syntax. So I have this function which I use to call my shell script and return the output. Can some please convert this into objective-c, I am having a hard time.
func shell(_ args: String) -> String {
var outstr = ""
let task = Process()
task.launchPath = "/bin/sh"
task.arguments = ["-c", args]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
if let output = NSString(data: data, encoding: String.Encoding.utf8.rawValue) {
outstr = output as String
}
task.waitUntilExit()
return outstr
}
then I call this method in swift something like
let shellOutput = shell("sh \(scriptPath) \(arg1) \(arg2)")
before you mark it duplicate or post answers, I really need the shell script output in some variable.