Mt Xia: Technical Consulting Group

Business Continuity / Disaster Recovery / High Availability
Data Center Automation / Audit Response / Audit Compliance

-
Current Location
-

css
  Downloads
    Scripts
      Korn

-

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


Business Web Site Hosting
$3.99 / month includes Tools,
Shopping Cart, Site Builder

www.siteox.com

FREE Domain Registration
included with Web Site Hosting
Tools, Social Networking, Blog

www.siteox.com

The following script provides several examples of using "discipline" funtions within Korn Shell 93.


#!/usr/bin/ksh93
################################################################
# A discipline function is a function that is associated with a specific
# variable. This allows you to define and call a function every time that
# variable is referenced, set, or unset. These functions take the form of
# varname.function, where varname is the name of the variable and function
# is the discipline function. The predefined discipline functions are get,
# set, and unset.
# 
#     * The varname.get function is invoked every time varname is
#     referenced. If the special variable .sh.value is set within this
#     function, then the value of varname is changed to this value. 
#     A simple example is the time of day:
# 
# $ function time.get 
# > { 
# >     .sh.value=$(date +%r) 
# > } 
# $ print $time 
# 09:15:58 AM 
# $ print $time    # it will change in a few seconds 
# 09:16:04 AM
# 
#     * The varname.set function is invoked every time varname is set. The
#     .sh.value variable is given the value that was assigned. The value
#     assigned to varname is the value of .sh.value when the function
#     completes. For example:
# 
# $ function adder.set 
# > { 
# >   let .sh.value="
# $ {.sh.value} + 1" 
# > } 
# $ adder=0 
# $ echo $adder 
# 1 
# $ adder=$adder 
# $ echo $adder 
# 2  
# 
#     * The varname.unset function is executed every time varname is
#     unset. The variable is not actually unset unless it is unset 
#     within the function itself; otherwise it retains its value.
# 
# Within all discipline functions, the special variable .sh.name is set to
# the name of the variable, while .sh.subscript is set to the value of the
# variables subscript, if applicable.
# 
# Compound variables are supported. A compound variable allows a user to
# specify multiple values within a single variable name. The values are
# each assigned with a subscript variable, separated from the parent
# variable with a . (period). For example:
# 
# $ myvar=( x=1 y=2 ) 
# $ print "${myvar.x}" 
# 1
# 
################################################################
function DATETIME.get
{ 
  print "\ninside the DATETIME.get function"
    print ".sh.name=${.sh.name}"
    print ".sh.value=${.sh.value}"
    print ".sh.subscript=${.sh.subscript}"
    .sh.value=$( printf "%(%c)T\n" )
} 
################################################################
function DATETIME.set
{
  print "\ninside the DATETIME.set function"
    print ".sh.name=${.sh.name}"
    print ".sh.value=${.sh.value}"
    print ".sh.subscript=${.sh.subscript}"
}
################################################################
function DATETIME.unset
{
  print "\ninside the DATETIME.unset function"
    print ".sh.name=${.sh.name}"
    print ".sh.value=${.sh.value}"
    print ".sh.subscript=${.sh.subscript}"
}
################################################################

DATETIME="test"

print "${DATETIME}"

unset DATETIME

-
Example Discipline Functions
-
 


FREE Domain Registration
included with Web Site Hosting
Tools, Social Networking, Blog

www.siteox.com

Business Web Site Hosting
$3.99 / month includes Tools,
Shopping Cart, Site Builder

www.siteox.com