Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

Customize welcome messages based on time of day UNIX
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:
set tod='morning'
set hour=`date | cut -c 12-13`
if ($hour > 11) then
set tod='afternoon'
if ($hour > 17) then
set tod='evening'
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.]
  • Currently 0.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (0 votes cast)

Customize welcome messages based on time of day | 6 comments | Create New Account
Click here to return to the 'Customize welcome messages based on time of day' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Error when run
Authored by: laurence.wilks on Apr 30, '02 03:17:11AM

I get the following error when running the script from a Terminal window:

parse error near `/Private/etc/motd'

I've tried running the script using sudo with no joy.

any ideas?

[ Reply to This | # ]
Error when run
Authored by: ashevin on Apr 30, '02 11:39:40AM


Chances are you left out a punctuation mark or 2. What happens if you run just the last line on the command line?

- Avi

[ Reply to This | # ]
Error when run
Authored by: laurence.wilks on May 01, '02 02:52:00AM

Hi Avi

When I run:

echo "Hello and welcome to Darwin." >/etc/motd

in the Terminal I get the expected result. If I run the script it seems to be having problems parsing the variable.

Any help would be apricated as this should be simple but apparantly is not!


[ Reply to This | # ]
Customizing MOTD
Authored by: jeepboy on Apr 30, '02 09:31:51AM

Hey! I have been playing with MOTD for a while because I would like to find a way for it to pull headlines from a news site and incorporate them into the MOTD. Thing is, I am new to Unix, and I'm not sure how to write a script that would go out to, perhaps CNN, and grab the top headline and paste it into the MOTD.

Any thoughts?


[ Reply to This | # ]
Customizing MOTD
Authored by: yosithezet on Apr 30, '02 08:49:06PM

Use PERL's LWP module to create a web agent to grab the info you want, likely some kind of RSS site like MacReporter uses and then toss that in the motd file. Set it up with a cron to keep updating.

[ Reply to This | # ]
/Private is wrong and a warning
Authored by: a1291762 on Apr 30, '02 05:50:41PM

/Private should be /private (at least on my system). Of course you could just use /etc/motd...

Not for a warning... If you leave the file writable (even if by root) then theres a chance somebody will be able to write to it and have that code executed as root by cron. It's a small security risk but it's still a risk.

chmod 555 that file so that everyone can read and execute but nobody can write. If you need to make a change, you may have to chmod 755 first.

[ Reply to This | # ]