Important: Please read the Qt Code of Conduct -

echo redirect with QProcess

  • Hi

    I want to use QProcess() with "echo".

    In terminal, $ echo "some out put" > test.txt , is OK.

    But, QProcess start("echo "some out put" > test.txt"), does not work.

    Finally, I want to call usb unbind/bind from Qt app.

    I want to run "echo "usb1" > /sys/bus/usb/drivers/unbind".

    Are there any solutions ?

    Best Regards,

  • @Shin.go
    In your commands, it is the > output redirection symbol which is your problem. Only a shell (e.g. /bin/sh or /bin/bash) understands that symbol and what to do about it. You have two choices:

    • Handle the redirection to file yourself prior to calling QProcess or similar. Nicest, but most work.
      (EDIT: I see there is QProcess::setStandardOutputFile(const QString &fileName, OpenMode mode = Truncate). You could use this and remove your > text.txt from your command line, then your command should work without needing a shell to interpret the >. Note that it will use /bin/echo (or whatever is on your PATH) rather than a shell's built-in echo.

    • Make the whole of your command line into a single string to be handled by the shell, and do a QProcess of the shell with that as an argument. Untested by me, but something like:
      /bin/sh -c 'echo "some out put" > test.txt'

Log in to reply