Home

scriptt.sh 2.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). 

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


Note:

This is the documentation for version 2.x of scriptt.sh . This version is the current version.

The documentation for the version 1.x of scriptt.sh is here.



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


History


Version
Releasedate
         Description



2.2.0
09.01.2016
##V#   2.2.0.0 09.01.2016 /bs
##V#     the function GetKeystroke did not process the parameter - fixed
##V#     GetKeystroke did not check for CTRL-C - fixed
##V#     the function GetKeystroke now supports the parameter [lower|upper] to convert the user input
##V#       to lowercase or uppercase; the real user input is still available in the global variable RAW_USER_INPUT
##V#     the script now prints the actions that would have been done in the cleanup routine if __NO_CLEANUP is set
##V#     added the parameter -D nocleanup (shortcut for -D setvar:__NO_CLEANUP=0)
##V#     added the parameter -D cleanup[=type] to enable/disable the house keeping
##V#     die() will now again end the script even if -f was used
##V#     includeScript now checks the syntax of an include script
##V#     added the function tryIncludeScript
##V#     added the parameter -D showdefaults
##V#     added the parameter -D tracemain
##V#     corrected some errors in the messages written by the script
##V#     reworked the comments in the script
##V#      added the variable __START_DIR_REAL


2.1.0.11
26.11.2015
##V#   2.1.0.8 09.09.2015 /bs
##V#      added the variable __SETOPTS and the alias __TRACE_ACTIVE
##V#
##V#   2.1.0.9 26.10.2015 /bs
##V#     added the keyword printargs to the parameter -D
##V#     BackupFileIfNecessary now supports rotating backups, e.g.
##V#        file.0, file.1, file.3, ...
##V#     To use that feature the new format of the parameter for the
##V#     function is:
##V#
##V#        BackupFileIfNeccessary [file{,no_of_backups}] [...]
##V#
##V#     In the default configuration the script now creates a new log
##V#     file for each execution of the script and retains up to
##V#     10 backups of the log file.
##V#     To overwrite the number of backups use the following syntax for
##V#     the parameter -l:
##V#
##V#        -l logfile,[no_of_backups_of_the_logfile]
##V#
##V#     The default number of backups for the log file is configured in the variable
##V#     MAX_NO_OF_LOGFILES
##V#
##V#   2.1.0.10 16.11.2015 /bs
##V#      replaced code incompatible with some ksh versions
##V#
##V#   2.1.0.11 26.11.2015 /bs
##V#      moved initialisation of __SCRIPTNAME, __SCRIPTDIR, and
##V#      __REAL_SCRIPTDIR to the beginnning of the script

2.1.0.7
25.07.2015
##V#   2.1.0.2 06.11.2014 /bs
##V#     Added the parameter "-D create_dump=d"
##V#     the function rand now uses nawk in Solaris and awk in all other OS
##V#
##V#   2.1.0.2 08.11.2014 /bs
##V#     corrected a bug in code for the parameter "-D fn_to_device=filename"
##V#     added ${__FUNCTION_EXIT} to some of the functions
##V#
##V#   2.1.0.3 12.12.2014 /bs
##V#     added the variable ${__BACKUP_FILE} to BackupFileIfNecessary
##V#
##V#   2.1.0.4 25.12.2014 /bs
##V#      __SCRIPTDIR was wrong if the script was called with the name only
##V#
##V#   2.1.0.5 08.05.2015 /bs
##V#      LogRuntimeInfo did not work in the default ksh from Solaris 11 --fixed
##V#
##V#   2.1.0.6 08.07.2015 /bs
##V#     Added the parameter "-D debug"
##V#
##V#   2.1.0.7 25.07.2015 /bs
##V#      added the parameter -D dryrun. To use dryrun in your code
##V#        add the prefix "${PREFIX}" to every external command executed
##V#        by your script
##V#      added the parameter -D list_rc to list all return codes used in the script
##V#        (only works if you only use "die" to end the script)

2.1.0.1
04.11.2014
##V#   2.1.0.1 04.11.2014 /bs
##V#     Added the parameter variable__DEBUG_PREFIX
##V#     Added the function LogDebugMsg
##V#     Added the parameter "-D listfunc"
##V#     Added the parameter "-D fn_to_device=filename"
##V#     corrected a minor bug in the parameter handling for the parameter "-D"

2.1.0.0
02.11.2014
##V#   2.1.0.0 02.11.2014 /bs
##V#     Added the parameter -D SyntaxHelp to print syntax usage examples for the template
##V#     Added the parameter -D debugcode="x"
##V#     Added the parameter -D tracefunc=f1
##V#     Added the parameter -D setvar:name=value
##V#     Added the parameter -D create_documentation
##V#     Added more usage information for the template


2.0.0.7
27.04.2014
##V#   2.0.0.7 27.04.2014 /bs
##V#     AskUser now save the last input in the variable LAST_USER_INPUT, to enter
##V#       this value again use "#last"
##V#     Version parameter (-V) usage enhanced:  use "-v -v  -V" to print also the version
##V#       history; use "-v -v -v -V" to also print the template version history.

