Issue in QProcess with quotes and spaces
-
Hi everyone,
I'm trying to execute an external command from my QT program, the problem is that the command has several arguments and quotes. In bash only, the command runs fine but I've tried to execute it with QProcess and it has been an impossible task for me. I've has tried with single quotes, triple quotes and many more ways, but none works.
The command in bash is:
tshark -n -r example.pcap -Y "rtp.ssrc==0x00002e3d" -T fields -e rtp.payload
Anybody can give me some idea to do that with QProcess please?
Thanks in advance -
Try this:
QStringList args = { "-n", "-r", "example.pcap", "-Y", "\"rtp.ssrc==0x00002e3d\"", "-T", "fields", "-e", "rtp.payload" }; QProcess proc("tshark", args);
-
Hi everyone,
I'm trying to execute an external command from my QT program, the problem is that the command has several arguments and quotes. In bash only, the command runs fine but I've tried to execute it with QProcess and it has been an impossible task for me. I've has tried with single quotes, triple quotes and many more ways, but none works.
The command in bash is:
tshark -n -r example.pcap -Y "rtp.ssrc==0x00002e3d" -T fields -e rtp.payload
Anybody can give me some idea to do that with QProcess please?
Thanks in advance@Shutta
As @sierdzio has written, it's often nicer/clearer/safer to pass your arguments separately forQProcess
to sort out quoting. (Though that may be a pain, depending on how your code is arranged to construct the command.)(However, I don't see an overload of
QProcess()
constructor matching his suggestion, I think he meantQProcess proc; proc.start("tshark", args);
.)However, just OOI, in whatever you say you tried what would not have worked with:
proc.start("tshark -n -r example.pcap -Y \"rtp.ssrc==0x00002e3d\" -T fields -e rtp.payload")
?
And for the record, in this particular case there is nothing in your
rtp.ssrc==0x00002e3d
argument which requires quoting, so you could simply omit the complexities of quoting and go:proc.start("tshark -n -r example.pcap -Y rtp.ssrc==0x00002e3d -T fields -e rtp.payload")
though you may have good reason to be quoting that argument in case in contains other characters than it does.
-
@JNBarchan said in Issue in QProcess with quotes and spaces:
(However, I don't see an overload of QProcess() constructor matching his suggestion, I think he meant QProcess proc; proc.start("tshark", args);.)
Yeah you are right, sorry, I have not checked the docs before sending ;-)
-
@Shutta
As @sierdzio has written, it's often nicer/clearer/safer to pass your arguments separately forQProcess
to sort out quoting. (Though that may be a pain, depending on how your code is arranged to construct the command.)(However, I don't see an overload of
QProcess()
constructor matching his suggestion, I think he meantQProcess proc; proc.start("tshark", args);
.)However, just OOI, in whatever you say you tried what would not have worked with:
proc.start("tshark -n -r example.pcap -Y \"rtp.ssrc==0x00002e3d\" -T fields -e rtp.payload")
?
And for the record, in this particular case there is nothing in your
rtp.ssrc==0x00002e3d
argument which requires quoting, so you could simply omit the complexities of quoting and go:proc.start("tshark -n -r example.pcap -Y rtp.ssrc==0x00002e3d -T fields -e rtp.payload")
though you may have good reason to be quoting that argument in case in contains other characters than it does.
@JNBarchan thanks very much for your answer.
The way proc.start("tshark -n -r example.pcap -Y "rtp.ssrc==0x00002e3d" -T fields -e rtp.payload") didn't work for me, but I don't know why it do works copying the whole command within a QString variable. Also works without quotes, but I hasn't proved because in original formula appeared as rtp.ssrc == 0x00002e3d (with spaces).
Now is working fine, thanks -
@JNBarchan thanks very much for your answer.
The way proc.start("tshark -n -r example.pcap -Y "rtp.ssrc==0x00002e3d" -T fields -e rtp.payload") didn't work for me, but I don't know why it do works copying the whole command within a QString variable. Also works without quotes, but I hasn't proved because in original formula appeared as rtp.ssrc == 0x00002e3d (with spaces).
Now is working fine, thanks@Shutta
proc.start("tshark -n -r example.pcap -Y "rtp.ssrc==0x00002e3d" -T fields -e rtp.payload")
would not work, I never suggested that. Please use copy & paste for examples from suggestions here.If you now say you want spaces in one of the arguments (it always helps if you post the exact question as you want it in the first place), you would need:
proc.start("tshark -n -r example.pcap -Y \"rtp.ssrc == 0x00002e3d\" -T fields -e rtp.payload")
Or you might prefer to do as @sierdzio suggested, with separate arguments in a list, to avoid dealing with quoting. However, note that what he wrote is not quite right, you would then not want embedded quotes, hisrtp.ssrc
argument should read"rtp.ssrc == 0x00002e3d"
.