Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QProcess probably reads command incorrectly
Qt 6.11 is out! See what's new in the release blog

QProcess probably reads command incorrectly

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 3 Posters 1.1k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sucharek
    wrote on last edited by
    #1

    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?

    JonBJ 1 Reply Last reply
    0
    • S Sucharek

      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?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

      @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 Windows cmd. You would have to try something like

      check.start("cmd", QStringList() << "/c" << "adb -e \"...\" ... && echo yes || echo no");
      

      If you don't need the && ... stuff, you might be able to invoke adb directly with something like

      check.start("adb", QStringList() << "-e" << "/sdcard/" + entry << ...;
      

      Note in the latter case you (probably) do not put embedded \"s in the string.

      S 1 Reply Last reply
      3
      • JonBJ JonB

        @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 Windows cmd. You would have to try something like

        check.start("cmd", QStringList() << "/c" << "adb -e \"...\" ... && echo yes || echo no");
        

        If you don't need the && ... stuff, you might be able to invoke adb directly with something like

        check.start("adb", QStringList() << "-e" << "/sdcard/" + entry << ...;
        

        Note in the latter case you (probably) do not put embedded \"s in the string.

        S Offline
        S Offline
        Sucharek
        wrote on last edited by
        #3

        Hi @JonB, the && and || are part of the adb shell command.
        I tried using cmd /C adb... instead of just adb..., but it doesn't work.

        Removing \" actually worked! Thanks!

        JonBJ 1 Reply Last reply
        0
        • S Sucharek

          Hi @JonB, the && and || are part of the adb shell command.
          I tried using cmd /C adb... instead of just adb..., but it doesn't work.

          Removing \" actually worked! Thanks!

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @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 QProcess you pass the desired string as a single argument but without any embedded quotes, like:

          QStringList() << ... << "abc def" << ...;
          
          Kent-DorfmanK 1 Reply Last reply
          1
          • JonBJ JonB

            @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 QProcess you pass the desired string as a single argument but without any embedded quotes, like:

            QStringList() << ... << "abc def" << ...;
            
            Kent-DorfmanK Offline
            Kent-DorfmanK Offline
            Kent-Dorfman
            wrote on last edited by
            #5

            @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.

            The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

            JonBJ 1 Reply Last reply
            0
            • Kent-DorfmanK Kent-Dorfman

              @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.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @Kent-Dorfman
              LOL :)

              BTW, I developed products using adb in the late 80s under UNIX before gdb was available. It was "painful"/"interesting" :)

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved