Search code examples
linuxqttarsu

QProcess::execute with su command


I am trying to call tar from a Qt project:

QProcess::execute("/bin/su", {"-", "user", "-c", "\'/bin/tar xpf /tmp/smt.tbz2 -C /tmp\'"})

Bit I am getting:

-su: /bin/tar xpf /tmp/smt.tbz2 -C /tmp: No such file or directory

It looks like the su command does not interpret correctly the command after -c.


Solution

  • I think the problem is that you are quoting the command to run explicitly: QProcess goes to some lengths to ensure parameters are passed as-is rather than being split any further.

    Instead try...

    QProcess::execute("/bin/su", {"-", "user", "-c", "/bin/tar xpf /tmp/smt.tbz2 -C /tmp"});