Home

scriptt.sh




Purpose

scriptt.sh is a template for Kornshell scripts.

scriptt.sh includes many predefined variables and functions (e.g. for string handling, user id handling, log file handling, house keeping routines, trap handler, etc).  See the documentation for additional information.

scriptt.sh also includes a powerful single step debugger.

Update 05.08.2007:

There is now an article describing some of the techniques used in scriptt.sh on BigAdmin

A Script Template and Useful Techniques for ksh Scripts

http://www.sun.com/bigadmin/content/submitted/ksh_script_template.jsp

see also the comments for the script template in the wiki:

http://wikis.sun.com/display/BigAdmin/A+Script+Template+and+Useful+Techniques+for+ksh+Scripts?focusedCommentId=12517653#comment-12517653




Back to top

License


# CDDL HEADER START
#
# The contents of this file and the script are subject to the terms of the
# Common Development and Distribution License, Version 1.0 only
# (the "License").  You may not use this file except in compliance
# with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END


Back to top

History


Version
Releasedate
         Description
1.22.33
12.02.2009
##   1.22.31 18.03.2008 /bs
##     added the version number to the start and end messages
##     an existing config file is now removed (and not read) if the script is called with -C to create a config file
##
##   1.22.32 04.04.2008 /bs
##     minor changes for zone support
##
##   1.22.33 12.02.2009 /bs
##     disabled the usage of prtdiag due to the fact that prtdiag on newer Sun machines needs a long time to run
##     (-> __MACHINE_SUBTYPE is now always empty for Solaris machines)
##     added the variable     __CONFIG_FILE_FOUND; this variable contains the name of the config file
##     read if a config file was found
##     added the variable __CONFIG_FILE_VERSION

1.22.30 28.02.2008 /bs ##   1.22.28 12.01.2008 /bs
##      corrected a syntax error in the show usage routine
##      added the function PrintWithTimestamp (see credits above)
##
##   1.22.29 31.01.2008 /bs
##      there was a bug in the new code to remove the lockfile which prevented
##      the script from removing the lockfile at program end
##      if the lockfile already exist the script printed not the correct error
##      message
##
##   1.22.30 28.02.2008 /bs
##      Info update: executeCommandAndLog does NOT return the RC of the executed
##      command if a logfile is defined
##      added inital support for CYGWIN
##      (tested with CYGWIN_NT-5.1 v..1.5.20(0.156/4/2)
##     Most of the internal functions are NOT tested yet in CYGWIN
##      GetCurrentUID now supports UIDs greater than 254; the function now prints the UID to STDOUT 
##      Corrected bug in GetUserName (only a workaround, not the solution)
##      now using printf in the AskUserRoutine

1.22.27
29.12.2007 /bs
##
##   1.22.24 05.10.2007 /bs
##      another minor fix for ksh93 compatibility
##
##   1.22.25 08.10.2007 /bs
##      only spelling errors corrected
##
##   1.22.26 19.11.2007 /bs
##      only spelling errors corrected
##
##   1.22.27 29.12.2007 /bs
##      improved the code to create the lockfile (thanks to wpollock for the info; see
##     credits above)
##      improved the code to create the temporary files (thanks to wpollock for the info; see
##      credits above)
##      added the function rand (thanks to wpollock for the info; see credits above)
##      the script now uses the directory name saved in the variable $TMPDIR for temporary files 
##      if it's defined
##      now the umask used for creating temporary files can be changed (via variable __TEMPFILE_UMASK)

see this wiki entry

