QProcess probably reads command incorrectly
-
Hi, I'm having problems with using QProcess. I'm trying to send a command with special characters, so I modified the command so I'm able to put it in QProcess. When I run the command in cmd, it works. I verified the command is the same by taking the command from QProcess and putting it into a file. I ran the command that was put in the file and it worked. So I'm 100% sure the commands are the same, but QProcess for some reason doesn't want to work with it.
Here's what I'm doing (this is running in a foreach loop):
entry.replace(" ", "\\ "); QString command = "[ -e \"/sdcard/" + entry + "\" ] && echo yes || echo no"; qDebug() << "command running for:" << entry; //entry is "File woth... spaces.txt" qDebug() << "current command:" << command; QProcess check; check.start("adb", QStringList() << "-s" << dev << "shell" << command); check.waitForFinished(INT_MAX); qDebug() << "result:" << check.readAll(); //prints "no", should print "yes"To put the current command in a file to test it's the same, here's what I did:
QFile a("C:/Users/.../Desktop/out.txt"); a.open(QIODevice::ReadWrite); a.resize(0); QStringList verify; a.write("qprocess command: adb " + verify.join(" ").toUtf8()); a.close();If I run the command in that file, cmd prints yes.
How can I fix this?
-
Hi, I'm having problems with using QProcess. I'm trying to send a command with special characters, so I modified the command so I'm able to put it in QProcess. When I run the command in cmd, it works. I verified the command is the same by taking the command from QProcess and putting it into a file. I ran the command that was put in the file and it worked. So I'm 100% sure the commands are the same, but QProcess for some reason doesn't want to work with it.
Here's what I'm doing (this is running in a foreach loop):
entry.replace(" ", "\\ "); QString command = "[ -e \"/sdcard/" + entry + "\" ] && echo yes || echo no"; qDebug() << "command running for:" << entry; //entry is "File woth... spaces.txt" qDebug() << "current command:" << command; QProcess check; check.start("adb", QStringList() << "-s" << dev << "shell" << command); check.waitForFinished(INT_MAX); qDebug() << "result:" << check.readAll(); //prints "no", should print "yes"To put the current command in a file to test it's the same, here's what I did:
QFile a("C:/Users/.../Desktop/out.txt"); a.open(QIODevice::ReadWrite); a.resize(0); QStringList verify; a.write("qprocess command: adb " + verify.join(" ").toUtf8()); a.close();If I run the command in that file, cmd prints yes.
How can I fix this?
@Sucharek
You have a command line, with symbols like&&and||. Only the shell/command interpreter can handle those. That means the command to run must be Windowscmd. You would have to try something likecheck.start("cmd", QStringList() << "/c" << "adb -e \"...\" ... && echo yes || echo no");If you don't need the
&& ...stuff, you might be able to invokeadbdirectly with something likecheck.start("adb", QStringList() << "-e" << "/sdcard/" + entry << ...;Note in the latter case you (probably) do not put embedded
\"s in the string. -
@Sucharek
You have a command line, with symbols like&&and||. Only the shell/command interpreter can handle those. That means the command to run must be Windowscmd. You would have to try something likecheck.start("cmd", QStringList() << "/c" << "adb -e \"...\" ... && echo yes || echo no");If you don't need the
&& ...stuff, you might be able to invokeadbdirectly with something likecheck.start("adb", QStringList() << "-e" << "/sdcard/" + entry << ...;Note in the latter case you (probably) do not put embedded
\"s in the string. -
Hi @JonB, the
&&and||are part of the adb shell command.
I tried usingcmd /C adb...instead of justadb..., but it doesn't work.Removing
\"actually worked! Thanks!@Sucharek said in QProcess probably reads command incorrectly:
Hi @JonB, the && and || are part of the adb shell command.
Ah, I thought they were cmd shell commands (like in Linux)!
Then yes to removing the quotes. If your command like is like
adb ... "def ghi" ...then the shell/cmd where you type deals with those quotes to make a single argument. For
QProcessyou pass the desired string as a single argument but without any embedded quotes, like:QStringList() << ... << "abc def" << ...; -
@Sucharek said in QProcess probably reads command incorrectly:
Hi @JonB, the && and || are part of the adb shell command.
Ah, I thought they were cmd shell commands (like in Linux)!
Then yes to removing the quotes. If your command like is like
adb ... "def ghi" ...then the shell/cmd where you type deals with those quotes to make a single argument. For
QProcessyou pass the desired string as a single argument but without any embedded quotes, like:QStringList() << ... << "abc def" << ...;@JonB said in QProcess probably reads command incorrectly:
Ah, I thought they were cmd shell commands (like in Linux)!
Actually they still are. The adb shell command opens a remote bash shell on an android device.
-
@JonB said in QProcess probably reads command incorrectly:
Ah, I thought they were cmd shell commands (like in Linux)!
Actually they still are. The adb shell command opens a remote bash shell on an android device.
@Kent-Dorfman
LOL :)BTW, I developed products using
adbin the late 80s under UNIX beforegdbwas available. It was "painful"/"interesting" :)