Echo output to file fails

I have a working sript

!/bin/bash
ip -details -statistics link show can0 | grep BUS-OFF > NULL
if [ $? = "0" ]
	then
		ip link set can0 down
		ip link set can0 up type can bitrate 250000
		systemctl start publish-py.service &
		echo "$(date) : BUS-OFF restart" >> /var/log/home.log

fi

I have another long working script

some code
    target_port_down = 0
    #print(msg.topic+" "+str(msg.payload))
    topic = msg.topic
    echo (f"topic. [topic]") >> /var/log/sl.log
    # Stehlampen
much more code

msg.topicwhere I added the line with the echo, and now it get an error at the “>>”
Do not understand it, There are a lot of samples on the net doing the same.

This is bash scripting, not Python, so you should probably ask this on a different forum.

I imagine many people here could debug this but it’s off-topic for the forum.

Yes you are right, the first is bash, the second is Python. I took a line of bash in Python. I am an idiot. so I have to open a file write to it and close it.

Thank you
Happy New Year

Or you can use logging module

Thank you, it is fixed
f=open("name,“at”)
f.write(“test \n”)
f.close

This is the preferred form:

with open("name", "at") as f:
    f.write("test \n")

This guarantees that the file is closed if an exception is raised.

1 Like