2

I am using a Raspberry Pi with a Debian based distro (I think it's called Raspbian). I put a file called 10-pi into the /etc/update-motd.d directory and made it executable. When I run the executable (bash) myself it shows me a nice MOTD with colors. When I login with ssh to my Pi however, I see the right MOTD but it shows everything in white. Anybody knows why this might be?

Here is my 10-pi file:

#!/bin/bash

let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
UPTIME=`printf "%d days, %02dh%02dm%02ds" "$days" "$hours" "$mins" "$secs"`

# get the load averages
read one five fifteen rest < /proc/loadavg

echo "$(tput setaf 2)
   .~~.   .~~.    `date +"%A, %e %B %Y, %r"`
  '. \ ' ' / .'   `uname -srmo`$(tput setaf 1)
   .~ .~~~..~.
  : .~.'~'.~. :   Uptime.............: ${UPTIME}
 ~ (   ) (   ) ~  Memory.............: `cat /proc/meminfo | grep MemFree | awk {'print $2'}`kB (Free) / `cat /proc/meminfo | grep MemTotal | awk {'print $2'}`kB (Total)
( : '~'.~.'~' : ) Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min)
 ~ .~ (   ) ~. ~  Running Processes..: `ps ax | wc -l | tr -d " "`
  (  : '~' :  )   IP Addresses.......: `/sbin/ifconfig eth0 | /bin/grep "inet addr" | /usr/bin/cut -d ":" -f 2 | /usr/bin/cut -d " " -f 1` and `wget -q -O - http://icanhazip.com/ | tail`
   '~ .~~~. ~'    Weather............: `curl -s "http://rss.accuweather.com/rss/liveweather_rss.asp?metric=1&locCode=EUR|UK|UK001|NAILSEA|" | sed -n '/Currently:/ s/.*: \(.*\): \([0-9]*\)\([CF]\).*/\2\3, \1/p'`
       '~'
$(tput sgr0)"

This is what it looks like when I execute the file myself: Output when executed manually

  • You might get expert help from: https://raspberrypi.stackexchange.com/ – James Brown Oct 12 '20 at 11:57
  • Probably your `ssh` terminal session doesn't support the control code repertoire you're trying to use. What's the value of `$TERM` when you don't get colored output? – tripleee Oct 12 '20 at 13:37
  • [This has already been answered on **unix.stackexchange.com**](https://unix.stackexchange.com/questions/223921/no-colour-in-motd) – 0stone0 Oct 12 '20 at 13:49
  • 1
    The link above doesn't answer the question. This is a script placed in /etc/update-motd.d and not simply a message placed in /etc/motd – Raman Sailopal Oct 12 '20 at 13:56
  • I put an echo $TERM line at the end of the script. If I login myself I always get "xterm-256color" but when it is executed as the motd it shows "dumb" – Maarten Meijer Oct 13 '20 at 12:11
  • You need to add it at the top, before any `tput` commands. – Rennex Mar 18 '22 at 20:35

1 Answers1

0

You could add the line:

export TERM="xterm-256color"

to the top of the script to ensure that the script is executed with the correct environment to display colours.

Alternatively, you can specify the colours directly as opposed to using echo and tput indirectly and so:

#!/bin/bash

let upSeconds="$(/usr/bin/cut -d. -f1 /proc/uptime)"
let secs=$((${upSeconds}%60))
let mins=$((${upSeconds}/60%60))
let hours=$((${upSeconds}/3600%24))
let days=$((${upSeconds}/86400))
UPTIME=`printf "%d days, %02dh%02dm%02ds" "$days" "$hours" "$mins" "$secs"`

# get the load averages
read one five fifteen rest < /proc/loadavg

printf "\033[1;32m
   .~~.   .~~.    `date +"%A, %e %B %Y, %r"`
  '. \ ' ' / .'   `uname -srmo`\033[0;31m
   .~ .~~~..~.
  : .~.'~'.~. :   Uptime.............: ${UPTIME}
 ~ (   ) (   ) ~  Memory.............: `cat /proc/meminfo | grep MemFree | awk {'print 
$2'}`kB (Free) / `cat /proc/meminfo | grep MemTotal | awk {'print $2'}`kB (Total)
( : '~'.~.'~' : ) Load Averages......: ${one}, ${five}, ${fifteen} (1, 5, 15 min)
 ~ .~ (   ) ~. ~  Running Processes..: `ps ax | wc -l | tr -d " "`
  (  : '~' :  )   IP Addresses.......: `/sbin/ifconfig eth0 | /bin/grep "inet addr" | 
/usr/bin/cut -d ":" -f 2 | /usr/bin/cut -d " " -f 1` and `wget -q -O - 
http://icanhazip.com/ | tail`
   '~ .~~~. ~'    Weather............: `curl -s 
      "http://rss.accuweather.com/rss/liveweather_rss.asp? 
   metric=1&locCode=EUR|UK|UK001|NAILSEA|" | sed -n '/Currently:/ s/.*: \(.*\): \([0- 
   9]*\)\([CF]\).*/\2\3, \1/p'`
       '~'
\033[1;37m"
Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
  • 1
    That's horrible advice, and probably doesn't solve the OP's problem anyway. – tripleee Oct 12 '20 at 13:35
  • That's great advice, setting TERM is literally the one line that made my customized motd script (that uses `tput`) work correctly. – Rennex Mar 18 '22 at 20:33