[Solved]Writing a file in a rule

Hello,
i tried to write a file in a rule with this command:

executeCommandLine("echo t >/home/pi/t.txt",1000) 

But it is not working…?? how can i do this???

Are you sure the owner of the openhab process has permissions to write to that directory?

No not really…

but i changed it to one openhab user can write and even there nothing happend

@rlkoshak i read Your Post created the Eule

val results = executeCommandLine("/bin/echo@@'The quick brown fox jumped over the lazy dog.'@@>/etc/openhab2/services/t.txt"

And this ist the Output in the Log

2019-06-19 22:15:00.151 [INFO ] [ipse.smarthome.model.script.execTest] - 'The quick brown fox jumped over the lazy dog.' >/etc/openhab2/services/t.txt

But No File created…whats wrong?

Add a space after the > and experiment with using double quotes and escapes. The echo command is treating everything after the String as part of the text to echo so it isn’t getting piped into the file.

Ist ther No better way May bei using Java io writer? Ist there any example available?

Thanks

Try ‘|’ instead of ‘>’ (without the quotes).

I know it doesn’t solve the root cause of the problem, but can you put the command in a shell script and then invoke the shell script from executeCommandLine?

You can use Java IO writer of course. It’s going to be about 20 lines of code or so so whether that’s better than this one line of code is I suppose left up to the coder.

I can’t remember why (user permissions, path?) but I ended up doing essentially that with little .bat files in a Windows environment; can pass parameters with filename etc., limited skills just could not make it work directly.

@rossko57 @rlkoshak
There are 2 ways doing it in an extra batch file or with one command

val String echoResult = executeCommandLine("/bin/sh@@-c@@(echo Alarm Test > /etc/openhab2/services/totalusage.txt) >/dev/null 2>&1; echo $?", 5000)
        logInfo("execTest", echoResult)

But how to I write 2 lines becuase “\n” is not ok for this. Any idea???

Did you try echo -e ‘Alarm\nTest’

Hmm. I just tried that and it didn’t work. I got

-e Alarm
Test

But, I tried echo ‘Alarm\nTest’ and got this

Alarm
Test

Odd…

So, this is the whole line that worked for me.

val String echoResult = executeCommandLine("/bin/sh@@-c@@(echo 'Multi\nLine\nTest' > /tmp/junk.txt) >/dev/null 2>&1; echo $?", 5000)

@mhilbush Thanks i forget the high comma

For those (like me) who would like to write an item’s state into a separate file:
val String echoResult = executeCommandLine("/bin/sh@@-c@@(echo '" + Loc_Work_N_Dur.state.format("%1$tH:%1$tM") + "\n' >> /var/log/openhab2/workhours.csv)", 5000)