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. Run a command with different result between in QProcess and in terminal
Forum Updated to NodeBB v4.3 + New Features

Run a command with different result between in QProcess and in terminal

Scheduled Pinned Locked Moved Solved General and Desktop
12 Posts 4 Posters 688 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.
  • MihanM Mihan

    Hi
    I find some special commands run in QProcess will get different result (different from in terminal ).
    e.g. I use mplayer -af volume=-20 xxx.wav > /dev/null .
    In terminal it just play the audio with no debug.
    But in QProcess it will run like mplayer -af volume=-20 xxx.wav , mplayer -af volume=-20 > ,mplayer -af volume=-20 /dev/null.

    e.g. sed -i \'s/iface eth0 inet static/iface eth0 inet dhcp/g\' /etc/network/interfaces
    in terminal it works well.
    in QProcess, it returns:
    sed: -e expression #1, char 1: unknown command: `''

    What should I pay attention to?

    Regards
    Mihan

    jsulmJ Offline
    jsulmJ Offline
    jsulm
    Lifetime Qt Champion
    wrote on last edited by
    #2

    @Mihan said in Run a command with different result between in QProcess and in terminal:

    But in QProcess it will run like mplayer -af volume=-20 xxx.wav , mplayer -af volume=-20 > ,mplayer -af volume=-20 /dev/null

    Please show how you're doing it in code.

    "What should I pay attention to?" - back slash (\) needs to be escaped as \\

    https://forum.qt.io/topic/113070/qt-code-of-conduct

    MihanM 1 Reply Last reply
    1
    • jsulmJ jsulm

      @Mihan said in Run a command with different result between in QProcess and in terminal:

      But in QProcess it will run like mplayer -af volume=-20 xxx.wav , mplayer -af volume=-20 > ,mplayer -af volume=-20 /dev/null

      Please show how you're doing it in code.

      "What should I pay attention to?" - back slash (\) needs to be escaped as \\

      MihanM Offline
      MihanM Offline
      Mihan
      wrote on last edited by
      #3

      @jsulm
      Sure, Here is the code

      void PlayInMplayer(QString file, int volume)
      {
          QString order = QString("mplayer -af volume=%1 %2 > /dev/null")
                                 .arg(QString::number(volume))
                                 .arg(file);
          QProcess::execute(order);
      }
      
      JonBJ 1 Reply Last reply
      0
      • MihanM Mihan

        @jsulm
        Sure, Here is the code

        void PlayInMplayer(QString file, int volume)
        {
            QString order = QString("mplayer -af volume=%1 %2 > /dev/null")
                                   .arg(QString::number(volume))
                                   .arg(file);
            QProcess::execute(order);
        }
        
        JonBJ Online
        JonBJ Online
        JonB
        wrote on last edited by JonB
        #4

        @Mihan
        You cannot have something like a redirection symbol, >, in a command you try to execute directly. Only the shell interprets that. So to do the command as shown you must go e.g.:

        QProcess::execute("/bin/bash", QStringList() << "-c" << order);
        

        In the case of this particular command, you might be better doing the output redirection via QProcess::setStandardOutputFile() instead of passing > /dev/null.

        Similarly for your other question where quoting is involved. ' ... ' is dealt with by the shell. Either use bash -c on the command line, or pass your items as separate arguments (without quoting) in the QStringList arguments to execute().

        1 Reply Last reply
        4
        • MihanM Offline
          MihanM Offline
          Mihan
          wrote on last edited by
          #5

          @JonB said in Run a command with different result between in QProcess and in terminal:

          You cannot have something like a redirection symbol, >

          Thank you so much , That's what I need!

          JonBJ 1 Reply Last reply
          0
          • MihanM Mihan

            @JonB said in Run a command with different result between in QProcess and in terminal:

            You cannot have something like a redirection symbol, >

            Thank you so much , That's what I need!

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

            @Mihan
            The list of symbols interpreted by the Linux shell, which you therefore cannot pass directly to execute without going through bash -c or thinking through, includes but is not limited to:

            < > | & ; ' " $ * [ ( ~ # \ <space>
            

            So kind of most symbols!

            MihanM 1 Reply Last reply
            4
            • JonBJ JonB

              @Mihan
              The list of symbols interpreted by the Linux shell, which you therefore cannot pass directly to execute without going through bash -c or thinking through, includes but is not limited to:

              < > | & ; ' " $ * [ ( ~ # \ <space>
              

              So kind of most symbols!

              MihanM Offline
              MihanM Offline
              Mihan
              wrote on last edited by
              #7

              @JonB
              By the way, does system() interpret symbols by the linux shell? I used system() to process commands before.

              JonBJ 1 Reply Last reply
              0
              • MihanM Mihan

                @JonB
                By the way, does system() interpret symbols by the linux shell? I used system() to process commands before.

                JonBJ Online
                JonBJ Online
                JonB
                wrote on last edited by JonB
                #8

                @Mihan
                It's not that system() does any interpretation, it's that system(command) precisely does /bin/sh -c command, technically

                execl("/bin/sh", "sh", "-c", command, (char *) 0);
                

                Therefore to do the equivalent from QProcess you would go:

                QProcess::execute("/bin/sh", QStringList() << "-c" << command)
                
                MihanM 1 Reply Last reply
                4
                • JonBJ JonB

                  @Mihan
                  It's not that system() does any interpretation, it's that system(command) precisely does /bin/sh -c command, technically

                  execl("/bin/sh", "sh", "-c", command, (char *) 0);
                  

                  Therefore to do the equivalent from QProcess you would go:

                  QProcess::execute("/bin/sh", QStringList() << "-c" << command)
                  
                  MihanM Offline
                  MihanM Offline
                  Mihan
                  wrote on last edited by
                  #9

                  @JonB said in Run a command with different result between in QProcess and in terminal:

                  execl("/bin/sh", "sh", "-c", command, (char *) 0);

                  Yep, I also find the same in man system.

                  But I can't find the explaination about this in wiki, so I don't pay attention to it so that I make a mistake.
                  I think it should be added in wiki.

                  JonBJ Pablo J. RoginaP 2 Replies Last reply
                  0
                  • MihanM Mihan

                    @JonB said in Run a command with different result between in QProcess and in terminal:

                    execl("/bin/sh", "sh", "-c", command, (char *) 0);

                    Yep, I also find the same in man system.

                    But I can't find the explaination about this in wiki, so I don't pay attention to it so that I make a mistake.
                    I think it should be added in wiki.

                    JonBJ Online
                    JonBJ Online
                    JonB
                    wrote on last edited by
                    #10

                    @Mihan
                    In the Qt wiki? I'm afraid that's not its job. There are millions of OS-specific issues, it just covers Qt stuff and does not get involved in those.

                    MihanM 1 Reply Last reply
                    0
                    • JonBJ JonB

                      @Mihan
                      In the Qt wiki? I'm afraid that's not its job. There are millions of OS-specific issues, it just covers Qt stuff and does not get involved in those.

                      MihanM Offline
                      MihanM Offline
                      Mihan
                      wrote on last edited by
                      #11

                      @JonB Ok, so maybe I make a great topic for linux Qt's user :).

                      1 Reply Last reply
                      0
                      • MihanM Mihan

                        @JonB said in Run a command with different result between in QProcess and in terminal:

                        execl("/bin/sh", "sh", "-c", command, (char *) 0);

                        Yep, I also find the same in man system.

                        But I can't find the explaination about this in wiki, so I don't pay attention to it so that I make a mistake.
                        I think it should be added in wiki.

                        Pablo J. RoginaP Offline
                        Pablo J. RoginaP Offline
                        Pablo J. Rogina
                        wrote on last edited by
                        #12

                        @Mihan said in Run a command with different result between in QProcess and in terminal:

                        I think it should be added in wiki.

                        You're more than welcomed to do that!

                        Upvote the answer(s) that helped you solve the issue
                        Use "Topic Tools" button to mark your post as Solved
                        Add screenshots via postimage.org
                        Don't ask support requests via chat/PM. Please use the forum so others can benefit from the solution in the future

                        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