Shell Script Which Gets Executed With Greetings – The Moment User Logs In

in Categories Time and Date last updated April 5, 2008

Write shell script which gets executed the moment user logs in, it should greet user

Script first finds out current hour using date command

Depend upon time of the day, it will say Good morning or Good afternoon to user.

Open your bash / shell startup file i.e. profile file – ~/.bash_profile and put path to this script in file as follow:
/home/you/path/to/script

Sample shell script to greet user

#!/bin/bash
# Shell program which gets executed the moment the user logs in, it should
# display the message "Good morning", "Good Afternoon", or "Good Evening"
# depnding upon the time which the user logs in.
# =====================================================================
# Q. How to run this script as soon as user logs in?
# A. Open your bash startup file aka profile file - ~/.bash_profile
# and put path to this script in file as follow:
# . /path/to/greetings.sh
# -----------------------------------------------
# Copyright (c) 2005 nixCraft project <http://cyberciti.biz/fb/>
# This script is licensed under GNU GPL version 2.0 or above
# -------------------------------------------------------------------------
# This script is part of nixCraft shell script collection (NSSC)
# Visit http://bash.cyberciti.biz/ for more information.
# -------------------------------------------------------------------------
 
# get current hour (24 clock format i.e. 0-23)
hour=$(date +"%H")
 
# if it is midnight to midafternoon will say G'morning
if [ $hour -ge 0 -a $hour -lt 12 ]
then
  greet="Good Morning, $USER"
# if it is midafternoon to evening ( before 6 pm) will say G'noon
elif [ $hour -ge 12 -a $hour -lt 18 ] 
then
  greet="Good Afternoon, $USER"
else # it is good evening till midnight
  greet="Good evening, $USER"
fi
 
# display greet
echo $greet

Share this on:

4 comment

  1. Thank you! this has helped me in a linux net admin lab. The instructor decided not to teach us one bit of this and expected us to do it.

    Thanks,

    Cesar

    Have a question? Post it on our forum!