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 686 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.
  • J jsulm
    11 May 2020, 06:37

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

    M Offline
    M Offline
    Mihan
    wrote on 11 May 2020, 06:46 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);
    }
    
    J 1 Reply Last reply 11 May 2020, 07:11
    0
    • M Mihan
      11 May 2020, 06:46

      @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);
      }
      
      J Offline
      J Offline
      JonB
      wrote on 11 May 2020, 07:11 last edited by JonB 5 Nov 2020, 07:17
      #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
      • M Offline
        M Offline
        Mihan
        wrote on 11 May 2020, 07:19 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!

        J 1 Reply Last reply 11 May 2020, 07:25
        0
        • M Mihan
          11 May 2020, 07:19

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

          J Offline
          J Offline
          JonB
          wrote on 11 May 2020, 07:25 last edited by JonB 5 Nov 2020, 07:55
          #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!

          M 1 Reply Last reply 11 May 2020, 07:41
          4
          • J JonB
            11 May 2020, 07:25

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

            M Offline
            M Offline
            Mihan
            wrote on 11 May 2020, 07:41 last edited by
            #7

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

            J 1 Reply Last reply 11 May 2020, 07:45
            0
            • M Mihan
              11 May 2020, 07:41

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

              J Offline
              J Offline
              JonB
              wrote on 11 May 2020, 07:45 last edited by JonB 5 Nov 2020, 07:46
              #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)
              
              M 1 Reply Last reply 11 May 2020, 07:59
              4
              • J JonB
                11 May 2020, 07:45

                @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)
                
                M Offline
                M Offline
                Mihan
                wrote on 11 May 2020, 07:59 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.

                J P 2 Replies Last reply 11 May 2020, 08:03
                0
                • M Mihan
                  11 May 2020, 07:59

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

                  J Offline
                  J Offline
                  JonB
                  wrote on 11 May 2020, 08:03 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.

                  M 1 Reply Last reply 11 May 2020, 08:09
                  0
                  • J JonB
                    11 May 2020, 08:03

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

                    M Offline
                    M Offline
                    Mihan
                    wrote on 11 May 2020, 08:09 last edited by
                    #11

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

                    1 Reply Last reply
                    0
                    • M Mihan
                      11 May 2020, 07:59

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

                      P Offline
                      P Offline
                      Pablo J. Rogina
                      wrote on 12 May 2020, 14:35 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

                      12/12

                      12 May 2020, 14:35

                      • Login

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