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. system vs process command
Forum Update on Monday, May 27th 2025

system vs process command

Scheduled Pinned Locked Moved Solved General and Desktop
15 Posts 4 Posters 3.4k Views
  • 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.
  • M Offline
    M Offline
    Mijaz
    wrote on 22 Oct 2019, 04:17 last edited by
    #1

    void MainWindow::on_read_push_clicked()
    {

    system("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");

    QProcess process;
    process.start("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");
    process.waitForFinished(-1); // will wait forever until finished
    QString stdout = process.readAllStandardOutput();
    qDebug() << stdout;

    ui->lineEdit->setText(stdout);
    }

    // output of above system ("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");
    1024

    // the output of process.start("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");
    ""

    Selection_018.png
    Although I have checked both command as follows they worked fine:

    system("sudo devmem2 0x80000000");
    QProcess process;
    process.start("sudo devmem2 0x80000000");
    process.waitForFinished(-1); // will wait forever until finished
    QString stdout = process.readAllStandardOutput();
    qDebug() << stdout

    S 1 Reply Last reply 22 Oct 2019, 06:25
    0
    • S sierdzio
      22 Oct 2019, 07:31

      Add CONFIG += c++11 to your .pro file.

      Or build the string list manually:

      QStringList temp;
      temp.append("-C");
      temp.append("\"cd /sys/bus/iio/devices/iio:device5; cat dwell_samples\"");
      

      I have not checked the code on my side, it may have some errors still.

      J Offline
      J Offline
      JonB
      wrote on 22 Oct 2019, 08:40 last edited by JonB
      #5

      @sierdzio , @Mijaz

      • In Linux/bash/sh, the (first) argument to the shell for executing a string is -c, not -C, and must be in lower-case.

      • In the second argument, the string of the command to execute, you must not embed literal double-quotes (the \"s shown above). Qt will end up protecting those, and you won't get a command which works. Since the whole point is of QProcess::start(program, arglist) overload is that it knows each argument is to be passed as a single argument to the program, you do not need or want to quote it yourself.

      • In this case you could make it easier on yourself by not passing a multi-statement command or needing to invoke the shell if you changed your command to the equivalent cat /sys/bus/iio/devices/iio:device5/dwell_samples.

      Of course, in the particular case of the example shown here, you don't want to use any OS/QProcess command at all. You are reading the output from a command which simply cats a file. That's a long way round from the much simpler and more efficient use of a QFile here in which you simply open & read the file's content...!

      M 1 Reply Last reply 22 Oct 2019, 08:52
      4
      • M Mijaz
        22 Oct 2019, 04:17

        void MainWindow::on_read_push_clicked()
        {

        system("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");

        QProcess process;
        process.start("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");
        process.waitForFinished(-1); // will wait forever until finished
        QString stdout = process.readAllStandardOutput();
        qDebug() << stdout;

        ui->lineEdit->setText(stdout);
        }

        // output of above system ("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");
        1024

        // the output of process.start("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");
        ""

        Selection_018.png
        Although I have checked both command as follows they worked fine:

        system("sudo devmem2 0x80000000");
        QProcess process;
        process.start("sudo devmem2 0x80000000");
        process.waitForFinished(-1); // will wait forever until finished
        QString stdout = process.readAllStandardOutput();
        qDebug() << stdout

        S Offline
        S Offline
        sierdzio
        Moderators
        wrote on 22 Oct 2019, 06:25 last edited by
        #2

        @Mijaz said in system vs process command:

        process.start("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");

        QProcess does not start any shell, it only attempts to start a process with given names and parameters. But you give here 2 separate calls and cram them together. Either run them separately or launch a shell like this:

        process.start("bash", QStringList { "-C", "\"cd /sys/bus/iio/devices/iio:device5; cat dwell_samples\"" });
        
        

        (Z(:^

        M 2 Replies Last reply 22 Oct 2019, 07:28
        1
        • S sierdzio
          22 Oct 2019, 06:25

          @Mijaz said in system vs process command:

          process.start("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");

          QProcess does not start any shell, it only attempts to start a process with given names and parameters. But you give here 2 separate calls and cram them together. Either run them separately or launch a shell like this:

          process.start("bash", QStringList { "-C", "\"cd /sys/bus/iio/devices/iio:device5; cat dwell_samples\"" });
          
          
          M Offline
          M Offline
          Mijaz
          wrote on 22 Oct 2019, 07:28 last edited by
          #3

          @sierdzio
          I have executed your suggested command but there are error. I have added #include <QStringList>
          Selection_019.png

          1 Reply Last reply
          0
          • S Offline
            S Offline
            sierdzio
            Moderators
            wrote on 22 Oct 2019, 07:31 last edited by
            #4

            Add CONFIG += c++11 to your .pro file.

            Or build the string list manually:

            QStringList temp;
            temp.append("-C");
            temp.append("\"cd /sys/bus/iio/devices/iio:device5; cat dwell_samples\"");
            

            I have not checked the code on my side, it may have some errors still.

            (Z(:^

            J 1 Reply Last reply 22 Oct 2019, 08:40
            1
            • S sierdzio
              22 Oct 2019, 07:31

              Add CONFIG += c++11 to your .pro file.

              Or build the string list manually:

              QStringList temp;
              temp.append("-C");
              temp.append("\"cd /sys/bus/iio/devices/iio:device5; cat dwell_samples\"");
              

              I have not checked the code on my side, it may have some errors still.

              J Offline
              J Offline
              JonB
              wrote on 22 Oct 2019, 08:40 last edited by JonB
              #5

              @sierdzio , @Mijaz

              • In Linux/bash/sh, the (first) argument to the shell for executing a string is -c, not -C, and must be in lower-case.

              • In the second argument, the string of the command to execute, you must not embed literal double-quotes (the \"s shown above). Qt will end up protecting those, and you won't get a command which works. Since the whole point is of QProcess::start(program, arglist) overload is that it knows each argument is to be passed as a single argument to the program, you do not need or want to quote it yourself.

              • In this case you could make it easier on yourself by not passing a multi-statement command or needing to invoke the shell if you changed your command to the equivalent cat /sys/bus/iio/devices/iio:device5/dwell_samples.

              Of course, in the particular case of the example shown here, you don't want to use any OS/QProcess command at all. You are reading the output from a command which simply cats a file. That's a long way round from the much simpler and more efficient use of a QFile here in which you simply open & read the file's content...!

              M 1 Reply Last reply 22 Oct 2019, 08:52
              4
              • S sierdzio
                22 Oct 2019, 06:25

                @Mijaz said in system vs process command:

                process.start("cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");

                QProcess does not start any shell, it only attempts to start a process with given names and parameters. But you give here 2 separate calls and cram them together. Either run them separately or launch a shell like this:

                process.start("bash", QStringList { "-C", "\"cd /sys/bus/iio/devices/iio:device5; cat dwell_samples\"" });
                
                
                M Offline
                M Offline
                Mijaz
                wrote on 22 Oct 2019, 08:45 last edited by
                #6

                @sierdzio
                I also tryed like this:
                Selection_020.png

                J 1 Reply Last reply 22 Oct 2019, 08:47
                0
                • M Mijaz
                  22 Oct 2019, 08:45

                  @sierdzio
                  I also tryed like this:
                  Selection_020.png

                  J Offline
                  J Offline
                  JonB
                  wrote on 22 Oct 2019, 08:47 last edited by JonB
                  #7

                  @Mijaz
                  Don't, you're all over the place. Your post may have crossed with mine above. Read what I have written and act on it.

                  You can make your current command (if that's what you want) work via:

                  process.start("/bin/bash", QStringList() << "-c", << "cd /sys/bus/iio/devices/iio:device5; cat dwell_samples");
                  

                  But as I said I wouldn't do that command that way.

                  1 Reply Last reply
                  1
                  • J JonB
                    22 Oct 2019, 08:40

                    @sierdzio , @Mijaz

                    • In Linux/bash/sh, the (first) argument to the shell for executing a string is -c, not -C, and must be in lower-case.

                    • In the second argument, the string of the command to execute, you must not embed literal double-quotes (the \"s shown above). Qt will end up protecting those, and you won't get a command which works. Since the whole point is of QProcess::start(program, arglist) overload is that it knows each argument is to be passed as a single argument to the program, you do not need or want to quote it yourself.

                    • In this case you could make it easier on yourself by not passing a multi-statement command or needing to invoke the shell if you changed your command to the equivalent cat /sys/bus/iio/devices/iio:device5/dwell_samples.

                    Of course, in the particular case of the example shown here, you don't want to use any OS/QProcess command at all. You are reading the output from a command which simply cats a file. That's a long way round from the much simpler and more efficient use of a QFile here in which you simply open & read the file's content...!

                    M Offline
                    M Offline
                    Mijaz
                    wrote on 22 Oct 2019, 08:52 last edited by
                    #8

                    @JonB
                    thank you soo much.
                    process.start("cat /sys/bus/iio/devices/iio:device5/dwell_samples");
                    this worked greatly.

                    J 1 Reply Last reply 22 Oct 2019, 08:55
                    0
                    • M Mijaz
                      22 Oct 2019, 08:52

                      @JonB
                      thank you soo much.
                      process.start("cat /sys/bus/iio/devices/iio:device5/dwell_samples");
                      this worked greatly.

                      J Offline
                      J Offline
                      JonB
                      wrote on 22 Oct 2019, 08:55 last edited by JonB
                      #9

                      @Mijaz
                      Yep, as you can see removing the need to cd makes it simpler, no shell.

                      Though as I wrote above, if all you want to do is cat a file, you really don't want to spawn a sub-process and read its output, you can accomplish that directly from QFile without any QProcess:

                      QFile f("/sys/bus/iio/devices/iio:device5/dwell_samples");
                      if (f.open(QIODevice::ReadOnly | QIODevice::Text))
                      {
                          QByteArray ba = f.readAll();
                          ui->lineEdit->setText(QString(ba));
                          f.close();
                      }
                      

                      Of course, that may not apply to other examples, e.g. the sudo devmem2 you mentioned.

                      M 1 Reply Last reply 22 Oct 2019, 12:07
                      2
                      • J JonB
                        22 Oct 2019, 08:55

                        @Mijaz
                        Yep, as you can see removing the need to cd makes it simpler, no shell.

                        Though as I wrote above, if all you want to do is cat a file, you really don't want to spawn a sub-process and read its output, you can accomplish that directly from QFile without any QProcess:

                        QFile f("/sys/bus/iio/devices/iio:device5/dwell_samples");
                        if (f.open(QIODevice::ReadOnly | QIODevice::Text))
                        {
                            QByteArray ba = f.readAll();
                            ui->lineEdit->setText(QString(ba));
                            f.close();
                        }
                        

                        Of course, that may not apply to other examples, e.g. the sudo devmem2 you mentioned.

                        M Offline
                        M Offline
                        Mijaz
                        wrote on 22 Oct 2019, 12:07 last edited by
                        #10

                        @JonB
                        process.start("cat /sys/bus/iio/devices/iio:device5/dwell_samples");

                        I need to access kernel space, so id needs permission. How can I modify command to give permission (sudo su).

                        The following command needs permission.
                        I want to execute two process

                        QProcess process1;
                        process1.start("echo /sys/kernel/iio/devices/iio:device4/ 0x418 0x2 > direct_reg_access");
                        process1.waitForFinished(-1); // will wait forever until finished
                        QString stdout1 = process1.readAllStandardOutput();
                        qDebug() << stdout1;

                        QProcess process2;
                        process2.start("cat /sys/kernel/iio/devices/iio:device4/ direct_reg_access");
                        process2.waitForFinished(-1); // will wait forever until finished
                        QString stdout2 = process2.readAllStandardOutput();
                        qDebug() << stdout2;

                        J 1 Reply Last reply 22 Oct 2019, 20:44
                        0
                        • M Mijaz
                          22 Oct 2019, 12:07

                          @JonB
                          process.start("cat /sys/bus/iio/devices/iio:device5/dwell_samples");

                          I need to access kernel space, so id needs permission. How can I modify command to give permission (sudo su).

                          The following command needs permission.
                          I want to execute two process

                          QProcess process1;
                          process1.start("echo /sys/kernel/iio/devices/iio:device4/ 0x418 0x2 > direct_reg_access");
                          process1.waitForFinished(-1); // will wait forever until finished
                          QString stdout1 = process1.readAllStandardOutput();
                          qDebug() << stdout1;

                          QProcess process2;
                          process2.start("cat /sys/kernel/iio/devices/iio:device4/ direct_reg_access");
                          process2.waitForFinished(-1); // will wait forever until finished
                          QString stdout2 = process2.readAllStandardOutput();
                          qDebug() << stdout2;

                          J Offline
                          J Offline
                          JonB
                          wrote on 22 Oct 2019, 20:44 last edited by
                          #11

                          @Mijaz
                          This is rather a different question. Of the commands as you have written, #1 requires write permission to wherever the file direct_reg_access is (current directory). That's all, and I don't see anything special about that. It's not clear whether there is a space in the path in command #2, and that makes a difference as to what it's doing. I kind of suspect the commands as written do not do what you might be intending, I don't know. There's not much point in saying more till you clarify exactly what you are trying to do here. And if you expect people to understand you must either put spaces in or take them out correctly, else it's all guesswork for the reader.

                          M 1 Reply Last reply 23 Oct 2019, 04:11
                          0
                          • J JonB
                            22 Oct 2019, 20:44

                            @Mijaz
                            This is rather a different question. Of the commands as you have written, #1 requires write permission to wherever the file direct_reg_access is (current directory). That's all, and I don't see anything special about that. It's not clear whether there is a space in the path in command #2, and that makes a difference as to what it's doing. I kind of suspect the commands as written do not do what you might be intending, I don't know. There's not much point in saying more till you clarify exactly what you are trying to do here. And if you expect people to understand you must either put spaces in or take them out correctly, else it's all guesswork for the reader.

                            M Offline
                            M Offline
                            Mijaz
                            wrote on 23 Oct 2019, 04:11 last edited by
                            #12

                            Hi @JonB How are you?

                            QProcess process1;
                            process1.start("echo /sys/kernel/iio/devices/iio:device4/ 0x418 0x2 > direct_reg_access");
                            process1.waitForFinished(-1); // will wait forever until finished
                            QString stdout1 = process1.readAllStandardOutput();
                            qDebug() << stdout1;

                            Actually I want to display the output of different processes, so of course, I need a process with different names. However, right now I need to implement just one process. Do you have Idea how to give permission in-process command? As I mentioned to access direct_reg_access.

                            aha_1980A J 2 Replies Last reply 23 Oct 2019, 06:49
                            0
                            • M Mijaz
                              23 Oct 2019, 04:11

                              Hi @JonB How are you?

                              QProcess process1;
                              process1.start("echo /sys/kernel/iio/devices/iio:device4/ 0x418 0x2 > direct_reg_access");
                              process1.waitForFinished(-1); // will wait forever until finished
                              QString stdout1 = process1.readAllStandardOutput();
                              qDebug() << stdout1;

                              Actually I want to display the output of different processes, so of course, I need a process with different names. However, right now I need to implement just one process. Do you have Idea how to give permission in-process command? As I mentioned to access direct_reg_access.

                              aha_1980A Offline
                              aha_1980A Offline
                              aha_1980
                              Lifetime Qt Champion
                              wrote on 23 Oct 2019, 06:49 last edited by
                              #13

                              Hi @Mijaz,

                              isn't dwell_samples a file? So why bother with QProcess, bash and cat if you can simply open the file with QFile and read it?

                              Regards

                              Qt has to stay free or it will die.

                              J 1 Reply Last reply 23 Oct 2019, 07:01
                              0
                              • aha_1980A aha_1980
                                23 Oct 2019, 06:49

                                Hi @Mijaz,

                                isn't dwell_samples a file? So why bother with QProcess, bash and cat if you can simply open the file with QFile and read it?

                                Regards

                                J Offline
                                J Offline
                                JonB
                                wrote on 23 Oct 2019, 07:01 last edited by JonB
                                #14

                                @aha_1980
                                As you can see, I have already said this twice in responses above and even given the code.... To be fair, in his latest post the OP is no longer asking about Question #2, which is the one which can be done by simple QFile::read().

                                1 Reply Last reply
                                1
                                • M Mijaz
                                  23 Oct 2019, 04:11

                                  Hi @JonB How are you?

                                  QProcess process1;
                                  process1.start("echo /sys/kernel/iio/devices/iio:device4/ 0x418 0x2 > direct_reg_access");
                                  process1.waitForFinished(-1); // will wait forever until finished
                                  QString stdout1 = process1.readAllStandardOutput();
                                  qDebug() << stdout1;

                                  Actually I want to display the output of different processes, so of course, I need a process with different names. However, right now I need to implement just one process. Do you have Idea how to give permission in-process command? As I mentioned to access direct_reg_access.

                                  J Offline
                                  J Offline
                                  JonB
                                  wrote on 23 Oct 2019, 07:13 last edited by JonB
                                  #15

                                  @Mijaz
                                  Here is your command, copied & pasted into triple-backticks here on this forum so that we can actually read it:

                                  process1.start("echo /sys/kernel/iio/devices/iio:device4/ 0x418 0x2 > direct_reg_access");
                                  

                                  So, this will put the characters

                                  /sys/kernel/iio/devices/iio:device4/ 0x418 0x2
                                  

                                  into a file named direct_reg_access.

                                  Now, as I have said before, this will create/overwrite that file in the current directory of whichever process it is run from. Why do you need any special permissions?

                                  Please log onto your Linux or whatever box, open a shell/terminal, and copy & paste the command you have written into it as the very first thing you do in the shell:

                                  echo /sys/kernel/iio/devices/iio:device4/ 0x418 0x2 > direct_reg_access
                                  

                                  Do you agree this works immediately, without any special permissions? Because your current directory is highly likely to be your home directory, and you have permission to create/write to files there. So, you don't need any special permissions, do you?

                                  Now, if it turns out that what you really mean is that you want to write to that file elsewhere, then you have to say so. I don't want to have to guess. Perhaps what you really mean is

                                  echo /sys/kernel/iio/devices/iio:device4/ 0x418 0x2 > /sys/kernel/iio/devices/iio:device4/direct_reg_access
                                  

                                  While we are here. Your question #2 , copied & pasted, says what you intend to do is:

                                  cat /sys/kernel/iio/devices/iio:device4/ direct_reg_access
                                  

                                  This will always error, because you cannot cat a directory. Try it yourself from a shell. I asked you before to look at your spacing and correct or tell us what you actually want to do.

                                  Finally, the whole process you have written is pointless. At the end of it, if you cat the direct_reg_access file you have created you will always get back precisely the string

                                  /sys/kernel/iio/devices/iio:device4/ 0x418 0x2
                                  

                                  so what is the whole point of the exercise?

                                  I will answer your question about permissions if you take the time to make clear what it is you actually want to do, where that file direct_reg_access is supposed to be, but not before....

                                  1 Reply Last reply
                                  2

                                  1/15

                                  22 Oct 2019, 04:17

                                  • Login

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