Customize welcome messages based on time of day

Apr 30, '02 01:58:42AM

Contributed by: Anonymous

I saw your earlier tip regarding changing the motd file, but this takes things a little further. I created a shell script that is run periodically via cron (run as root, so it has write permission to /etc/motd) that changes the message of the day based on the time. I stored the script in /usr/local/bin, set the execute flag and changed the owner to root and group to wheel.

Read the rest of the article for the script and an explanation as to how it works.

Here is the script:

#!/bin/tcsh
set tod='morning'
set hour=`date | cut -c 12-13`
if ($hour > 11) then
set tod='afternoon'
endif
if ($hour > 17) then
set tod='evening'
endif
echo "Good" $tod "and welcome to Darwin." > /Private/etc/motd
(The last "echo" command is on all one line)

The way this works is to initially set a variable to "morning". It then sets a variable to the results of running date to get the current date and time. This is piped to cut which selects characters 12 and 13 which correspond to the hour (in 24-hour format). The open single quote characters around the statement means that its contents will be evaluated before the assignment is made, which is a very useful tool in shell scripting. The hour is then compared against 11 to see if the time is 12 or later. If so, the time of day string is changed to "afternoon". If the time is greater than 17 (6 pm or later), the string is changed to "evening". This is then echoed out and redirected to the file /Private/etc/motd.

When this runs next, the motd file will be set according to the time of day and the next time you open a terminal window, the welcoming message will be appropriate to the time of day.

Of course, if your machine doesn't run all the time, the cron task may not have run, but there are other tools available to solve this problem.

[Editor's note: I have not tried this script myself.]

Comments (6)


Mac OS X Hints
http://hints.macworld.com/article.php?story=2002043001584298