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:) -
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 2That'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
awkorsedyou 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 Serialand thecutare 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/cpuinfointo aQFileand 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
catexecutable, that's why you are getting the error message. You have to split it up: taking my rewrite above, oneQProcessfor 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/shor/bin/bashto handle the commands & the piping for you.
/bin/sh -c "grep Serial /proc/cpuinfo | cut -d ' ' -f 2"Now you use just one
QProcessfor/bin/sh, with arg #1 being-cand 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 -
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 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/cpuinfoyou don't even needcatorQProcess.Just open the file with
QFileand find the line of interest. That's it :) -
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 2That'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
awkorsedyou 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 Serialand thecutare 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/cpuinfointo aQFileand 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
catexecutable, that's why you are getting the error message. You have to split it up: taking my rewrite above, oneQProcessfor 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/shor/bin/bashto handle the commands & the piping for you.
/bin/sh -c "grep Serial /proc/cpuinfo | cut -d ' ' -f 2"Now you use just one
QProcessfor/bin/sh, with arg #1 being-cand arg #2 beinggrep Serial /proc/cpuinfo | cut -d ' ' -f 2. Marginally less efficient, but saves writing multipleQProcess. -