Home About BC DR HA Support Training Download
You are here: Home/ Downloads/ Scripts/ Korn/ Library/ Please Login or Register

Current Location


 Mt Xia Inc.
BOOK:Advanced Shell Scripting

 Join our LinkedIn Group
AIX Advanced Technical Experts
Contract Opportunities

 Train By Tweet
Training courses Tweet By Tweet
Various Subjects and Products

AIX Admin Methodology
Global Consolidation Project
All AIX admins should join

digg Digg this page
del.icio.us Post to del.icio.us
Slashdot Slashdot it!

Function Libraries


How to create a Shell Script Function Library

A Shell Script Function Library is a reusable source of functions, routines, and procedures which can be utilized by the script programmer.

Shell Script Function Libraries have the following advantages:

  • Maintenance and updates of the functions in the library are easier. They only need to be performed in a single file, instead of updating each standalone script that include the function.
  • Another advantage is that it Saves memory. Functions are loaded only when they are needed and many scripts can use a library function simultaneously.
  • Saves disk space. Many scripts can share a single copy of a library function on disk.
  • But certainly, the biggest advantage of a Shell Script Function Library is standardization of code. Every shell script that utilizes the library calls the same functions from the same library.

VIDEO: How to create a Shell Script Function Library:

The first step in creating a function library is to create a directory to contain the functions.

This directory can exist anywhere, for example: each user can have their own function library.

But if the functions are to be shared between multiple users, a centralized location may be desirable, such as "/usr/local/functions."

For this procedure create a directory for the function library under your home directory. Name the directory "functions"

  mkdir  ~/functions
  ls -ld ~/functions

Change the permissions on the directory to make it publicly accessible so that others may use the functions from your function library, but make it so others cannot modify it or the scripts in the directory.

  chmod 755 ~/functions   
  ls -ld ~/functions   

Now create the functions in the function library. To do this copy your shell script containing functions to a file in the library directory, and name the file using the function name. If more than one function is in the file, copy it for each function, naming each file according to the function names.

For example, assume there is a standalone shell script called "displaymsg.ksh" which contains the following three functions:

  • function usagemsg_displaymsg
  • function configure_displaymsg
  • function displaymsg

Copy the standalone script file into the function library directory, once for each function in the script:

  cp displaymsg.ksh ~/functions/usagemsg_displaymsg 
  cp displaymsg.ksh ~/functions/configure_displaymsg
  cp displaymsg.ksh ~/functions/displaymsg

Now edit each of the files in the function library and remove everything from the file except the function matching the file name. When finished, each file should contain one function and the function name should be on the first line of the file. The function name should match the file name.

  vi ~/functions/usagemsg_displaymsg 
  vi ~/functions/configure_displaymsg
  vi ~/functions/displaymsg   

The "function" declaration line needs to be the first line in each file, and the SHEBANG line "#!/..." should be removed.

Now create a new shell script that will utilize functions from the library.

  vi ~/tmp.sh

In this new shell script, reference the function library using the "FPATH" environment variable. For example, to call the "displaymsg" function from the function library in our home directory, the new shell script might look like this:

  export FPATH=~/functions:/usr/local/functions
  displaymsg "${@}"
  exit ${?}

The FPATH variable is used just like the normal PATH variable, so multiple function libraries can be listed, separated by a colon ":". Furthermore, the FPATH environment variable could be defined in your personal ".profile" file, "/etc/profile", or "/etc/environment", and then it would not be necessary to specify it in the "~/tmp.sh" shell script at all.

If the FPATH variable is defined in your personal .profile, "/etc/profile", or "/etc/environment", the ~/tmp.sh could be changed as follows:

  displaymsg "${@}"
  exit ${?}

And if you want the ~/tmp.sh shell script to exit with the return code from the function "displaymsg", you could change the shell script further as follows:

  displaymsg "${@}"

The function "displaymsg" is called from the tmp.sh script and all command line arguments are passed to the function using the double quoted dollar at-sign variable "${@}", each command line argument double quoted.

Finally the "tmp.sh" script exits with the return code from the function "displaymsg". The variable ${?} contains the exit code of the last executed command, so the command "exit ${?}" will cause the "tmp.sh" script to exit with the return code of the "displaymsg" function.

Now make the file ~/tmp.sh executable and run it.

  chmod 755 ~/tmp.sh

The function "displaymsg" is executed from the function library after being called from the script ~/tmp.sh.


Function Libraries