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. why QProcess not show output of ls /dev/sd* command ?
Forum Updated to NodeBB v4.3 + New Features

why QProcess not show output of ls /dev/sd* command ?

Scheduled Pinned Locked Moved Solved General and Desktop
18 Posts 4 Posters 1.1k 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.
  • Q Offline
    Q Offline
    Qt embedded developer
    wrote on 14 Dec 2022, 11:45 last edited by
    #1

    i have implemted below code to find usb device name to print but its not working .

    Actually this command working when execute on linux terminal.

     Command = "ls";
    
    args<<" /dev/sd*"
    
        OProcess.start(Command,args,QIODevice::ReadOnly); //Starts execution of command
        OProcess.waitForFinished();                       //Waits for execution to complete
    
        QString sFromDateTime = OProcess.readAllStandardOutput();  //Reads standard output
        QString StdError = OProcess.readAllStandardError();
    
        if(StdError.isEmpty())
        {
    
            qDebug() << "output : " << sFromDateTime;
    
                QStringList List;
                   List = sFromDateTime.split("\n");
                   List = List.filter("/dev");
                   foreach(QString item, List)
                          qDebug() << "List items = " << item;
        }
        else
        {
            qDebug() << "StdError:" << StdError;
        }
    
    
    J J 2 Replies Last reply 14 Dec 2022, 11:48
    0
    • Q Qt embedded developer
      14 Dec 2022, 11:45

      i have implemted below code to find usb device name to print but its not working .

      Actually this command working when execute on linux terminal.

       Command = "ls";
      
      args<<" /dev/sd*"
      
          OProcess.start(Command,args,QIODevice::ReadOnly); //Starts execution of command
          OProcess.waitForFinished();                       //Waits for execution to complete
      
          QString sFromDateTime = OProcess.readAllStandardOutput();  //Reads standard output
          QString StdError = OProcess.readAllStandardError();
      
          if(StdError.isEmpty())
          {
      
              qDebug() << "output : " << sFromDateTime;
      
                  QStringList List;
                     List = sFromDateTime.split("\n");
                     List = List.filter("/dev");
                     foreach(QString item, List)
                            qDebug() << "List items = " << item;
          }
          else
          {
              qDebug() << "StdError:" << StdError;
          }
      
      
      J Offline
      J Offline
      JonB
      wrote on 14 Dec 2022, 11:52 last edited by JonB
      #4

      @Qt-embedded-developer
      Because your /dev/sd* has a * wildcard in it. ls (or any other Linux command) does not expand wildcards. Only a shell expands wildcards! If you want to do it this way you will need:

       Command = "/bin/sh";
      
      args << "-c" << "ls /dev/sd*";
      

      Note that running a command to expand a filepath like this is not "efficient". You could do the work from, say, Qt QDir on /dev and look through the child filenames yourself to see which start with sd. Or QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const could be used to do the wildcard matching. This would also be a lot cleaner than your code trying to parse ls's output.

      Q 1 Reply Last reply 14 Dec 2022, 12:16
      1
      • Q Qt embedded developer
        14 Dec 2022, 11:45

        i have implemted below code to find usb device name to print but its not working .

        Actually this command working when execute on linux terminal.

         Command = "ls";
        
        args<<" /dev/sd*"
        
            OProcess.start(Command,args,QIODevice::ReadOnly); //Starts execution of command
            OProcess.waitForFinished();                       //Waits for execution to complete
        
            QString sFromDateTime = OProcess.readAllStandardOutput();  //Reads standard output
            QString StdError = OProcess.readAllStandardError();
        
            if(StdError.isEmpty())
            {
        
                qDebug() << "output : " << sFromDateTime;
        
                    QStringList List;
                       List = sFromDateTime.split("\n");
                       List = List.filter("/dev");
                       foreach(QString item, List)
                              qDebug() << "List items = " << item;
            }
            else
            {
                qDebug() << "StdError:" << StdError;
            }
        
        
        J Offline
        J Offline
        jsulm
        Lifetime Qt Champion
        wrote on 14 Dec 2022, 11:48 last edited by jsulm
        #2

        @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

        Command = "ls";

        Use whole path to ls instead of relative path.
        Also, always add error handling code (https://doc.qt.io/qt-6/qprocess.html#errorOccurred) to know what happens...
        "/dev/sd*" will NOT work with QProcess, because the shell is resolving *.

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

        Q 1 Reply Last reply 14 Dec 2022, 11:51
        0
        • J jsulm
          14 Dec 2022, 11:48

          @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

          Command = "ls";

          Use whole path to ls instead of relative path.
          Also, always add error handling code (https://doc.qt.io/qt-6/qprocess.html#errorOccurred) to know what happens...
          "/dev/sd*" will NOT work with QProcess, because the shell is resolving *.

          Q Offline
          Q Offline
          Qt embedded developer
          wrote on 14 Dec 2022, 11:51 last edited by Qt embedded developer
          #3

          @jsulm said in why QProcess not show output of ls /dev/sd* command ?:

          shell is resolving *

          so what i need to do to execute it ?

          i think its not relative path this is absolute path. if not then can you let me know how i can get absolute path ?

          J 2 Replies Last reply 14 Dec 2022, 11:57
          0
          • Q Qt embedded developer
            14 Dec 2022, 11:45

            i have implemted below code to find usb device name to print but its not working .

            Actually this command working when execute on linux terminal.

             Command = "ls";
            
            args<<" /dev/sd*"
            
                OProcess.start(Command,args,QIODevice::ReadOnly); //Starts execution of command
                OProcess.waitForFinished();                       //Waits for execution to complete
            
                QString sFromDateTime = OProcess.readAllStandardOutput();  //Reads standard output
                QString StdError = OProcess.readAllStandardError();
            
                if(StdError.isEmpty())
                {
            
                    qDebug() << "output : " << sFromDateTime;
            
                        QStringList List;
                           List = sFromDateTime.split("\n");
                           List = List.filter("/dev");
                           foreach(QString item, List)
                                  qDebug() << "List items = " << item;
                }
                else
                {
                    qDebug() << "StdError:" << StdError;
                }
            
            
            J Offline
            J Offline
            JonB
            wrote on 14 Dec 2022, 11:52 last edited by JonB
            #4

            @Qt-embedded-developer
            Because your /dev/sd* has a * wildcard in it. ls (or any other Linux command) does not expand wildcards. Only a shell expands wildcards! If you want to do it this way you will need:

             Command = "/bin/sh";
            
            args << "-c" << "ls /dev/sd*";
            

            Note that running a command to expand a filepath like this is not "efficient". You could do the work from, say, Qt QDir on /dev and look through the child filenames yourself to see which start with sd. Or QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const could be used to do the wildcard matching. This would also be a lot cleaner than your code trying to parse ls's output.

            Q 1 Reply Last reply 14 Dec 2022, 12:16
            1
            • Q Qt embedded developer
              14 Dec 2022, 11:51

              @jsulm said in why QProcess not show output of ls /dev/sd* command ?:

              shell is resolving *

              so what i need to do to execute it ?

              i think its not relative path this is absolute path. if not then can you let me know how i can get absolute path ?

              J Offline
              J Offline
              jsulm
              Lifetime Qt Champion
              wrote on 14 Dec 2022, 11:57 last edited by
              #5

              @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

              i think its not relative path this is absolute path

              How is ls an absolute path?!
              On my Ubuntu machine the absolute path to ls is /usr/bin/ls

              I'm wondering why you need QProcess for such a task at all? You can simply use https://doc.qt.io/qt-6/qdir.html

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

              Q 1 Reply Last reply 14 Dec 2022, 12:14
              0
              • Q Qt embedded developer
                14 Dec 2022, 11:51

                @jsulm said in why QProcess not show output of ls /dev/sd* command ?:

                shell is resolving *

                so what i need to do to execute it ?

                i think its not relative path this is absolute path. if not then can you let me know how i can get absolute path ?

                J Offline
                J Offline
                jsulm
                Lifetime Qt Champion
                wrote on 14 Dec 2022, 11:58 last edited by
                #6

                @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                can you let me know how i can get absolute path ?

                Execute the bellow command:
                which ls

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

                J 1 Reply Last reply 14 Dec 2022, 11:59
                1
                • J jsulm
                  14 Dec 2022, 11:58

                  @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                  can you let me know how i can get absolute path ?

                  Execute the bellow command:
                  which ls

                  J Offline
                  J Offline
                  JonB
                  wrote on 14 Dec 2022, 11:59 last edited by JonB
                  #7

                  @jsulm said in why QProcess not show output of ls /dev/sd* command ?:

                  which ls

                  Observation: if which finds ls then there is no need to use a full path to it. And if it's not on your path which won't find it! :)

                  1 Reply Last reply
                  1
                  • J jsulm
                    14 Dec 2022, 11:57

                    @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                    i think its not relative path this is absolute path

                    How is ls an absolute path?!
                    On my Ubuntu machine the absolute path to ls is /usr/bin/ls

                    I'm wondering why you need QProcess for such a task at all? You can simply use https://doc.qt.io/qt-6/qdir.html

                    Q Offline
                    Q Offline
                    Qt embedded developer
                    wrote on 14 Dec 2022, 12:14 last edited by
                    #8

                    @jsulm means i just need to check there is file name start with sd is there on not int /dev directory

                    J 1 Reply Last reply 14 Dec 2022, 12:17
                    0
                    • J JonB
                      14 Dec 2022, 11:52

                      @Qt-embedded-developer
                      Because your /dev/sd* has a * wildcard in it. ls (or any other Linux command) does not expand wildcards. Only a shell expands wildcards! If you want to do it this way you will need:

                       Command = "/bin/sh";
                      
                      args << "-c" << "ls /dev/sd*";
                      

                      Note that running a command to expand a filepath like this is not "efficient". You could do the work from, say, Qt QDir on /dev and look through the child filenames yourself to see which start with sd. Or QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const could be used to do the wildcard matching. This would also be a lot cleaner than your code trying to parse ls's output.

                      Q Offline
                      Q Offline
                      Qt embedded developer
                      wrote on 14 Dec 2022, 12:16 last edited by
                      #9

                      @JonB said in why QProcess not show output of ls /dev/sd* command ?:

                      QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const

                      as per my case what i need to write in above ?

                      J 1 Reply Last reply 14 Dec 2022, 12:17
                      0
                      • Q Qt embedded developer
                        14 Dec 2022, 12:14

                        @jsulm means i just need to check there is file name start with sd is there on not int /dev directory

                        J Offline
                        J Offline
                        jsulm
                        Lifetime Qt Champion
                        wrote on 14 Dec 2022, 12:17 last edited by
                        #10

                        @Qt-embedded-developer Yes, get entries in /dev folder using QDir::entryInfoList and filter all entries starting with sd (you can pass file name filter directly to QDir::entryInfoList).

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

                        1 Reply Last reply
                        0
                        • Q Qt embedded developer
                          14 Dec 2022, 12:16

                          @JonB said in why QProcess not show output of ls /dev/sd* command ?:

                          QFileInfoList QDir::entryInfoList(const QStringList &nameFilters, QDir::Filters filters = NoFilter, QDir::SortFlags sort = NoSort) const

                          as per my case what i need to write in above ?

                          J Offline
                          J Offline
                          jsulm
                          Lifetime Qt Champion
                          wrote on 14 Dec 2022, 12:17 last edited by
                          #11

                          @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                          as per my case what i need to write in above ?

                          What exactly is not clear?

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

                          Q 1 Reply Last reply 14 Dec 2022, 12:19
                          1
                          • J jsulm
                            14 Dec 2022, 12:17

                            @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                            as per my case what i need to write in above ?

                            What exactly is not clear?

                            Q Offline
                            Q Offline
                            Qt embedded developer
                            wrote on 14 Dec 2022, 12:19 last edited by Qt embedded developer
                            #12

                            @jsulm sorry its clear. i have to try. but if possible can you give example so it can help better.

                            C 1 Reply Last reply 14 Dec 2022, 12:36
                            0
                            • Q Qt embedded developer
                              14 Dec 2022, 12:19

                              @jsulm sorry its clear. i have to try. but if possible can you give example so it can help better.

                              C Offline
                              C Offline
                              Christian Ehrlicher
                              Lifetime Qt Champion
                              wrote on 14 Dec 2022, 12:36 last edited by
                              #13

                              @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                              but if possible can you give example so it can help better.

                              Maybe reading the answers would help - it was already given...

                              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                              Visit the Qt Academy at https://academy.qt.io/catalog

                              Q 1 Reply Last reply 14 Dec 2022, 12:39
                              0
                              • C Christian Ehrlicher
                                14 Dec 2022, 12:36

                                @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                                but if possible can you give example so it can help better.

                                Maybe reading the answers would help - it was already given...

                                Q Offline
                                Q Offline
                                Qt embedded developer
                                wrote on 14 Dec 2022, 12:39 last edited by Qt embedded developer
                                #14

                                @Christian-Ehrlicher i have tried like below but i am getting empty list from fileinfolist

                                QString myPath="/dev";
                                        QDir dir;
                                        dir.setPath(myPath);
                                        QStringList filters;
                                        filters<<"sd";
                                        dir.setFilter( QDir::AllEntries | QDir::System);
                                        dir.setNameFilters(filters);
                                        dir.setSorting(QDir::Name );
                                        QFileInfoList list = dir.entryInfoList();
                                
                                

                                why my list is not giving file name what is expected ?

                                J 1 Reply Last reply 14 Dec 2022, 13:02
                                0
                                • C Offline
                                  C Offline
                                  Christian Ehrlicher
                                  Lifetime Qt Champion
                                  wrote on 14 Dec 2022, 12:43 last edited by
                                  #15

                                  Because there is no /dev/sd in your directory. Please read the documentation...

                                  Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                  Visit the Qt Academy at https://academy.qt.io/catalog

                                  Q 1 Reply Last reply 14 Dec 2022, 12:45
                                  0
                                  • C Christian Ehrlicher
                                    14 Dec 2022, 12:43

                                    Because there is no /dev/sd in your directory. Please read the documentation...

                                    Q Offline
                                    Q Offline
                                    Qt embedded developer
                                    wrote on 14 Dec 2022, 12:45 last edited by
                                    #16

                                    @Christian-Ehrlicher actually above code i written to find name that start with sd .

                                    C 1 Reply Last reply 14 Dec 2022, 12:50
                                    0
                                    • Q Qt embedded developer
                                      14 Dec 2022, 12:45

                                      @Christian-Ehrlicher actually above code i written to find name that start with sd .

                                      C Offline
                                      C Offline
                                      Christian Ehrlicher
                                      Lifetime Qt Champion
                                      wrote on 14 Dec 2022, 12:50 last edited by Christian Ehrlicher
                                      #17

                                      @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                                      actually above code i written to find name that start with sd .

                                      No, RTFM!

                                      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                                      Visit the Qt Academy at https://academy.qt.io/catalog

                                      1 Reply Last reply
                                      0
                                      • Q Qt embedded developer
                                        14 Dec 2022, 12:39

                                        @Christian-Ehrlicher i have tried like below but i am getting empty list from fileinfolist

                                        QString myPath="/dev";
                                                QDir dir;
                                                dir.setPath(myPath);
                                                QStringList filters;
                                                filters<<"sd";
                                                dir.setFilter( QDir::AllEntries | QDir::System);
                                                dir.setNameFilters(filters);
                                                dir.setSorting(QDir::Name );
                                                QFileInfoList list = dir.entryInfoList();
                                        
                                        

                                        why my list is not giving file name what is expected ?

                                        J Offline
                                        J Offline
                                        JonB
                                        wrote on 14 Dec 2022, 13:02 last edited by
                                        #18

                                        @Qt-embedded-developer said in why QProcess not show output of ls /dev/sd* command ?:

                                        filters<<"sd";

                                        void QDir::setNameFilters(const QStringList &nameFilters)

                                        Each name filter is a wildcard (globbing) filter that understands * and ? wildcards. See QRegularExpression::fromWildcard().

                                        1 Reply Last reply
                                        0

                                        5/18

                                        14 Dec 2022, 11:57

                                        topic:navigator.unread, 13
                                        • Login

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