1.22.23 25.09.2007 /bs    1.22.20 12.09.2007 /bs
      the script now checks the ksh version if running on Solaris
      made some changes for compability with ksh93

   1.22.21 18.09.2007 /bs (BigAdmin Version 2)
      added the variable __FINISHROUTINES
      changed __REQUIRED_ZONE to __REQUIRED_ZONES
      added the variable __KSH_VERSION
      reworked the trap handling

   1.22.22 23.09.2007 /bs
      added the signal handling for SIGUSR1 and SIGUSR2 (variables __SIGUSR1_FUNC and __SIGUSR2_FUNC)
      added user defined function for the signals HUP, BREAK, TERM, QUIT, EXIT, USR1 and USR2
     added the variables __WARNING_PREFIX, __ERROR_PREFIX,  __INFO_PREFIX, and __RUNTIME_INFO_PREFIX
      the parameter -T or --tee can now be on any position in the parameters
      the default output file if called with -T or --tee is now
        /var/tmp/${0##*/}.$$.tee.log

   1.22.23 25.09.2007 /bs
      added the environment variables __INFO_PREFIX, __WARNING_PREFIX,
      __ERROR_PREFIX, and __RUNTIME_INFO_PREFIX
      added the environment variable __DEBUG_HISTFILE
      reworked the function to print the usage help :
      use "-h -v" to view the extented usage help and use "-h -v -v" to
      view the environment variables used also

1.22.19
08/04/2007
   1.22.16 05.07.2007 /bs
      enhanced initial support for other Operating Systems
      Support for other OS is still not fully tested!

   1.22.17 06.07.2007 /bs
      added the global variable __TRAP_SIGNAL

   1.22.18 01.08.2007 /bs
      __OS_VERSION and __OS_RELEASE were not set - corrected

   1.22.19 04.08.2007 /bs
      wrong function used to print "__TRAP_SIGNAL is \"${__TRAP_SIGNAL}\"" - fixed

1.22.15
11/23/2006
   1.22.11 19.10.2006 /bs
      corrected a minor bug in AskUser (/c was not interpreted by echo)
      corrected a bug in the handling of the parameter -S (-S was ignored)

   1.22.12 31.10.2006 /bs
      added the variable __REQUIRED_ZONE

   1.22.13 13.11.2006 /bs
      the template now uses TMP or TEMP if set for the temporary files

   1.22.14 14.11.2006 /bs
      corrected a bug in the function AskUser (the default was y not n)

   1.22.15 21.11.2006 /bs
      added initial support for other Operating Systems
      corrected some bugs in the debug handler

1.22.10
09/20/2006
   1.22.4 06.07.2006 /bs
      corrected a bug in the parameter error handling routine

   1.22.5 27.07.2006 /bs
      corrected some minor bugs

   1.22.6 09.08.2006 /bs
      corrected some minor bugs

   1.22.7 17.08.2006 /bs
      add the CheckParameterCount function
      added the parameter -T
      added long parameter support (e.g --help)

   1.22.8 07.09.2006 /bs
      added code to save the env variable LANG and set it temporary to C

   1.22.9 20.09.2006 /bs
      corrected code to save the env variable LANG and set it temporary to C
 
   1.22.10 21.09.2006 /bs
      cleanup comments
      the number of temporary files created automatically is now variable
        (see the variable __NO_OF_TEMPFILES)
      added code to install the trap handler in all functions

1.22.3
06/24/2006
   1.22.2. 21.06.2006 /bs
      added the parameter -V
      added the use of environemnt variables
      added the variable __NO_TIME_STAMPS
      added the variable __NO_HEADERS
      corrected a bug in the function executeCommandAndLogSTDERR
      added missing return commands

   1.22.3 24.06.2006 /bs
      added the function StartStop_LogAll_to_logfile
      added the variable __USE_TTY (used in AskUser)
      corrected an spelling error (dev/nul instead of /dev/null)
1.20.0
08/01/2005
some enhancements and bug fixes
1.18.2
03/18/2005 minor bug fixes
1.18.0
02/02/2005


Back to top

Operating system

Solaris (Linux, AIX)


Back to top

Language / type

Kornshell Script


Back to top

Prerequisites

n/a


Back to top

Usage

Predefined parameter:

xtrnaw7@T30:/data/develop/scripts$ bg
[1]+ gedit scriptt.sh &
xtrnaw7@T30:/data/develop/scripts$ ./scriptt.sh -v -v -h
[27.09.2007 19:34:07] scriptt.sh started on Thu Sep 27 19:34:07 CEST 2007
[27.09.2007 19:34:07] Reading the config file "./scriptt.conf" ...
[27.09.2007 19:34:07] RUNTIME INFO: Parameter after processing the default parameter are:  "-v -v -h"
  scriptt.sh v0.0.0.1 - ??? short description ???

  Usage: scriptt.sh [-T] [-v|+v] [-q|+q] [-h] [-l logfile|+l] [-y|+y] [-n|+n]
                    [-D|+D] [-a|+a] [-O|+O] [-f|+f] [-C] [-H] [-S n] [-V]

                    [??? add additional parameter here ???]



 Note: Use -{switch} or --{longswitch} to turn an option on;
       use +{switch} or ++{longswitch} to turn an option off

       The long format of the parameter (--parameter/++parameter) is not supported by all ksh implementations


    Parameter:

      -v|+v - turn verbose mode on/off; current value: y
              Long format: --verbose / ++verbose
      -q|+q - turn quiet mode on/off; current value: n
              Long format: --quiet / ++quiet
      -h    - show usage
              Long format: --help
      -l    - set the logfile
              current value: /var/tmp/scriptt.LOG
              Long format: --logfile
      +l    - do not write a logfile
              Long format: ++logfile
      -y|+y - assume yes to all questions or not
              Long format: --yes / ++yes
      -n|+n - assume no to all questions or not
              Long format: --no /++no
      -D|+D - run main in single step mode (and turn colors on); current value: n
              Long format: --debug / ++debug
      -a|+a - turn colors on/off; current value: n
              Long format: --color / ++color
      -O|+O - overwrite existing files or not; current value: n
              Long format: --overwrite / ++overwrite
      -f|+f - force; do it anyway; current value: n
              Long format: --force / ++force
       -C   - write a default config file in the current directory and exit
              Long format: --writeconfigfile
      -S n  - print error/warning summaries:
              n = 0 no summarys, 1 = print error msgs,
              2 = print warning msgs, 3 = print error and warning mgs
              Current value: 0
              Long format: --summaries
      -H    - write extended usage to STDERR and exit
              Long format: --doc
      -V    - write version number to STDOUT and exit
              Long format: --version
      -T    - append STDOUT and STDERR to the file "/var/tmp/scriptt.sh.8225.tee.log"
              Note: This parameter must be the FIRST parameter if used!
              Long format: --tee

      [??? add additional parameter here ???]


Environment variables that are used if set:

  __DEBUG_CODE (Current value: "")
  __RT_VERBOSE_LEVEL (Current value: "1")
  __QUIET_MODE (Current value: "1")
  __VERBOSE_MODE (Current value: "0")
  __VERBOSE_LEVEL (Current value: "2")
  __OVERWRITE_MODE (Current value: "1")
  __USER_BREAK_ALLOWED (Current value: "0")
  __NO_TIME_STAMPS (Current value: "1")
  __NO_HEADERS (Current value: "1")
  __USE_COLORS (Current value: "1")
  __USE_RBAC (Current value: "1")
  __TEE_OUTPUT_FILE (Current value: "/var/tmp/scriptt.sh.8225.tee.log")
  __INFO_PREFIX (Current value: "INFO:")
  __WARNING_PREFIX (Current value: "WARNING:")
  __ERROR_PREFIX (Current value: "ERROR:")
  __RUNTIME_INFO_PREFIX (Current value: "RUNTIME INFO:")
  __DEBUG_HISTFILE (Current value: "/tmp/ksh.history.8225")
[27.09.2007 19:34:07] The log file used was "/tmp/scriptt.sh.8225.TEMP"
[27.09.2007 19:34:07] scriptt.sh ended on Thu Sep 27 19:34:07 CEST 2007.
[27.09.2007 19:34:07] The RC is 1.
xtrnaw7@T30:/data/develop/scripts$

see also the documentation


Back to top

Examples

easycd.sh is based on scriptt.sh

Screen shots from the debugger included in scriptt.sh:

scriptt.sh debugger - screen shot

scriptt.sh debugger - screen shot


Back to top

Notes

You must add your own code to scriptt.sh - without it, it is not very useful.


Back to top

Download

Download scriptt.sh

Download script_test.sh

Output of scriptt.sh -H


Back to top