Reputation: 751
I'm trying to read the output of a shell script in Qt. However, passing an argument to the shell script doesn't work, as it is ignored completely. What am I doing wrong in the following excerpt?
QProcess *process = new QProcess;
process->start("sh", QStringList() << "-c" << "\"xdotool getactivewindow\"");
process->waitForFinished();
QString output = process->readAllStandardOutput();
target = output.toUInt();
I have looked at several other threads and tried solutions such as
process->start("sh", QStringList() << "-c" << "xdotool getactivewindow");
and
process->start("sh", QStringList() << "-c" << "xdotool" << "getactivewindow");
but none worked.
Upvotes: 1
Views: 2023
Reputation: 11064
I would expect that your second method should work.
I tested it with the following script (test.sh
):
#!/bin/bash
echo "First arg: $1"
echo "Second arg: $2"
And I called the script using QProcess
in following ways:
QProcess *process = new QProcess;
process->start("./test.sh", QStringList() << "abc" << "xyz");
process->waitForFinished();
qDebug () << process->readAllStandardOutput();
// returns: "First arg: abc\nSecond arg: xyz\n" => OK
process->start("sh", QStringList() << "-c" << "./test.sh abc xyz");
process->waitForFinished();
qDebug () << process->readAllStandardOutput();
// returns: "First arg: abc\nSecond arg: xyz\n" => OK
process->start("sh", QStringList() << "-c" << "./test.sh" << "abc xyz");
process->waitForFinished();
qDebug () << process->readAllStandardOutput();
// returns: "First arg: \nSecond arg: \n" => WRONG
Explanation
process->start("sh", QStringList() << "-c" << "\"xdotool getactivewindow\"");
: it is not needed (nor allowed) to quote the arguments yourself. The documentation is not that clear, but it states:Note: No further splitting of the arguments is performed.
process->start("sh", QStringList() << "-c" << "xdotool getactivewindow");
: this should work
process->start("sh", QStringList() << "-c" << "xdotool" << "getactivewindow");
: getactivewindow
is passed as an argument to sh
instead of xdotool
Upvotes: 1