BradTrupp.com - Little slices of my life and my projects BradTrupp.com -- Tags -- Code
BradTrupp.com -- Tags -- Code -- Bash - A Basic Greeting Program. (Bash)

Bash - A Basic Greeting Program. (Bash)
(2007/10/31)

This is a basic Bash shell program to greet you with short message based on the time of the day.

#!/bin/bash

# get current hour - uses 24 hour format ( 0 to 23 )
hour=$(date +"%H");

msg="Good Morning, $USER"
if [ $hour -ge 12 -a $hour -lt 18 ]; then 
   msg="Good Afternoon, $USER" 
fi
if [ $hour -ge 18 ]; then 
  msg="Good Evening, $USER" 
fi

# display msg
echo "$msg"

Save the script as greetings.sh.

To run it, you would open a terminal session, change to directory where you saved the scripts, and enter the command as shown below.

./greetings.sh

Depending on the time of day, you should be greeted with Good Morning, Good Afternoon, or Good Evening.

Date Tips

The first line to get the hour will seem a little strange.

hour=$(date +"%H");

What is happening is that you are running the date command with the optional format specifier. The format is "date [OPTION]... [+FORMAT]" where date is the command and "%H" means format the results as an hour in 24 hour format.

The result of the date command gets saved as the value of the variable hour.

Tags: Code

Share: Del.icio.us | Digg | Facebook | Google Bookmarks | Reddit | Technorati | Twitter | Windows Live | Yahoo! My Web

View Comments (0)


 

Tag: Code
A Little Bit of Automation for your Backups (2010/02/25)
A Simple CSV to iCalendar Conversion Utility written in Ruby (2009/06/29)
A Simple iCalendar to Task Coach Conversion Utility written in Ruby (2009/05/09)
Using Ruby to generate Social Bookmarks for your Web Pages (2008/11/23)
Using Ruby to fix id3 tags on mp3 files (Ruby) (2008/11/10)
Copy all files in a directory tree to a common destination. (Ruby) (2008/10/08)
Check for File Changes using Ruby and MD5 (Ruby) (2008/08/20)
Create Checkpoints using Ruby and MD5 (Ruby) (2008/07/30)
Bash - A Basic Greeting Program. (Bash) (2007/10/31)
Bash - An Introduction to Scripting (Bash) (2007/10/10)
UUMerge Command Line Text File Merge (Delphi) (2007/07/01)
Explorer-Style Fly-By Buttons (Delphi) (2006/01/14)
Handle-Free Checkboxes in a String Grid (Delphi) (2006/01/14)

All Tags
Business Tips (5)
Code (13)
Life Skills (1)
Music (2)
My 15 minutes (5)
Old Articles (39)
Photos (10)

Advertisement