User Shell Library

by Bob Orlando


The User Shell Library (USL) is a collection of some 69 shell functions intended for use primarily in Unix application development.1  These functions are intended to handle the tedious, mundane, and often overlooked details when scripting — things like ensuring that a given command actually works!  And if not, that it notifies those responsible upon failure. 

The library contains basic functions like those that change directory, file ownership and permissions; copy, touch, move, and remove files, etcetera, all with built in logging ability and optional error notification via email.  But that kind of functionality only involves 18 of the 69 functions in the USL.

In addition to the short list of "basic" functions listed above, the USL also contains some 49 other functions that go well beyond the mundane.  For example, such things as returning a given file's octal file permissions (for easy comparison), the running process' elapsed time, termination of jobs exceeding a specified run time limit, even exiting on process conflicts or if another process bearing the running process' name is already running.  Further, an additional file (not a true function), defaults.sh, serves as a template that the user can use to create his own 'defaults' script providing default assignments to critical shell library variables that are often unassigned by the user.

There are also routines for converting month numbers to names and vice versa, prompting an interactive yes or no reply, and even accepting standard input without it being echoed.  Date, time, numeric and integer validation, integer and character range generators, as well as file compression and removal for automagically maintaining files (including the running process' logged output) are also integral to the USL.  Further, there are some purely "cosmetic" functions that simply provide more readable output like formatting numbers with commas or framing a line of text within in a box to make it stand out in a log.  These are all included the USL suite.

Beyond the USL's robust functionality, the wide range of capabilities within the USL makes the individual functions themselves excellent teaching tools and code repositories providing well-documented "how to" examples of many Unix shell and Awk functions, as well as readable coding methods.  To assist the user with just how to use or call these functions, as well as how they work, a sample script (example.sh) is provided that calls each function.

USL functions are written in Bourne shell, meaning every function works equally well in Korn shell.  Developed over 10+ years, the USL was first used on the Sun Solaris platform, and while commands like Sun's ptree are not available on Linux (at least it wasn't when I developed them), the functions are still smart enough to know on what platform and under OS they are running, making most functions operational in both environments.  (Doubtless, with minor modification these functions will likely work on most any Unix-based, Command-line environment.)

A quick grasp of every function's role is as important as ease of use, so the following list of self-describing function names are really all that is needed to provide a fair overview of the USL's purpose or objective.

 chdir_exit_err
 chdir_exit_err_nomail
 check_tape_status
 chgrp_exit_err
 chgrp_exit_err_nomail
 chmod_exit_err
 chmod_exit_err_nomail
 chown_exit_err
 chown_exit_err_nomail
 cleanup_work_files
 compress_by_date
 cp_dev_null_exit_err
 cp_dev_null_exit_err_nomail
 cp_exit_err
 cp_exit_err_nomail
 debug_pause_loop
 defaults
 display_fs_size
 display_fs_space
 echo_exec
 elapsed_time
 email_msg
 exit
 exit_if_already_running
 exit_on_process_conflicts
 format_num_with_commas
 frame_text
 ftp_host
 get_file_perms
 get_file_yymmddhhmi
 get_next_yyyymmddhh
 get_prev_yyyymmddhh
 get_options
 isint
 isnum
 kill_pid
 leap_year
 mkdir_exit_err
 mkdir_exit_err_nomail
 month_mmm_to_nn
 month_nn_to_mmm
 mv_exit_err
 mv_exit_err_nomail
 parse_parameters
 pause_y_or_n
 ping_host
 print_vars
 prune_files
 ps_auxww
 range_nums
 range_values
 read_noecho
 readkey
 readkey_noecho
 remove_by_date
 rm_exit_err
 rm_exit_err_nomail
 show_documentation
 terminate
 terminate_subprocesses
 touch_exit_err
 touch_exit_err_nomail
 trap_exit
 validate_date
 validate_time
 verify_vars
 verify_num_vars
 wait_on_process
 write_err_to_syslogs

The Bourne shell functions in this library have been used/called by numerous scripts.  They are used because their code has been tested repeatedly.  While no code is bullet-proof, these functions come as close to bullet-resistant as possible. 

Each function has its own documentation section (located at the end of the function), and each checks to see if it has already been loaded into memory before loading itself again.  The design method assumes process logging is a requirement and everything works toward that end.

Functions are installed/declared/prototyped using their lowercased fileids (e.g.  "pause_y_or_n.sh"),  yet they are called or executed using their internal uppercased function names (e.g.  "PAUSE_Y_OR_N 'Continue? y|n [n]'").

Some functions return a status indicating success or failure.  Those, like the above  PAUSE_Y_OR_N,  are to be tested on completion.  In  PAUSE_Y_OR_N,  for example, returning "1" (one) means "yes," and "0" (zero), "no".  Other functions return string values.  For example, the command  mon=`MONTH_NN_TO_MMM 02`  assigns the string, "Feb", to the variable,  $mon.  All functions with the word "exit" in their names, exit on failure.  (What a concept, descriptive names!)

Functions that expect certain global variables will default those variables if unassigned.  Function dependencies are likewise handled.  For example, the  EMAIL_MSG  function will notify (echo or send mail) to the user if the variable  $notify  is appropriately assigned.  If the  EXIT  function has not already been declared,  EMAIL_MSG  will automagically declare it.  You may also use the  defaults.sh  to assign all defaults, then reassign any you wish (for example,  $log_root  and  $log  variables).

Internal Function variable names are scoped by prefixing a unique string to any of their internal variable names so no critical variable names are clobbered by other library functions.  For example, the prefix,  "CME_" identifies variables local to  CHMOD_EXIT_ERR  function.  A complete list of function prefixes followed by function usage briefs for each of the 69 functions presented here are found in  function_varname_prefix.lst.
 



Lastly, every Unix nerd knows that in UNIX there is never just one way to do anything.  These functions, therefore, are not cut in stone.  If anything, the concrete remains forever wet.  However, since I compiled and designed them, it remains my intent to maintain the functionality of those in this library.  That is, I will not change the essence of how a function works (returns output, expects input, or the like).  Replacement (and hopefully, improved) functions will be given new names thereby preserving the old routines for legacy/portability purposes.  

Footnotes:


  1. Of the 69 User Shell Library functions, 30 are actually called by a system administration application developed by yours truly called, janitor.sh.   [Return to reference point]

Artificial Intelligence is no match for natural stupidity.
©Copyright Bob Orlando, 1995-2014
All rights reserved.
http://www.OrlandoKuntao.com
E-mail: Bob@OrlandoKuntao.com
Last update: Mar. 3, 2014
by Bob Orlando