why QProcess not show output of ls /dev/sd* command ?
-
wrote on 14 Dec 2022, 11:45 last edited by
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; }
-
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; }
wrote on 14 Dec 2022, 11:52 last edited by 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 withsd
. OrQFileInfoList 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 parsels
's output. -
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; }
@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 *. -
@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 *.wrote on 14 Dec 2022, 11:51 last edited by 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 ?
-
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; }
wrote on 14 Dec 2022, 11:52 last edited by 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 withsd
. OrQFileInfoList 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 parsels
's output. -
@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 ?
@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/lsI'm wondering why you need QProcess for such a task at all? You can simply use https://doc.qt.io/qt-6/qdir.html
-
@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 ?
@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 -
@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 lswrote on 14 Dec 2022, 11:59 last edited by JonB@jsulm said in why QProcess not show output of ls /dev/sd* command ?:
which ls
Observation: if
which
findsls
then there is no need to use a full path to it. And if it's not on your pathwhich
won't find it! :) -
@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/lsI'm wondering why you need QProcess for such a task at all? You can simply use https://doc.qt.io/qt-6/qdir.html
wrote on 14 Dec 2022, 12:14 last edited by@jsulm means i just need to check there is file name start with sd is there on not int /dev directory
-
@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 withsd
. OrQFileInfoList 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 parsels
's output.wrote on 14 Dec 2022, 12:16 last edited by@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 ?
-
@jsulm means i just need to check there is file name start with sd is there on not int /dev directory
@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).
-
@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 ?
@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?
-
@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?
wrote on 14 Dec 2022, 12:19 last edited by Qt embedded developer@jsulm sorry its clear. i have to try. but if possible can you give example so it can help better.
-
@jsulm sorry its clear. i have to try. but if possible can you give example so it can help better.
@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-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...
wrote on 14 Dec 2022, 12:39 last edited by 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 ?
-
Because there is no
/dev/sd
in your directory. Please read the documentation... -
Because there is no
/dev/sd
in your directory. Please read the documentation...wrote on 14 Dec 2022, 12:45 last edited by@Christian-Ehrlicher actually above code i written to find name that start with sd .
-
@Christian-Ehrlicher actually above code i written to find name that start with sd .
Lifetime Qt Championwrote on 14 Dec 2022, 12:50 last edited by Christian Ehrlicher@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!
-
@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 ?
wrote on 14 Dec 2022, 13:02 last edited by@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/18