Solved Run a command with different result between in QProcess and in terminal
-
Hi
I find some special commands run in QProcess will get different result (different from in terminal ).
e.g. I usemplayer -af volume=-20 xxx.wav > /dev/null
.
In terminal it just play the audio with no debug.
But in QProcess it will run likemplayer -af volume=-20 xxx.wav , mplayer -af volume=-20 > ,mplayer -af volume=-20 /dev/null
.e.g.
sed -i \'s/iface eth0 inet static/iface eth0 inet dhcp/g\' /etc/network/interfaces
in terminal it works well.
in QProcess, it returns:
sed: -e expression #1, char 1: unknown command: `''What should I pay attention to?
Regards
Mihan -
@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 \\
-
@jsulm
Sure, Here is the codevoid PlayInMplayer(QString file, int volume) { QString order = QString("mplayer -af volume=%1 %2 > /dev/null") .arg(QString::number(volume)) .arg(file); QProcess::execute(order); }
-
@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 usebash -c
on the command line, or pass your items as separate arguments (without quoting) in theQStringList
arguments toexecute()
. -
@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!
-
@Mihan
The list of symbols interpreted by the Linux shell, which you therefore cannot pass directly to execute without going throughbash -c
or thinking through, includes but is not limited to:< > | & ; ' " $ * [ ( ~ # \ <space>
So kind of most symbols!
-
@JonB
By the way, doessystem()
interpret symbols by the linux shell? I usedsystem()
to process commands before. -
@Mihan
It's not thatsystem()
does any interpretation, it's thatsystem(command)
precisely does/bin/sh -c command
, technicallyexecl("/bin/sh", "sh", "-c", command, (char *) 0);
Therefore to do the equivalent from
QProcess
you would go:QProcess::execute("/bin/sh", QStringList() << "-c" << command)
-
@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. -
@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. -
@JonB Ok, so maybe I make a great topic for linux Qt's user :).
-
@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!