本站内容有下面分类知识,欢迎您的到来^_^

shell相关:指令篇 基础篇 脚本欣赏 编程实例 shell问问 shell视频教程 技巧篇 水平测试 E文资料 vi编辑器 高级Bash脚本编程指南
其他:mysql perl c语言

设soyo123为首页 收藏本站
当前位置:|主页>shell相关E文资料>

#23 A Reminder Utility

百度收藏 QQ搜藏

Windows and Mac users for years have appreciated simple utilities like Stickies and Post-It, streamlined applications that let you have tiny reminder windows stuck on your screen. They're a perfect place to jot down a phone number or other reminder. If you're at the Unix command line, there's nothing analogous, yet the problem is quite easily solved, as shown in this pair of scripts.

The first script, remember, lets you easily file random snippets of information into a file. If invoked without any arguments, it reads standard input until the end of the file, and if invoked with arguments, it saves those arguments to the data file instead.

The other half of this duo is remindme, a companion shell script that either displays the contents of the rememberfile if no arguments are given or, if an argument is given, searches in this file for the specified pattern.

The Code
#!/bin/sh

# remember - An easy command-line-based memory pad.

rememberfile="$HOME/.remember"

if [ $# -eq 0 ] ; then
  echo "Enter note, end with ^D: "
  cat - >> $rememberfile
else
  echo "$@" >> $rememberfile
fi

exit 0

Here's the second script, remindme:

#!/bin/sh

# remindme - Searches a data file for matching lines, or shows the entire contents
#   of the data file if no argument is specified.

rememberfile="$HOME/.remember"

if [ $# -eq 0 ] ; then
  more $rememberfile
else
  grep -i "$@" $rememberfile | ${PAGER:-more}
fi

exit 0


Running the Scripts
To use the remindme utility, first add notes, phone numbers, or anything else to the rememberfile with the remember script. Then search this freeform database with remindme, specifying as long or short a pattern as you'd like.

The Results
$ remember
Enter note, end with ^D:
The Boulder Community Network: http://bcn.boulder.co.us/
^D

Then, when I want to remember that note, months later:

$ remindme boulder
The Boulder Community Network: http://bcn.boulder.co.us/

Or if I need any other data that might be in there:

$ remindme 800
Southwest Airlines: 800-IFLYSWA

Hacking the Script
While certainly not any sort of shell script programming tour de force, these scripts neatly demonstrate the incredible extensibility of the Unix command line. If you can envision something, the odds are good that there's a simple and straightforward way to accomplish it.

These scripts could be improved in any number of ways. For instance, you could create the concept of records such that each record is time-stamped and multiline input is saved as a single entity that can be searched with a regular expression, which would enable you to store phone numbers for a group of people and retrieve them all by remembering the name of only one person in the group. If you're really into scripting, you might also want to include edit and delete capabilities. Then again, it's pretty easy to edit the ~/.remember file by hand.


上一篇:#22 Displaying the Time in Different Time Zones 下一篇:#24 An Interactive Calculator
power by soyo123 2007-2008