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.3k 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.
  • jsulmJ jsulm

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

    jsulmJ 2 Replies Last reply
    0
    • Q Qt embedded developer

      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;
          }
      
      
      JonBJ Online
      JonBJ Online
      JonB
      wrote on 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
      1
      • Q Qt embedded developer

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

        jsulmJ Online
        jsulmJ Online
        jsulm
        Lifetime Qt Champion
        wrote on 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
        0
        • Q Qt embedded developer

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

          jsulmJ Online
          jsulmJ Online
          jsulm
          Lifetime Qt Champion
          wrote on 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

          JonBJ 1 Reply Last reply
          1
          • jsulmJ jsulm

            @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

            JonBJ Online
            JonBJ Online
            JonB
            wrote on 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
            • jsulmJ jsulm

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

              jsulmJ 1 Reply Last reply
              0
              • JonBJ JonB

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

                jsulmJ 1 Reply Last reply
                0
                • Q Qt embedded developer

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

                  jsulmJ Online
                  jsulmJ Online
                  jsulm
                  Lifetime Qt Champion
                  wrote on 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

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

                    jsulmJ Online
                    jsulmJ Online
                    jsulm
                    Lifetime Qt Champion
                    wrote on 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
                    1
                    • jsulmJ jsulm

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

                      Christian EhrlicherC 1 Reply Last reply
                      0
                      • Q Qt embedded developer

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

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 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
                        0
                        • Christian EhrlicherC Christian Ehrlicher

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

                          JonBJ 1 Reply Last reply
                          0
                          • Christian EhrlicherC Offline
                            Christian EhrlicherC Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 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
                            0
                            • Christian EhrlicherC Christian Ehrlicher

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

                              Q Offline
                              Q Offline
                              Qt embedded developer
                              wrote on last edited by
                              #16

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

                              Christian EhrlicherC 1 Reply Last reply
                              0
                              • Q Qt embedded developer

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

                                Christian EhrlicherC Offline
                                Christian EhrlicherC Offline
                                Christian Ehrlicher
                                Lifetime Qt Champion
                                wrote on 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

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

                                  JonBJ Online
                                  JonBJ Online
                                  JonB
                                  wrote on 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

                                  • Login

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