2.0.0.6
27.01.2014
####   2.0.0.5 08.01.2014 /bs
####      added the function executeFunctionIfDefined
####
####   2.0.0.6 27.01.2014 /bs
####     added the function PrintLine
####     added the functions GetSeconds, GetMinutes, ConvertMinutesToHours,
####       and GetTimeStamp
####     added the debug options fn_to_stderr, fn_to_tty, and fn_to_handle9
####     max. return value for a function is 255 and therefor the functions
####       for the stack and the functions pos and lastpos now abort the
####       script if a value greater than 255 should be returned
####     added the variables __SHEBANG, __SCRIPT_SHELL, and __SCRIPT_SHELL_OPTIONS
####     added the function DebugShell
####     AskUser now has a hidden shell; use "shell<return>" to call the DebugShell
####       set __DEBUG_SHELL_IN_ASKUSER to ${__FALSE} to disable the DebugShell
####       in AskUser
####     added the function ConvertDateToEpoc

2.0.0.4
01.01.2014 ####  2.0.0.1 06.08.2013 /bs
####     added the variable __MACHINE_SUB_CLASS. Possible values
####     for sun4v machines: either "GuestLDom" or "PrimaryLDom"
####
####  2.0.0.2 01.09.2013 /bs
####     added the variables __SYSCMDS and __SYSCMDS_FILE
####
####  2.0.0.3 16.12.2013 /bs
####     now the Log-* functions return ${__TRUE} if a message is printed
####     and ${__FALSE} if not
####

####  2.0.0.4 01.01.2014 /bs
####     the alias __settrap is renamed to settraps (with leading s)
####     two new aliase are defined: __ingoretraps and __unsettraps
####     whence function for non-ksh compatible shells rewritten
####       without using ksh
####     the switch -D is now used to toggle debug switches
####       known debug switches:
####        help  -- print the usage help for -D
####         msg  -- log debug messages to /tmp/<scriptname>.<pid>.debug
####       trace  -- activate tracing to the file /tmp/<scriptname>.<pid>.trace
####     AskUser now accepts also "yes" and "no"
####     function IsFunctionDefined rewritten
####     now __LOGON_USERID and __USERID are equal to $LOGNAME until I
####     find a working solution
####       (the code in the previous version did not work if STDIN is not a tty)

2.0.0.0
31.05.2013
####  2.0.0.0 17.05.2013 /bs
####     added the variable __GENERAL_SIGNAL_FUNCTION: This variable
####     contains the name of a function that is called for all SIGNALs
####       before the special SIGNAL handler  is called
####     removed the Debug Handler for single step execution (due to the
####       length of the  template it is not useful anymore; use the
####       version 1.x of scriptt.sh if you still need the Debug Handler)
####     function executeCommandAndLogSTDERR rewritten
####     removed the function CheckParameterCount
####     use lsb_release in Linux to retrieve OS infos if available
####     minor fixes for code and comments
####    replaced PrintWithTimeStamp with code that does not use awk


... (see here for the old history entries)
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



Operating system

Solaris, Linux, AIX, Windows with Cygwin


Language / type

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


Prerequisites

n/a


Usage

Predefined parameter:

[xtrnaw7@t540p jwm]$ /data/develop/scripts/scriptt.sh -v -v -h
[26.11.2015 19:46:05] scriptt.sh v1.0.0 started at Do 26. Nov 19:46:05 CET 2015.
[26.11.2015 19:46:05] No config file ("scriptt.conf") found (use -C to create a default config file)
[26.11.2015 19:46:05] RUNTIME INFO: Parameter after processing the default parameter are:  "-v -v -h" 
  scriptt.sh v1.0.0 - ??? short description ???

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

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


  Use the parameter "-v -h [-v]" to view the detailed online help; use the parameter "-X" to view some usage examples.

  see also http://bnsmb.de/solaris/scriptt.html




 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
              use "-l [logfile,no_of_backups]" to define the number of retained backups
              of the logfile; the default no of backups is 10
              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    - debug switch
              current value:
              use "-D help" to list the known debug switches
              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.13956.tee.log"
              Long format: --tee

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



  Use  "-D create_documentation" to create all available documentation for the script.

  Use "-D SyntaxHelp" to print usage examples for the functions defined in the
  template to STDERR.

  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.13956.tee.log")
  __INFO_PREFIX (Current value: "INFO:")
  __DEBUG_PREFIX (Current value: "DEBUG:")
  __WARNING_PREFIX (Current value: "WARNING:")
  __ERROR_PREFIX (Current value: "ERROR:")
  __RUNTIME_INFO_PREFIX (Current value: "RUNTIME INFO:")
  __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: "")
