Trouble with QProcess and getting back standard output
Hi,
I have a script that returns a number that is the percentage of partition used, it looks like
Code:
#!/bin/bash
case $1 in
root_partition)
devname="/dev/sda1"
let p=`df -k $devname | grep -v ^File | awk '{printf ("%i",$3*100 / $2); }'`
echo $p
;;
default)
echo "Unknown command"
;;
esac
exit 0
I have a QProcess that looks like this
Code:
arguments.clear();
qDebug("proc running");
arguments << " root_partition ";
if (_proc
->state
() == QProcess::NotRunning) {
_proc->disconnect();
connect(_proc, SIGNAL(readyReadStandardOutput()), this, SLOT(outLog()));
_proc->start(program, arguments);
_proc
->setReadChannel
(QProcess::StandardOutput);
_proc->waitForFinished();
}
}
void MyClass::outLog()
{
qDebug("outLog");
QByteArray array
= _proc
->readAllStandardOutput
();
}
My problem is that running the script by hand returns 87 - the % used on that partition. But running the script in my QT code does not work. If I replace the line
with
I get "root partition" returned - as expected. But why cannot I not anything back from running the script in Qt? The outLog function never gets called? Is echo not standard output?
I have done quite a bit of Googling but other people working solutions are not working for me? :(
Thank you very much for helping.
cheers,
Tara
Qt4 on Linux
Re: Trouble with QProcess and getting back standard output
You are using a relative path to the script. Ensure the current working directory of your Qt program is where you think it is, that the script is there, and that the current working directory is in PATH. You could use a fully qualified path to the script or ensure the script is on the system PATH (like echo) which quite often does not include the current working directory.
Also check what QProcess::error() returns.
Re: Trouble with QProcess and getting back standard output
Thanks!
Changed
to
Code:
QString program
= QDir().
absoluteFilePath("script.sh");
and all working now!