How to execute shell command which has pipe?
-
Hi,
I want to run below command with Qt.cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
My code is:
QProcess process; process.start("cat", QStringList() <<"/proc/cpuinfo"<< "|" << "grep"<< "Serial"<< "|"<< "cut"<< "-d"<< "' '"<< "-f"<< "2", QIODevice::ReadOnly); process.waitForFinished(-1); // will wait forever until finished QString stdout = process.readAllStandardOutput(); QString stderr = process.readAllStandardError(); qDebug()<<stdout<< endl; qDebug()<<stderr<< endl;
But I gues I got error with piping? Because I got below error message::
"/bin/cat: invalid option -- 'd'\nTry '/bin/cat --help' for more information.\n"
What the command String List must be?..
Regards,
Mucip:) -
@Mucip
First things first while I think of it:cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
That's 2 pipes. One pipe is wasted: 100% equivalent with 1 pipe is:
grep Serial /proc/cpuinfo | cut -d ' ' -f 2
[And if you know your
awk
orsed
you could rewrite this as one command, no piping :) ]A separate point is whether it's worth doing any commands/piping for this. You're going to be reading the output of this back into your parent process. While you're doing that, both
grep Serial
and thecut
are pretty simple to implement while you read the output stream in code, so as @aha_1980 has said you could get rid of the need to do anyQProcess
/piping if you just open/proc/cpuinfo
into aQFile
and do the read-parsing yourself.If you do want to do it via processes/piping, you have to change your code to take one of two approaches:
-
You cannot just pass the whole command line to
cat
executable, that's why you are getting the error message. You have to split it up: taking my rewrite above, oneQProcess
for just thegrep Serial /proc/cpuinfo
, another one for thecut -d ' ' -f 2
, with output/input piping viaQProcess
(no|
). Efficient, but more code. -
You can pass the whole line to one process if you use
/bin/sh
or/bin/bash
to handle the commands & the piping for you.
/bin/sh -c "grep Serial /proc/cpuinfo | cut -d ' ' -f 2"
Now you use just one
QProcess
for/bin/sh
, with arg #1 being-c
and arg #2 beinggrep Serial /proc/cpuinfo | cut -d ' ' -f 2
. Marginally less efficient, but saves writing multipleQProcess
. -
-
Hi,
-
Hi
QProcess is meant to run a single process so it will give the pipe as a parameter as you might have noticed.
I think this is what you want.
https://stackoverflow.com/questions/20901884/piping-or-command-chaining-with-qprocess -
@Mucip and the extraction of a serial number can easily be done in code too ;)
Edit: thinking about it again, to read the contents of
/proc/cpuinfo
you don't even needcat
orQProcess
.Just open the file with
QFile
and find the line of interest. That's it :) -
@Mucip
First things first while I think of it:cat /proc/cpuinfo | grep Serial | cut -d ' ' -f 2
That's 2 pipes. One pipe is wasted: 100% equivalent with 1 pipe is:
grep Serial /proc/cpuinfo | cut -d ' ' -f 2
[And if you know your
awk
orsed
you could rewrite this as one command, no piping :) ]A separate point is whether it's worth doing any commands/piping for this. You're going to be reading the output of this back into your parent process. While you're doing that, both
grep Serial
and thecut
are pretty simple to implement while you read the output stream in code, so as @aha_1980 has said you could get rid of the need to do anyQProcess
/piping if you just open/proc/cpuinfo
into aQFile
and do the read-parsing yourself.If you do want to do it via processes/piping, you have to change your code to take one of two approaches:
-
You cannot just pass the whole command line to
cat
executable, that's why you are getting the error message. You have to split it up: taking my rewrite above, oneQProcess
for just thegrep Serial /proc/cpuinfo
, another one for thecut -d ' ' -f 2
, with output/input piping viaQProcess
(no|
). Efficient, but more code. -
You can pass the whole line to one process if you use
/bin/sh
or/bin/bash
to handle the commands & the piping for you.
/bin/sh -c "grep Serial /proc/cpuinfo | cut -d ' ' -f 2"
Now you use just one
QProcess
for/bin/sh
, with arg #1 being-c
and arg #2 beinggrep Serial /proc/cpuinfo | cut -d ' ' -f 2
. Marginally less efficient, but saves writing multipleQProcess
. -