xawk.com - technical wisdom served up in little slices xawk.com -- Tags -- Bash
 
xawk.com -- Tags -- Bash -- Bash - A Basic Greeting Program.

Bash - A Basic Greeting Program.

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.

View Comments (0)

Tags: Bash

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


 

Tag: Bash
Bash - A Basic Greeting Program.
Bash - An Introduction to Scripting

All Tags
Apple
Bash
Business Tips
Css
Delphi
Firefox
Gmail
Html
MySql
Php
Productivity Tips
Ruby
Ubuntu
Website Tips
Windows XP

Search