We have moved to a new Sailfish OS Forum. Please start new discussions there.

Run Bash Command from App?

asked 2018-11-13 09:27:29 +0300

lispy gravatar image

updated 2018-11-13 09:27:39 +0300

How can I run a bash command from within a QT App? Sorry, if this is the wrong place to ask. I think it's not ok for Harbour Apps to do this, but I want to run a simple one liner from withn a Sailfish app but couldn't find a good example.

edit retag flag offensive close delete



The most naive version could be

QProcess::execute("/bin/bash", {"-c", "<your-script>"});

But do you really need the power of bash? What does the command you would like to execute look like?

martyone ( 2018-11-13 11:18:31 +0300 )edit

pactl set-sink-volume 1 180% Makes Xperia X louder.

lispy ( 2018-11-13 12:47:51 +0300 )edit

4 Answers

Sort by » oldest newest most voted

answered 2018-11-13 13:40:02 +0300

martyone gravatar image

If you need to execute a BASH script, the most naive way could be

QProcess::execute("/bin/bash", {"-c", "<your-script>"});

If you need to execute single command, BASH is not needed:

QProcess::execute("/path/to/command", {"arg1", "arg2"});

If your command and arguments are hard-coded, this can be used:

QProcess::execute("/path/to/command 'arg1' 'arg2'");

Check QProcess API docs for more advanced use.

edit flag offensive delete publish link more

answered 2018-11-13 10:00:48 +0300

DaveRo gravatar image

I did it recently in python (I don't know C). There are extracts from the code here: https://together.jolla.com/question/191655/error-passing-argument-to-pyotherside/

That worked fine for issuing a command and returning the response.

It turned out that maintaining an SSH session, which meant continuing the shell session, was more complicated; I ended up using pexpect.

edit flag offensive delete publish link more

answered 2018-11-13 16:01:59 +0300

Rikujolla gravatar image

You can add simple header file in your app like in my app: https://github.com/Rikujolla/trafficviewer/blob/master/src/forceupdater.h Then you can call it from your qml file

edit flag offensive delete publish link more



Great. was looking for that missing link. Thanks for all the friendly help.

lispy ( 2018-11-13 16:42:46 +0300 )edit

answered 2019-06-09 16:22:24 +0300

marsch gravatar image

In case you're at this currently: It would actually be cool if you built a generic app for commands, i.e. an app that doesn't anything else then executing the commands a user puts into a textarea in its settings. Virtually the equivalent of a bash script that can be started graphically by clicking the icon.

edit flag offensive delete publish link more



Isn't ShellEx like that? It runs any script you write.

hsjpekka ( 2019-06-09 20:38:53 +0300 )edit

Yes, exactly. https://openrepos.net/content/acce/shellex

peterleinchen ( 2019-06-09 21:12:58 +0300 )edit

It's in Jolla Store also.

hsjpekka ( 2019-06-09 21:51:17 +0300 )edit
Login/Signup to Answer

Question tools



Asked: 2018-11-13 09:27:29 +0300

Seen: 714 times

Last updated: Jun 09 '19