home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


UNIX Power Tools

UNIX Power ToolsSearch this book
Previous: 48.6 Get Calendar for Any Month or Year: cal Chapter 48
Office Automation
Next: 48.8 Calendar for 132-Column Terminals or Printers
 

48.7 cal That Marks Today's Date

If you're like me and you tend to forget what day it is :-) , a calendar like the one that cal (48.6 ) prints doesn't help much. Here's a little shell script below that puts angle brackets around the current date. For example, if today is August 7, 1996:

% cal


    August 1996
  S  M Tu  W Th  F  S
              1  2  3
  4  5  6 >7< 8  9 10
 11 12 13 14 15 16 17
 18 19 20 21 22 23 24
 25 26 27 28 29 30 31

If you're sure that this script will never be called by another program that expects the system version, you can name this cal , too - just be sure to put it in a directory somewhere in your PATH before /usr/bin (8.7 ) , the system location of most versions of cal . Otherwise, give the script another name, such as cal_today .

The script puts the output of date into its command-line parameters; it adds an x first for safety (in case the date command doesn't make any output, the set command will still have arguments and won't output a list of all shell variables). The parameters look like this:

x Wed Aug 7 20:04:04 PDT 1996

and the fourth parameter, in $4 , is what the script uses:




set
 




"$@"
 



#! /bin/sh
# If user didn't give arguments, put > < around today's date:
case $# in
0) set x `date`
   # Place > < around $4 (shell expands it inside doublequotes):
   /usr/bin/cal |
   sed -e 's/^/ /' -e "s/ $4$/>$4</" -e "s/ $4 />$4</"
   ;;
*) /usr/bin/cal "$@" ;;
esac

If you give any arguments, the script assumes that you don't want the current month; it runs the system cal command. Otherwise, the script pipes the system cal output into sed (34.24 ) . The sed expression puts a space before every line to make room for any> < at the start of a line. Then it uses two substitute commands - one for the beginning or middle, the other for the end of a line - one is guaranteed to match the current date.

- JP


Previous: 48.6 Get Calendar for Any Month or Year: cal UNIX Power Tools Next: 48.8 Calendar for 132-Column Terminals or Printers
48.6 Get Calendar for Any Month or Year: cal Book Index 48.8 Calendar for 132-Column Terminals or Printers

The UNIX CD Bookshelf NavigationThe UNIX CD BookshelfUNIX Power ToolsUNIX in a NutshellLearning the vi Editorsed & awkLearning the Korn ShellLearning the UNIX Operating System