[26.11.2015 19:46:05] The log file used was "/tmp/scriptt.sh.13956.TEMP" 
[26.11.2015 19:46:05] scriptt.sh v1.0.0 started at Do 26. Nov 19:46:05 CET 2015 and ended at Do 26. Nov 19:46:05 CET 2015.
[26.11.2015 19:46:05] The time used for the script is 0 minutes and 0 seconds.
[26.11.2015 19:46:05] The RC is 1.
[xtrnaw7@t540p jwm]$

 
 
 
 


Known debug switches for scriptt.sh

[xtrnaw7@t540p jwm]$ /data/develop/scripts/scriptt.sh -D help
[11.01.2016 17:37:17] scriptt.sh v1.0.0 started at Mo 11. Jan 17:37:17 CET 2016.
[11.01.2016 17:37:17] No config file ("/etc/scriptt.conf") found (use -C to create a default config file)
Known debug switches (for -D / --debug):

  help          -- show this usage and exit
  create_documentation
                -- create the script documentation
  list_rc       -- list return codes used by this script
                   Works only if you only use "die" to end the script
  msg           -- log debug messages to the file /tmp/scriptt.sh.14441.debug
                   This parameter should be the first parameter.
  trace         -- activate tracing to the file /tmp/scriptt.sh.14441.trace
  tracemain     -- trace the main function
  fn_to_stderr  -- print the function names to STDERR
  fn_to_tty     -- print the function names to /dev/tty
  fn_to_handle9 -- print the function names to the file handle 9
  fn_to_device=filename
                -- print the function names to the file "filename"
  debugcode="x" -- execute the debug code "x" at every function start
  printargs     -- print the script arguments
  tracefunc=f1[,...,f#]
                -- enable tracing for the functions f1 to f#
                   Note: Use either debugcode=x or tracefunc=f1 - but NOT both
  debug         -- start debug env
  setvar:name=value
                -- set the variable "name" to "value"
  listfunc      -- list all functions defined and exit
  showdefaults  -- show the default variable values
  create_dump=dirname
                -- enable environment dumps; target directory is dirname
  SyntaxHelp    -- print syntax usage examples for the functions in the template
                   and exit
  dryrun        -- dry run only, do not execute commands
  dryrun=prefix -- dry run only, add the prefix "prefix" to all commands

  nocleanup     -- disable the house keeping at script end
  cleanup[=type]
                -- disable the house keeping at script end; type can be
                   all - enable all house keeping
                   none - disable all house keeping (this is the default)
                   nodelete - disable only removing of temporary files and directories
[11.01.2016 17:37:17] The log file used was "/tmp/scriptt.sh.14441.TEMP" 
[11.01.2016 17:37:17] The debug messages are logged to "/tmp/scriptt.sh.14441.debug" 
[11.01.2016 17:37:17] scriptt.sh v1.0.0 started at Mo 11. Jan 17:37:17 CET 2016 and ended at Mo 11. Jan 17:37:17 CET 2016.
[11.01.2016 17:37:17] The time used for the script is 0 minutes and 0 seconds.
[11.01.2016 17:37:17] The RC is 0.
[xtrnaw7@t540p jwm]$







Documentation


Use the parameter - D create_documentation to create the documentation for scriptt.sh:

[xtrnaw7@t540p templates]$ ./scriptt.sh -D create_documentation
[01.11.2014 18:39:01] scriptt.sh v1.0.0 started at Sa 1. Nov 18:39:01 CET 2014.
[01.11.2014 18:39:01] No config file ("scriptt.conf") found (use -C to create a default config file)
[01.11.2014 18:39:01] Writing the long usage documentation to scriptt.sh.long_usage.txt ...
[01.11.2014 18:39:01] Writing the debug switch documentation to scriptt.sh.debug_switches.text ...
[01.11.2014 18:39:01] Writing the script documentation to scriptt.sh.txt ...
[01.11.2014 18:39:01] Writing the script usage examples to scriptt.sh.usage_examples.txt ...
[01.11.2014 18:39:01] Writing the function usage examples to scriptt.sh.function_examples.txt ...
[01.11.2014 18:39:01] Using the log file "/var/tmp/scriptt.LOG" 
[01.11.2014 18:39:01] The log file used was "/var/tmp/scriptt.LOG" 
[01.11.2014 18:39:01] scriptt.sh v1.0.0 started at Sa 1. Nov 18:39:01 CET 2014 and ended at Sa 1. Nov 18:39:01 CET 2014.
[01.11.2014 18:39:01] The time used for the script is 0 minutes and 0 seconds.
[01.11.2014 18:39:01] The RC is 0.
[xtrnaw7@t540p templates]$




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



Notes

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


Links


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
http://blog.fpmurphy.com/2008/10/ksh93-date-manipulation.html
ksh93 builtin time formatting with printf examples



Download

Download scriptt.sh


Documentation

scriptt.sh usage help

scriptt.sh debug switches documentation

scriptt.sh function documentation

scriptt.sh function examples

Testsuite

Download scriptt_testsuite.sh

Download script_test.sh




Back to top