1

I'm using python scripts to create my MOTD in Ubuntu Server 18.04. I found this problem when trying to display the temps of my hard drives. I wanted to display the temperature using the degree symbol as such ##°C, but the output wouldn't show when I logged in to the server. Removing the degree symbol fixed the problem.

Example code

#!/usr/bin/python3

print(u"\u00B0" + "C")
print("°C")
print('°' + "C")
print('\u00B0' + "C")

File was saved as 00-testpython, and running the script from the command line outputs as expected:

$ ./00-testpython 
°C
°C
°C
°C

Running the scripts with run-parts returns the expected response:

$ run-parts --lsbsysinit /etc/update-motd.d
°C
°C
°C
°C

Logging in with ssh displays whatever was in the old /run/motd.dynamic, and does not update with the expected MOTD.

I have a strong feeling that this has something to do with encoding, but I'm not sure what it would be. Printing from python can use ANSI escape characters to print in color; I'm not sure why this is causing an issue.

Using a bash script to echo the degree symbol works fine:

#!/bin/sh

echo °

1 Answer 1

1

It turned out to be an encoding issue as I suspected. I found a similar question to encoding in the MOTD with the answer I needed here: https://serverfault.com/a/207357 When I ran the command to check the environment variable LANG from my logged-in console, I got the value:

$ echo $LANG
C.UTF-8

But if I put the same command in my MOTD (bash) script, LANG was empty when I logged in. I assume the default would be ASCII in that case, which would have caused issues when encoding the degree symbol.

To fix the problem, in my script I just needed to add the line:

export LANG="C.UTF-8"

I put this line in a bash script which then calls my python script. I couldn't get it to work in the python script with the command os.environ['LANG'] = "C.UTF-8"

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .