Home

scriptt.sh v1.x




Note

This is the short documentation for scriptt.sh v1.x . This version is not updated anymore. The current version of scriptt.sh is v2.x.


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). 

scriptt.sh also includes a powerful single step debugger.

Detailed information about scriptt.sh including usage information are here: Detailed documentation for scriptt.sh.


Update 25.07.2011:

The links to www.sun.com do not work anymore. I did not find the new location (if any) for these documents on www.oracle.com


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

Note 16.10.2011: The link above does not work anymore.

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


The 1.x version of scriptt.sh is NOT updated anymore.
The current version of scriptt.sh is v2.x
1.22.46
27.04.2013
####  1.22.46 27.04.2013 /bs
####     executeCommandAndLog rewritten using coprocesses (see also credits)
####     Info update: executeCommandAndLog does now return the RC of the executed
####                         command even if a logfile is defined
1.22.45
07.06.2012
####  1.22.45 07.06.2012 /bs
####     added code to check if the symbolic link for the lockfile already exists before creating
####     the lock file
1.22.44
22.04.2012
####  1.22.44 22.04.2012
####     The script now uses nawk only if available (if not awk is used)
####     variables are now supported in the usage examples (prefixed with ##EXAMPLE##)
####     add a line with the current date and time to variable dumps, e.g.
####
####         ### /var/tmp/scriptt.sh.exported_envvars.dump_no_0_20074 - exported environment variable dump created on Sun Apr 22 11:35:38 CEST 2012
####
####         ### /var/tmp/scriptt.sh.envvars.dump_no_0_20074 - environment variable dump created on Sun Apr 22 11:35:38 CEST 2012
####     added experimental interactive mode to the signal handler for USR2
####     replaced /usr/bin/echo with printf
####     added the variable LOGMSG_FUNCTION
1.22.43
15.10.2011
####  1.22.43 15.10.2011
####    added support for disabling the config file feature CONFIG_FILE=none ./scriptt.sh
####    corrected a minor bug in SaveEnvironmentVariables
####    corrected a bug in the function SaveEnvironmentVariables
####    corrected a bug in getting the value for the variable ${__ABSOLUTE_SCRIPTDIR}
####
1.22.42
05.10.2011
####  1.22.41 26.09.2011
####    added the parameter -X
####    disabled some ksh93 code because "ksh -x -n" using ksh88 does not like it
####
####  1.22.42 05.10.2011
####   added the function PrintDotToSTDOUT

1.22.40 25.07.20111
####  1.22.39 24.07.2011
####    __INIT_FUNCTION now enable for cygwin also
####    __SHELL did not work in all Unixes - fixed
####    __OS_FULLNAME is now also set in Solaris and Linux
####
####  1.22.40 25.07.2011
####    added some code for ksh93 (functions: substr)
####    Note: set __USE_ONLY_KSH88_FEATURES to ${__TRUE} to suppress using the ksh93 features
####    The default action for the signal handler USR1 is now "Create an env dump in /var/tmp"
####    The filenames for the dumps are
####       
####      /var/tmp/<scriptname>.envvars.dump_no_<no>_<PID>
####      /var/tmp/<scriptname>.exported_envvars.dump_no_<no>_<PID>
####
####    where <no> is a sequential number, <PID> is the PID of the process with the script,
####    and <scriptname> is the name of the script without the path.
####
1.22.38
22.07.2011
####  1.22.38 22.07.2011
####    added code to make the trap handling also work in bash
####    added a sample user defined trap handler (function USER_SIGNAL_HANDLER)
####    added the function SetHousekeeping to enabe or disable house keeping
####    scriptt.sh did not write all messages to the logfile if a relative filename was used - fixed
####    added more help text for "-v -v -v -h"
####    now user defined signal handler can have arguments
####    the RBAC feature (__USE_RBAC) did not work as expected - fixed
####    added new scriptt testsuite for testing the script template on other OS and/or shells
####    added the function SaveEnvironmentVariables
####
1.22.37
08.07.2011
##  1.22.37 08.07.2011
##    corrected a minor error with the QUIET parameter
##    added code to dump the environment (env var __CREATE_DUMP, function CreateDump )
##    implemented work around for missing function whence in bash
##    added the function LogIfNotVerbose
##
1.22.36
11.04.2009
##   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.34 28.02.2009 /bs
##     added code to check for the max. line no for the debug handler
##    (an array in ksh88 can only handle up to 4096 entries)
##    added the variable __PIDFILE
##
##  1.22.35 06.04.2009 /bs
##     added the variables
##       __NO_CLEANUP
##       __NO_EXIT_ROUTINES
##       __NO_TEMPFILES_DELETE
##       __NO_TEMPMOUNTS_UMOUNT
##       __NO_TEMPDIR_DELETE
##       __NO_FINISH_ROUTINES
##       __CLEANUP_ON_ERROR
##       CONFIG_FILE
##
##  1.22.36 11.04.2009 /bs
##    corrected a cosmetic error in the messages (wrong: ${TEMPFILE#} correct: ${__TEMPFILE#})
##

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, Windows with Cygwin)


Back to top

Language / type

Kornshell Script (developed for ksh88; should also run under ksh93 or bash)


Back to top

Prerequisites

n/a


Back to top

Usage

Predefined parameter:

xtrnaw7@t61p:/data/develop/scripts$ ./scriptt.sh -v -v -h
[14.10.2011 19:36:01] scriptt.sh v0.0.1 started on Fri Oct 14 19:36:01 CEST 2011 
[14.10.2011 19:36:01] Reading the config file "/data/develop/scripts/scriptt.conf" ...
[14.10.2011 19:36:01] RUNTIME INFO: Parameter after processing the default parameter are:  "-v -v -h" 
  scriptt.sh v0.0.1 - ??? short description ???

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

                    [??? 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
      -H    - write extended usage to STDERR and exit
              Long format: --doc
      -X    - write usage examples to STDERR and exit
              Long format: --view_examples
      -S n  - print error/warning summaries:
              n = 0 no summariess, 1 = print error msgs,
              2 = print warning msgs, 3 = print error and warning mgs
              Current value: 0
              Long format: --summaries
      -V    - write version number to STDOUT and exit
              Long format: --version
      -T    - append STDOUT and STDERR to the file "/var/tmp/scriptt.sh.30291.tee.log"
              Long format: --tee

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


Environment variables that are used if set (0 = TRUE, 1 = FALSE):

  __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")
  __RBAC_BINARY (Current value: "/usr/bin/pfexec")
  __TEE_OUTPUT_FILE (Current value: "/var/tmp/scriptt.sh.30291.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: "")
  __NO_CLEANUP (Current value: "1")
  __NO_EXIT_ROUTINES (Current value: "1")
  __NO_TEMPFILES_DELETE (Current value: "1")
  __NO_TEMPMOUNTS_UMOUNT (Current value: "1")
  __NO_TEMPDIR_DELETE (Current value: "1")
  __NO_FINISH_ROUTINES (Current value: "1")
  __CLEANUP_ON_ERROR (Current value: "1")
  __CREATE_DUMP (Current value: "")
  __DUMP_ALREADY_CREATED (Current value: "")
  __DUMPDIR (Current value: "")
  __USE_ONLY_KSH88_FEATURES (Current value: "1")
  CONFIG_FILE (Current value: "")
[14.10.2011 19:36:01] The log file used was "/tmp/scriptt.sh.30291.TEMP" 
[14.10.2011 19:36:01] scriptt.sh v0.0.1 ended on Fri Oct 14 19:36:01 CEST 2011.
[14.10.2011 19:36:01] The RC is 1.
 


see also the documentation


Back to top

Examples

easycd.sh is based on scriptt.sh

Screenshots from the debugger included in scriptt.sh:

scriptt.sh debugger - screen shot

scriptt.sh debugger - screen shot


Back to top 

Scriptt test suite


Beginning with version 1.22.38 there is now also a test suite for scriptt.sh: scriptt_testsuite.sh

Using this test suite you can test if scriptt.sh is properly running on other Operating systems and/or other shells.

The test suite tests

To use the test suite uncomment or add the following lines to scriptt.sh:

  set --
  .  ./scriptt_testsuite.sh


(scriptt_testsuite.sh must be in the current directory to make this work)

An example output of scriptt_testsuite for ksh in Solaris is here:

scriptt_testsuite output


Back to top

Scriptt Test Suite Results


OS
Shell
Date
scriptt version
TestSuite version
Result
Tests failed
Solaris x86 U9
Generic_142910-17
/usr/bin/ksh
24.07.2011
1.22.39 24.07.2011
2.0.0 17.07.2011/bs
all ok
-
Solaris x86 U9
Generic_142910-17
/usr/db/bin/dtksh
24.07.2011
1.22.39 24.07.2011 2.0.0 17.07.2011/bs mostly ok
long_parameter_test
Solaris x86 U9
Generic_142910-17
/usr/xpg4/bin/sh
24.07.2011
1.22.39 24.07.2011 2.0.0 17.07.2011/bs mostly ok long_parameter_test
Solaris x86 U9
Generic_142910-17
/usr/bin/bash
24.07.2011
1.22.39 24.07.2011 2.0.0 17.07.2011/bs not ok
long_parameter_test
syntax_error_test_004
tee_test_001
Linux 2.6.32-33-generic-pae
#70-Ubuntu SMP Thu Jul 7 22:51:12 UTC 2011 i686 GNU/Linux
/usr/bin/ksh
24.07.2011
1.22.39 24.07.2011 2.0.0 17.07.2011/bs mostly ok
long_parameter_test
Linux 2.6.32-33-generic-pae
#70-Ubuntu SMP Thu Jul 7 22:51:12 UTC 2011 i686 GNU/Linux
/bin/bash
24.07.2011 1.22.39 24.07.2011 2.0.0 17.07.2011/bs not ok
long_parameter_test
syntax_error_test_004
tee_test_001
Solaris 9 4/04 s9s_u6wos_08a SPARC
"Generic_122300-08
/usr/bin/ksh
26.07.2011
1.22.40
25.07.2011
2.0.0 17.07.2011/bs mostly ok

AIX 5.3
/usr/bin/ksh
26.07.2011
1.22.40
25.07.2011
2.0.0 17.07.2011/bs not ok
long_parameter_test
syntax_error_test_004




Back to top   

Notes

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


Back to top

Links


scriptt.sh is v2.x scriptt.sh v2.x
http://www.tramm.li/iWiki/ShellTipps.html
Shell Tips
http://www.shelldorado.com/
Heiner's Shell Dorado
http://apenwarr.ca/log/?m=201102#28
Insufficiently known POSIX shell features



Back to top

Download

Download scriptt.sh

Download scriptt_testsuite.sh

Download script_test.sh

Output of scriptt.sh -H


Back to top