How to use QtProcess [solved]
-
Hi,
Here is my batch file :
@#!/bin/sh
var=$(dmesg | grep -1 hda)
if [ ! "x$var" = "x" ]; then
exc=$(/sbin/udevadm info --query=property --name=hda | grep ID_SERIAL_SHORT | cut -d = -f 2)
echo $exc
else
var=$(dmesg | grep -1 sda)
if [ ! "x$var" = "x" ]; then
exc=$(/sbin/udevadm info --query=property --name=sda | grep ID_SERIAL_SHORT | cut -d = -f 2)
echo $exc
else
echo ""
fi
fi@
and my Qt code :
@ QProcess *batch= new QProcess;
QString name;
QStringList arg;
#if defined(Q_OS_LINUX)
name = "/bin/bash";
arg << "-c" ;
QFile file(":/scripts/hdserial.sh");
#endif
[...]
if (file.open(QIODevice::ReadOnly | QIODevice::Text)) {
arg << file.readAll();
batch->start(name, arg);
batch->waitForFinished(-1);
QString batch_stdout = batch->readAllStandardOutput();
QString batch_stderr = batch->readAllStandardError();
[...]
}@It works fine...
I was wondering whether it would be possible to write directly lines of the batch file in arg ?
For example :
@arg << "-c"
<< "var=$(dmesg | grep -1 hda)"
<< "if [ ! "x$var"= "x" ]; then"
<< "exc=$(/sbin/udevadm info --query=property --name=hda | grep ID_SERIAL_SHORT | cut -d = -f 2)"
<< "echo $exc"
<< "else"
<< [...]@Thanks. Gilles
-
This will create a lot of arguments for the process. You can try something like
@ arg << "-c"
"var=$(dmesg | grep -1 hda)"
"if [ ! "x$var"= "x" ]; then"
"exc=$(/sbin/udevadm info --query=property --name=hda | grep ID_SERIAL_SHORT | cut -d = -f 2)"
"echo $exc"
"else"
[...]
@ -
Unfortunately not. Error reported (readAllStandardError())
/bin/bash: - : option non valable
Utilisation : /bin/bash [option longue GNU] [option] ...
/bin/bash [option longue GNU] [option] fichier-script ...
Options longues GNU :
--debug
--debugger
--dump-po-strings
--dump-strings
--help
--init-file
--login
--noediting
--noprofile
--norc
--posix
--rcfile
--restricted
--verbose
--version
Options du shell :
-ilrsD or -c command or -O shopt_option (invocation only)
-abefhkmnptuvxBCHP ou -o optionWhat does it mean ?
-
Sorry, I deleted on << too much:
@arg << "-c"
<< "var=$(dmesg | grep -1 hda)"
"if [ ! "x$var"= "x" ]; then"
"exc=$(/sbin/udevadm info --query=property --name=hda | grep ID_SERIAL_SHORT | cut -d = -f 2)"
"echo $exc"
"else"
[...]@Probably you might also have to add " before the whole string and after but I am actually not sure how bash interprets the string. You should also see the man page for bash to be sure about the syntax. From Qt's side there should not be an issue at all.
-
Thank you very much, butterface, for your answer. This gave me an appropriate solution:
@ QProcess *batch= new QProcess;
QString name = "/bin/bash";
QStringList arg;
arg << "-c";
arg << "var=$(dmesg | grep -1 hda) \n"
"if [ ! "x$var" = "x" ]; then \n"
"exc=$(/sbin/udevadm info --query=property --name=hda | grep ID_SERIAL_SHORT | cut -d = -f 2) \n"
"echo $exc \n"
"else \n"
"var=$(dmesg | grep -1 sda) \n"
"if [ ! "x$var" = "x" ]; then \n"
"exc=$(/sbin/udevadm info --query=property --name=sda | grep ID_SERIAL_SHORT | cut -d = -f 2) \n"
"echo $exc \n"
"else \n"
"echo "" \n"
"fi \n"
"fi";
batch->start(name, arg );
batch->waitForFinished(-1);
QString batch_stdout = batch->readAllStandardOutput();
QString batch_stderr = batch->readAllStandardError();
[...]
@