- Platform information:
- Hardware: CPUArchitecture/RAM/storage
- OS: what OS is used and which version
- Java Runtime Environment: which java platform is used and what version
- openHAB version:
- Issue of the topic: please be detailed explaining your issue
I have a hard time finding my way through the jungle that is executeCommandLine. Googeling examples finds lots of different syntaxes that not longer work in OH 3. Up to now not a single script using executeCommandLine worked for me. To make it as simple as possible, this script should work:
var String testvar = executeCommandLine(Duration.ofSeconds(5), "/bin/cat", "/volume1/docker/openhab/conf/html/xxx.txt")
But it doesn’t:
- /bin/cat: /volume1/docker/openhab/conf/html/xxx.txt: No such file or directory
What the heck is wrong here? The file xxx.txt contains the simple line “This is a test.”, it belongs to user 9001 (OpenHab) and is read/writable for everyone (rw-rw-rw-). The path is correct.
This is just a test. My actual aim is to feed a variable value into a file (echo $VARIABLE >> xxx.txt). I already have a horror to find out the OH syntax for this simple command.
Any help appreciated.
You seem to be making a Docker-beginner mistake.
The path is correct from your POV, but not from the POV of the process running in the Docker container.
You probably want to change it to:
var String testvar = executeCommandLine(Duration.ofSeconds(5), "/bin/cat", "/openhab/conf/html/xxx.txt")
Indeed. Who would have thought that software has a point of view? No, it absolutely makes sense. I’m a bit embarrassed that I didn’t see this mistake. Yes, I never used Docker before openHAB (1 month) and hadn’t fully digested the fact that I’m running a VM. Thank you very much!
Still don’t know how to express the following command in the OH 3 way:
echo "This is a test." >> /openhab/conf/html/xxx.txt
I’ve tried several variants but I’m going blind since I cannot find a full documentation how the new syntax of executeCommandLine under OH3 works and basically guess. What are people using as a reference guide? (Sorry for this bloody beginner’s question).
You just give executeCommandLine() parameters.
You’ll probably want a timeout, then I see four elements in your command. An instruction, a string, another instruction, a filename.
You might need to experiment with escaping the quotemarks of your string.
It’s worth pointing out executeCommandLine is not running in a shell, it is not executeConsoleCommand.
You can of course also start bash and instruct it to execute a shell command:
executeCommandLine(Duration.ofSeconds(5), "/bin/bash", "-c", "echo 'This is a test.' >> /openhab/conf/html/xxx.txt")
Thank you very much for your answers. I finally got a working solution but in a different way (inspired by what you wrote).
Wouter’s method of calling /bin/bash worked but I failed when I tried to replace the fixed string “This is a test” with a variable value. Surely a no-brainer for you guys but not within my meager competences.
I could not get a working command line with rossko57’s approach. I tried every variation along the lines of
executeCommandLine(Duration.ofSeconds(5), "/bin/echo ", "'Test'", ">> ", "/path/to/file")
With commata, without, with quotation marks, without, grouping the parameters differently, etc. The documentation of executeCommandLine is so patchy that I didn’t even know how it is meant. Again, surely a no-brainer for more experienced users.
I ended up with a short shell script (capture.sh):
echo "* TODO" $1 >> /openhab/conf/scripts/capture.org
which is invoked by
executeCommandLine(Duration.ofSeconds(5), "/openhab/conf/scripts/capture.sh", message)
The variable message is passed to the shell script as an argument. It is extracted from Amazon Echo’s channel last.command. The whole procedure is part of my attempts to make Alexa + openHAB work with Emacs orgmode. Now I can say to Alexa “Take a note” → “what is your note?” → “Write abstract for the new article”. The string “Write abstract for the new article” is then turned into an orgmode TODO line and appended to orgmode’s inbox (capture.org) which, in turn, is synced via Syncthing.
I’ve been explaining this in so great detail in case that other orgmode users are interested in my approaches to integrate orgmode and openHAB. I have some other scripts, too, that make the whole agenda accessible in openHAB and can be streamed to Alexa text-to-speech. Please send me an email if someone is interested in the full approach.
If you just want to append text to a file, using the Java NIO Files class can also be convenient:
var todo = "Do this!"
"* TODO " + todo + "\n",
It would save the overhead of creating a new process and all sorts of escaping.