de5@ornl.gov (Dave Sill) (01/30/91)
Disclaimer: provided without warranty as a public service. Use at your own risk. Send comments/additions to de5@ornl.gov. --8<--cut-here---- #!/bin/sh # # NAME # whats - lookup acronym/abbreviation in database # # SYNOPSIS # whats [acronym [defn...]] # # DESCRIPTION # Whats, without any arguments, prompts for an acronym to look up. # # With one argument, whats prints all entries from the database # that match the argument. If no matches are found, whats # prompts for expansion of the acronym or abbreviation. # # With more than one argument, whats scans the database for # entries matching the first argument. If any matches are found, # whats prints the matching entries and asks if the remaining # arguments should be added as a new entry. If no matches are # found, a new entry is created for the first argument with the # remaining arguments forming the expansion. # # FILES # acron or $ACRON # # BUGS/DEFICIENCIES # Not blindingly fast. # Not tested under System V. (Uses "echo -n", "grep -i") # # AUTHOR # Dave Sill (dsill@nswc-oas.arpa) (de5@ornl.gov as of 6/90) # Naval Surface Warfare Center (NSWC), Dahlgren, VA # n='-n' # Use '' if your echo uses '\c' c='' # Use '\c' if your echo uses it q='-i' # Use '' or your grep's "ignore case" flag if [ -z "$1" ] then echo $n "What is what? $c" read ac else ac=$1 fi ac=`echo ${ac} | tr "[a-z]" "[A-Z]"` if [ ! -f acron ] then acron=${ACRON} else acron=acron fi i=`grep $q -c "^${ac} " ${acron}` if [ -z "$2" -a $i -ne 0 ] then # No definition passed and is on file, so just do a lookup. grep $q "^${ac} " ${acron} exit elif [ $i -ne 0 ] then # Is on file and a definition was passed, check before adding. echo $i occurrences found. grep $q "^${ac} " ${acron} echo $n "Still want to add ${ac}? $c" read reply if [ "${reply}" = n -o "${reply}" = N ] then exit fi fi if [ -z "$2" ] then echo $n "What does ${ac} stand for? $c" read def if [ -z "${def}" ] then exit fi else shift def=$* fi echo "${ac} - ${def}" >>${acron} echo "${ac} - ${def}" --8<--cut-here---- -- Dave Sill (de5@ornl.gov) It will be a great day when our schools have Martin Marietta Energy Systems all the money they need and the Air Force Workstation Support has to hold a bake sale to buy a new bomber.
composer@chem.bu.edu (Jeff Kellem) (02/03/91)
Dave, Thanks for posting the acronym database stuff. For those that have Perl, below is a quick rewrite of Dave's acronym lookup tool in Perl. It's not fancy, just a quick translation to Perl. Same logic, etc.. The perl script is also it's own man page (thanks to Larry Wall for the nroff/perl trickery). The only thing I added was to look for the acronyms database in a few default locations, along with having the ACRON environment variable override the defaults. The Perl code could look better, but hey.. it's just a quick translation. ;-) Enjoy... -jeff Jeff Kellem Internet: composer@chem.bu.edu ===CUT HERE===whats (in Perl)=== #!/usr/local/bin/perl 'di'; 'ig00'; # # file: whats (or whats.pl or whats.perl) # # usage: whats [ acronym [defn...]] # See man page after __END__ line # $ACRON = $ENV{'ACRON'} || (-f '/usr/local/lib/acronyms' && '/usr/local/lib/acronyms') || (-f './acronyms' && './acronyms') || (-f './acron' && './acron'); ($me = $0) =~ s|.*/||; die "$me: Can't find acronym file\n" unless $ACRON; $lookup = shift || &input('What is what? '); $lookup =~ tr/a-z/A-Z/; $def = join(" ", @ARGV); # gather up rest of args open ACRON || die "$me: Can't open acronym file: $!\n"; while (<ACRON>) { next unless /^$lookup\s*-/o; push(@found, $_); # or could do $found .= $_; $count++; } close ACRON; if (!$def && @found) { print @found; exit; } elsif (@found) { # or $#found+1 print scalar(@found), " occurrences found.\n", @found; if (&input("Still want to add $lookup? ") !~ /^y$/oi) { exit; } } $def = $def || &input("What does $lookup stand for? ") || exit; $lookup = sprintf("%-8s- $def", $lookup); open(ACRON, ">> $ACRON") || die "$me: Can't append to $ACRON: $!\n"; print ACRON "$lookup\n"; close ACRON; print "$lookup\n"; sub input { print @_[0]; chop($_ = <STDIN>); $_; } ############################################################### # These next few lines are legal in both Perl and nroff. .00; # finish .ig 'di \" finish diversion--previous line must be blank .nr nl 0-1 \" fake up transition to first page again .nr % 0 \" start at page 1 '; __END__ ##### From here on it's a standard manual page ##### .TH WHATS 1 "February 2, 1991" .AT 3 .SH NAME whats \- lookup acronym/abbreviation in database .SH SYNOPSIS .B whats [acronym [defn...]] .SH DESCRIPTION .IR Whats , without any arguments, prompts for an acronym to look up. With one argument, .I whats prints all entries from the database that match the argument. If no matches are found, .I whats prompts for expansion of the acronym or abbreviation. With more than one argument, .I whats scans the database for entries matching the first argument. If any matches are found, .I whats prints the matching entries and asks if the remaining arguments should be added as a new entry. If no matches are found, a new entry is created for the first argument with the remaining arguments forming the expansion. .SH ENVIRONMENT .IP ACRON The location of the acronym database. Overrides any default locations. .SH FILES The acronym database is searched for in the following order: $ACRON /usr/local/lib/acronyms ./acronyms ./acron .SH AUTHOR Original shell script written by: .br Dave Sill (dsill@nswc-oas.arpa) (de5@ornl.gov as of 6/90) Naval Surface Warfare Center (NSWC), Dahlgren, VA .PP Perl translation written by: .br Jeff Kellem (composer@chem.bu.edu), 2 Feb 1991 .SH "SEE ALSO" .SH BUGS Not blindingly fast. The Perl version should work on any system that runs Perl. .PP The Perl version is basically just a straight translation of the original shell script. .PP The Perl version should probably be reorganized a tiny bit.
composer@chem.bu.edu (Jeff Kellem) (02/04/91)
In article <COMPOSER.91Feb2145029@chem.bu.edu> I wrote... > sub input { > print @_[0]; > chop($_ = <STDIN>); > $_; > } Oops.. darn typos. That first line should have been "print $_[0];". It was originally set to print the entire arg list to &input. Both will work, though. -jeff Jeff Kellem Internet: composer@chem.bu.edu