shell - Combine bash commands with each other -
i using following code execute bash command swift:
func runcommand(cmd : string, args : string...) -> (output: [string], error: [string], exitcode: int32) { var output : [string] = [] var error : [string] = [] let task = nstask() task.launchpath = cmd task.arguments = args let outpipe = nspipe() task.standardoutput = outpipe let errpipe = nspipe() task.standarderror = errpipe task.launch() let outdata = outpipe.filehandleforreading.readdatatoendoffile() if var string = string.fromcstring(unsafepointer(outdata.bytes)) { string = string.stringbytrimmingcharactersinset(nscharacterset.newlinecharacterset()) output = string.componentsseparatedbystring("\n") } let errdata = errpipe.filehandleforreading.readdatatoendoffile() if var string = string.fromcstring(unsafepointer(errdata.bytes)) { string = string.stringbytrimmingcharactersinset(nscharacterset.newlinecharacterset()) error = string.componentsseparatedbystring("\n") } task.waituntilexit() let status = task.terminationstatus return (output, error, status) }
credit: get terminal output after command swift
when run function following code:
runcommand("/sbin/ifconfig", args: "en1", "|", "grep", "ether")
to simulate running following command shell:
ifconfig en1 | grep ether
that result in output this:
ether xx:xx:xx:xx:xx:xx
i following error:
ifconfig: |: bad value
i'm guessing because commands aren't being combined , "|" symbol being interpreted direct argument ifconfig.
is there way simulate type of shell behaviour (the usage of "|" symbol filter output of command) within swift?
solved nhgrif's comment: combine bash commands each other