Okay so I'm trying to run a Mosquitto publish command in bash from a Swift application for MacOS. Here is my code:
@IBAction func buttonClicked(_ sender: Any) {
let mosquittoCommand = "mosquitto_pub --cert blahblah.pem --key blahblah.key --cafile blahblah.pem -h 'blah.blah.com' -p 443 -t 'blah/blah/blah/blah' -m '{\"msg\": \"blahblahblah\", \"time\": \"2019-08-07T15:12:00Z\", \"id\": \"blah-blah-blah\", \"localpwd\": \"blahblahblah\"}' --tls-alpn x-amzn-mqtt-ca -i 'blahblahblah'"
print(shell("cd /Users/Me/Desktop/certs && " + mosquittoCommand))
}
func shell(_ command: String) -> String {
let task = Process()
task.launchPath = "/usr/bin/env"
task.arguments = ["-c", command]
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let output: String = NSString(data: data, encoding: String.Encoding.utf8.rawValue)! as String
return output
}
I'm getting the following error:
/usr/bin/env: illegal option -- c
usage: env [-iv] [-P utilpath] [-S string] [-u name]
[name=value ...] [utility [argument ...]]
You'll have to trust me that running the command in a terminal window directly works as expected. The only difference is the escape characters in the mosquitto command to prevent the quotations messing up the command. Perhaps the escape chars are causing the problems?
I have no idea what the error is trying to tell me. Any advice would be greatly appreciated. Thanks.
EDIT - I've made sure chaining some basic commands (pwd, cd... etc) from Swift works. So it's definitely set up correctly to be able to run commands like this, I just don't know why it can't run the Mosquitto publish command.