scriptt_mini.sh



Contents


  1. scriptt_mini.sh
    1. Contents
    2. Purpose
    3. Author
    4. License
    5. History
    6. Operating system
    7. Language / type
    8. Script Usage
    9. Details
      1. Script usage help
      2. Parameter handling
      3. Predefined variables
      4. Logging
      5. BuildIn log functionality
      6. Executing commands
      7. BREAK/CTRL-C handling
      8. Housekeeping / Cleanup
      9. Trap Handler
      10. Tracing functions
      11. DebugShell
      12. Other functions defined
      13. User defined functions
    10. Links
    11. Download



Purpose

scriptt_mini.sh is a minimal template for Kornshell scripts.

scriptt_mini.sh includes only a very small runtime for shell scripts with functionality for logging, housekeeping, trap handling, and trouble shooting / debugging.

scriptt_mini.sh is only a template for a shell script -- without adding your code it does not do any useful things.

Note:

A much more complex script template for shell scripts with various functions is scriptt.sh.




Author


Bernd Schemmer

email: Bernd.Schemmer@gmx.de

Homepage: http://bnsmb.de



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.0.0
07.11.2017
initial public release





Operating system


Solaris, Linux, AIX, Windows with Cygwin



Language / type


Kornshell Script . Note that not all ksh features required for this script template are available in all kornshell versions. E.g. in the ksh from Solaris 10 some features for the trace handling are missing.

Be aware that the features for the debugging do not work in sh or bash.



Script Usage

[xtrnaw7@t540p /data/develop/scripts]$ ./scriptt_mini.sh -h
[06.11.2017 09:30 ] ### scriptt_mini.sh started at Mon Nov  6 09:30:41 CET 2017
 ???

 Function: ???
 
 Usage:    ??? [-v|--verbose] [-q|--quiet] [-f|--force] [-y|--yes] [-n|--no] [-l|--logfile filename]
               [d{:dryrun_prefix}|--dryrun{:dryrun_prefix}] [-D|--debugshell] [-t fn|--tracefunc fn]

 Parameter:
   -v - verbose mode; use -v -v to also print the runtime system messages
   -q - quiet mode
   -f - force execution
   -y - answer yes to all questions
   -n - answer no to all questions
   -l - logfile to use; use "-l none" to not use a logfile at a
        the default logfile is /var/tmp/${SCRIPTNAME}.log
   -d - dryrun mode, default dryrun_prefix is "echo "
   -D - start a debug shell and continue the script afterwards
   -t - trace the function fn


Current environment: ksh version: 93 | change function code supported: yes | tracing features using $0 supported: yes

[06.11.2017 09:30 ] ### The logfile used was /var/tmp/scriptt_mini.sh.log
[06.11.2017 09:30 ] ### scriptt_mini.sh ended at Mon Nov  6 09:30:41 CET 2017
[xtrnaw7@t540p /data/develop/scripts]$




Details



Script usage help

If scriptt_mini.sh is called with the parameter -h or --help the script usage help text is written to STDOUT using the function show_script_usage.

To avoid redundant data the function show_script_usage reads the source of the script and prints all lines starting with #H# (without the "#H# ").



Parameter handling


scriptt_mini.sh processes all parameter found and known; the parameter handling stops at the first not known parameter. The remaining parameter will be saved in the variable  NOT_USED_PARAMETER.

Before processing the parameter the script saves all parameter in the variable ALL_PARAMETER.


Predefined variables


scriptt_mini.sh defines the following variables:

Note: If "Environment variable used"  is yes for a variable the script uses the value of the environment variable set before the script starts


Variablename
Content
Environment
variable used?
Comment
PREFIX
prefix for executables or commands called by the script to implement the dry-run feature
yes
you must use

${PREFIX} os_command {..}

for all binaries/commands in your script to use the dry-run feature
KSH_VERSION
ksh version - either 88 or 93


USE_ONLY_KSH88_FEATURES
${__TRUE} if the ksh is compatible to ksh88 only
yes
the default value depends on the ksh version used to execute the script
TYPESET_F_SUPPORTED
"yes" if the current shell supports "typeset -f function_name" else "no"

some ksh88 shells (like the one in Solaris 10) do not support this feature
TRACE_FEATURE_SUPPORTED
"yes" if the current shell supports $0 in functions else "no"

some ksh88 shells (like the one in Solaris 10) do not support this feature -- see DebugShell for a workaround
__DEBUG_CODE
debug code for the functions -- see DebugShell
yes
you must use eval in the debug code and the debug could should not write to STDOUT, e.g

__DEBUG_CODE="eval echo \"Current function is \$__FUNCTION\" >&2 " ./scriptt_mini.sh

__FUNCTION_INIT
code to execute in every function -- see trap handling


__TRUE
0


__FALSE
1


BREAK_ALLOWED
either ${__TRUE}, ${__FALSE}, or DebugShell, Default is DebugShell
yes
${__TRUE} - CTRL-C aborts the script
${__FALSE} - CTRL-C is disabled
DebugShell - call the DebugShell if CTRL-C is pressed




CUR_HOST
hostname

output of the command "hostname"; so either with or without domain
CUR_SHORT_HOST
hostname without domain


CUR_OS
Operating system

output of the command "uname -s"
SCRIPTNAME
name of the script as used by the user (e.g. ./myscript.sh, /home/user/myscript.sh)


SCRIPTDIR
directory with the script as used by the user ( e.g. . ,  ../bin/, /usr/local/bin)


REAL_SCRIPTDIR
absolute path for the script (always starting at /)


REAL_SCRIPTNAME
fully qualified name of the script






WORKING_DIR
current working directory at script start


LOGFILE
log file for the script, default is /var/tmp/<scriptname>.log
yes
use the parameter -l to change the logfile used; use the parameter -l none to not use a logfile
LOGFILE_FOUND
LOG_MESSAGE_CACHE
internal variables for the log file handling -- do not change






ALL_PARAMETER all parameter for the script


NOT_USED_PARAMETER the script parameter that are not processed by the runtime code






CUR_USER_ID
UID (e.g. 1000)


CUR_USER_NAME
user name (e.g. max)






CUR_GROUP_ID
GID (e.g. 1000)


CUR_GROUP_NAME
group name (e.g. staff)






RUNNING_IN_TERMINAL_SESSION
${__TRUE} - the script is running in an interactive session; ${__FALSE} - the script is running in a session without tty






DIRS_TO_REMOVE
directories that should be deleted at script end in the cleanup function

FILES_TO_REMOVE
files that should be deleted at script end in the cleanup function

PROCS_TO_KILL
processes that should be killed at script end in the cleanup function

KILL_PROC_TIMEOUT
number of seconds to wait after the "kill <pid>" command in the housekeeping routine until a "kill -9 <pid>" is issued

this is only the default timeout value for all PIDs; you can define a different timeout for each PID in PROCS_TO_KILL using the syntax PID:timeout_in_seconds.
CLEANUP_FUNCTIONS
commands to execute in the cleanup function


FINISH_FUNCTIONS
commands to execute in the cleanup function





APPLICATION_VARIABLES
additional variables to print in the DebugShell


RUNTIME_VARIABLES
variables to print in the DebugShell, see DebugShell






QUIET
${__TRUE} if the parameter -q is used
yes

FORCE
${__TRUE} if the parameter -f is used
yes

VERBOSE
${__TRUE} if the parameter -v is used
yes

VERBOSE_LEVEL
if this variable is greater then 1 the script also prints the runtime messages; use multiple times "-v" to increase the VERBOSE_LEVEL
yes





__USER_RESPONSE_IS
"y" if parameter -y is used; "n" if parameter -n is used; else empty

if the variable __USER_RESPONSE_IS is either "y" or "n" the function AskUser will not request input from the user
__NOECHO
variable for the function AskUser: Do not print the user input if set to ${__TRUE}
default is ${__FALSE}
__USE_TTY
variable for the function AskUser: User /dev/tty instead of STDIN or STDOUT if set to ${__TRUE}
default is ${__FALSE}
__DEBUG_SHELL_IN_ASKUSER
variable for the function AskUser: Allow to call the DebugShell  in AskUser if set to ${__TRUE}
default is ${__TRUE}
__STTY_SETTINGS
stty settings; used in the function die to reset the stty settings if neccessary






INSIDE_DIE
${__TRUE} if inside of the function die


INSIDE_DEBUG_SHELL
${__TRUE} if inside of the function DebugShell


INSIDE_USR1_HANDLER
${__TRUE} if inside the trap handler for the signal USR1


INSIDE_USR2_HANDLER ${__TRUE} if inside the trap handler for the signal USR2

INSIDE_BREAK_HANDLER
${__TRUE} if inside the trap handler for the signal BREAK


INSIDE_HUP_HANDLER ${__TRUE} if inside the trap handler for the signal HUP


INSIDE_EXIT_HANDLER ${__TRUE} if inside the trap handler for the signal EXIT


INSIDE_QUIT_HANDLER ${__TRUE} if inside the trap handler for the signal QUIT






RCM_SERVICE
RCM_FUNCTION
RCM_HOSTID_FILE
RCM_HOSTID
RCM_DBQUERY
RCM_DBGET_FILE
RCM_USERID
RCM_PASSWORD
FOUND_APPL_PARAM_ENTRY_KEYS
variables used for an RCM environment - - just ignore or delete them if not used





Logging

You should only use one of the defined logging functions to write messages to STDOUT or STDERR. The log function will also write the messages to the logfile and  implement a logrotate for the logfile used (with up to 10 backups). Set the variable ROTATE_LOG to ${__FALSE} to disable the log rotating before calling one of the log functions.

To disable the usage of a logfile set the variable LOGFILE to an empty string or use the parameter -l none.

The defined functions for logging are

Function
Purpose
Comment
LogMsg
write a message to STDOUT and also to the logfile

LogOnly
write a message only to the logfile
LogMsg is used to write the message
LogInfo
write the message prefixed with "INFO: " to STDERR and the logfile if VERBOSE is __TRUE
LogMsg is used to write the message
LogError
write the message prefixed with  "ERROR: " to STDERR and the logfile
LogMsg is used to write the message
LogWarning
write the message prefixed with "WARNING: " to STDOUT and the logfile
LogMsg is used to write the message
LogRuntimeInfo
internal function to print the verbose messages of the runtime system to STDERR and the logfile
LogMsg is used to write the message



RotateLog
internal function to rotate the log file if neccessary
You can use this function to rotate any file , usage is

RotateLog [file1 [... [file#]]]







BuildIn log functionality


If scriptt_mini.sh is called in a session without an tty all output for STDOUT or STDERR is redirected to the file /var/tmp/${SCRIPTNAME}.STDOUT_STDERRR.

To disable this feature use the parameter -q.


Executing commands

scriptt_mini.sh supports a simple dry-run feature: In dry-run mode every command is prefixed with "echo ".

To use the dry-run feature of the script for your commands also use always

    ${PREFIX} os_command {...}

for all commands used in your script that might change something.  ${PREFIX} is empty in non-dryrun mode; in dry-run mode it is set to "echo " (or what ever you define with the parameter -d).

To execute a command and write all output of the command to STDOUT/STDERR also to the logfile use

    executeCommandAndLog ${PREFIX} os_command {...}


The returncode of executeCommandAndLog is the returncode of the command executed.


BREAK/CTRL-C handling

scriptt_mini.sh installs a trap handler for CTRL-C . To disable CTRL-C set the variable BREAK_ALLOWED to ${__FALSE}; to enable CTRL-C set the variable BREAK_ALLOWED to ${__TRUE}, and to call the DebugShell if CTRL-C is pressed set the variable BREAK_ALLOWED to DebugShell.

Default value for BREAK_ALLOWED is "DebugShell".


Housekeeping / Cleanup

To do the house keeping at script end you should always use the function die to end the script instead of exit.

The usage for the function die is

die [script_return_code] [message_to_print]

The default for the parameter script_return_code is 0; there is no default for the  parameter message_to_print.

The function die uses the function cleanup to do the house keeping. cleanup does these tasks in this order:

    - execute the functions listed in the variable CLEANUP_FUNCTIONS

    - kill all running processes listed in the variable PROCS_TO_KILL using "kill" and, if neccessary, "kill -9".

    - delete all files listed in the variable FILES_TO_REMOVE using "rm -f"

    - delete all directories listed in the variable DIRS_TO_REMOVE using "rm -rf"

   - execute the functions listed in the variable FINISH_FUNCTIONS


scriptt_mini.sh
installs a trap handler  for the signal EXIT to do the house keeping at script end even if the function die was not used to end the script.


Notes:

To execute a function at script end before the other housekeeping steps are done use

      CLEANUP_FUNCTIONS=${CLEANUP_FUNCTIONS} <your_cleanup_function>"

Notes: The number of cleanup functions is not limited; parameter for the functions are not supported and not existent functions are ignored.


To start a process in the background that should be killed at script end if it's still running use

    <start_process> &
    ps -p $! >/dev/null && PROCS_TO_KILL="${PROCS_TO_KILL} $!"



To create a file that should be removed at script end if it still exists use

    touch "${NEW_FILE}" && FILES_TO_REMOVE="${FILES_TO_REMOVE} ${NEW_FILE}"



To create a directory that should be removed at script end if it still exists use

    mkdir -p "${NEW_DIR}" && DIRS_TO_REMOVE="${DIRS_TO_REMOVE} ${NEW_DIR}"


To execute a function at script end after the other housekeeping steps are done use

      FINISH_FUNCTIONS=${FINISH_FUNCTIONS} <your_finish_function>"

Notes: The number of finish functions is not limited; parameter for the functions are not supported and not existent functions are ignored.




Trap Handler


scriptt_mini.sh installs the following trap handler for the signals

Signal
Trap Handler
Comment
EXIT
signal_exit_handler
house keeping cleanup
BREAK signal_break_handler
disable or enable CTRL-C
USR1 signal_usr1_handler
call the DebugShell
USR2
signal_usr2_handler the handler does nothing in the default config
HUP
signal_hup_handler the handler does nothing in the default config
QUIT
signal_quit_handler
house keeping cleanup

Unfortunately the trap handler are set back to the default values in each function. Therefor you must set the trap handler in every function.

scriptt_mini.sh define some aliase and variables to simplify this

# alias to install the trap handler
#
# Note: The statement LINENO=${LINENO} is necessary to use the variable LINENO in the trap command
#
alias __settraps="
  LINENO=\${LINENO}
  trap 'signal_hup_handler    \${LINENO}' 1
  trap 'signal_break_handler  \${LINENO}' 2
  trap 'signal_quit_handler   \${LINENO}' 3
  trap 'signal_exit_handler   \${LINENO}' 15
  trap 'signal_usr1_handler   \${LINENO}' SIGUSR1
  trap 'signal_usr2_handler   \${LINENO}' SIGUSR2
"

# alias to ignore all traps
#
alias __ignoretraps="
  LINENO=\${LINENO}
  trap '' 1
  trap '' 2
  trap '' 3
  trap '' 15
  trap '' SIGUSR1
  trap '' SIGUSR2
"

# alias to reset all traps to the defaults
#
alias __unsettraps="
  LINENO=\${LINENO}
  trap - 1
  trap - 2
  trap - 3
  trap - 15
  trap - SIGUSR1
  trap - SIGUSR2
"

__FUNCTION_INIT="eval __settraps"



So every function should start with


function <your_function_name> {
  ${__FUNCTION_INIT}
...




Tracing functions


To trace one or more functions the parameter "-t fn" can be used. Tracing is implemented with the variable ${__DEBUG_CODE}. So for this feature the function should be defined like this

function <your_function_name> {
  ${__DEBUG_CODE}
  ${__FUNCTION_INIT}
...

If a function that should be traced does not start with ${__DEBUG_CODE} the script will temporary add the ${__DEBUG_CODE} statement to the function and document this with the log message

Adding debug code to the function "<function_name>"


This feature only works with some ksh versions but not all. The script will print the status "change function code supported: no" in the usage help and in the DebugShell if this feature is not supported.


Some ksh version do not support the usage of $0 in functions. The script prints the status of this feature either if called with the parameter -h :

[xtrnaw7@t540p /data/develop/scripts]$ ./scriptt_mini.sh -h | grep envi
Current environment: ksh version: 93 | change function code supported: no | tracing feature using $0 supported: no
[xtrnaw7@t540p /data/develop/scripts]$

and also in the DebugShell

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: no | tracing feature using $0 supported: no
>>

To use the trace functionality for these ksh versions you must add the statement

  typeset __FUNCTION="<function_name>"

to each function, e.g.

function MyFunction {
  typeset __FUNCTION="MyFunction"
  ${__DEBUG_CODE}
  ${__FUNCTION_INIT}



All functions of the runtime system are already prepared for this workaround.


Example for the paramter "t fn" for shells that support $0 in functions

[xtrnaw7@t540p /data/develop/scripts]$ ./scriptt_mini.sh  -v -t MyFunction,Myfunction1,MyFunction1 -t die
[04.11.2017 17:44 ] ### scriptt_mini.sh started at Sat Nov  4 17:44:29 CET 2017
[04.11.2017 17:44 ] ### The logfile used is /var/tmp/scriptt_mini.sh.log
[04.11.2017 17:44 ] Enabling trace for the function "MyFunction" ...
[04.11.2017 17:44 ] Enabling trace for the function "Myfunction1" ...
[04.11.2017 17:44 ] The function "Myfunction1" is not defined
[04.11.2017 17:44 ] Enabling trace for the function "MyFunction1" ...
[04.11.2017 17:44 ] Adding debug code to the function "MyFunction1" ...
[04.11.2017 17:44 ] Enabling trace for the function "die" ...
[04.11.2017 17:44 ] Hello World, you must add your own code here to do something useful

*** Enabling trace for the function MyFunction ...
+ THISRC=0
+ typeset THISRC
+ NO_OF_LINES=0
+ typeset NO_OF_LINES
+ LINENO=1410
+ typeset LINENO
+ CUR_VAR=''
+ typeset CUR_VAR
+ LogMsg 'This is a test function'
[04.11.2017 17:44 ] This is a test function
+ date $'+DATE=%Y-%m-%d\n               YEAR=%Y\n               MONTH=%m\n               DAY=%d\n               TIME=%H:%M:%S\n               HOUR=%H\n               MINUTE=%M\n               SECOND=%S\n               datestamp=%Y-%m-%d_%H.%M.%S\n               DayOfWeek=%a\n               MonthAbbrev=%b'
+ eval DATE=$'2017-11-04\n               YEAR=2017\n               MONTH=11\n               DAY=04\n               TIME=17:44:29\n               HOUR=17\n               MINUTE=44\n               SECOND=29\n               datestamp=2017-11-04_17.44.29\n               DayOfWeek=Sat\n               MonthAbbrev=Nov'
+ DATE=2017-11-04
+ YEAR=2017
+ MONTH=11
+ DAY=04
+ TIME=17:44:29
+ HOUR=17
+ MINUTE=44
+ SECOND=29
+ datestamp=2017-11-04_17.44.29
+ DayOfWeek=Sat
+ MonthAbbrev=Nov
+ eval echo 'YEAR is $YEAR'
+ echo YEAR is 2017
YEAR is 2017
+ eval echo 'MONTH is $MONTH'
+ echo MONTH is 11
MONTH is 11
+ eval echo 'DAY is $DAY'
+ echo DAY is 04
DAY is 04
+ eval echo 'TIME is $TIME'
+ echo TIME is 17:44:29
TIME is 17:44:29
+ eval echo 'HOUR is $HOUR'
+ echo HOUR is 17
HOUR is 17
+ eval echo 'MINUTE is $MINUTE'
+ echo MINUTE is 44
MINUTE is 44
+ eval echo 'SECOND is $SECOND'
+ echo SECOND is 29
SECOND is 29
+ eval echo 'datestamp is $datestamp'
+ echo datestamp is 2017-11-04_17.44.29
datestamp is 2017-11-04_17.44.29
+ eval echo 'DayOfWeek is $DayOfWeek'
+ echo DayOfWeek is Sat
DayOfWeek is Sat
+ eval echo 'MonthAbbrev is $MonthAbbrev'
+ echo MonthAbbrev is Nov
MonthAbbrev is Nov
+ [ -r /data/develop/scripts/scriptt_mini.sh ]
+ wc -l /data/develop/scripts/scriptt_mini.sh
+ NO_OF_LINES='1686 /data/develop/scripts/scriptt_mini.sh'
+ LogMsg '/data/develop/scripts/scriptt_mini.sh has 1686 /data/develop/scripts/scriptt_mini.sh lines'
[04.11.2017 17:44 ] /data/develop/scripts/scriptt_mini.sh has 1686 /data/develop/scripts/scriptt_mini.sh lines
+ THISRC=0
+ return 0

*** Enabling trace for the function MyFunction1 ...
+ THISRC=0
+ typeset THISRC
+ LogMsg 'Inside of MyFunction1'
[04.11.2017 17:44 ] Inside of MyFunction1
+ return 0

*** Enabling trace for the function die ...
+ THISRC=0
+ typeset THISRC
+ [ 0x '=' x ]
+ INSIDE_DIE=0
+ trap '' EXIT
+ trap '' SIGUSR1
+ trap '' SIGUSR2
+ trap '' SIGINT
+ trap '' SIGHUP
+ [ x '!=' x ]
+ cleanup
+ [ 1 -ne 0 ]
+ shift
+ [ 0 -ne 0 ]
+ [ /var/tmp/scriptt_mini.sh.logx '!=' x -a -f /var/tmp/scriptt_mini.sh.log ]
+ LogMsg '### The logfile used was /var/tmp/scriptt_mini.sh.log'
[04.11.2017 17:44 ] ### The logfile used was /var/tmp/scriptt_mini.sh.log
+ date
+ LogMsg '### scriptt_mini.sh ended at Sat Nov  4 17:44:29 CET 2017'
[04.11.2017 17:44 ] ### scriptt_mini.sh ended at Sat Nov  4 17:44:29 CET 2017
+ exit 0
[xtrnaw7@t540p /data/develop/scripts]$
 



DebugShell

The DebugShell is in principle only a loop that reads a line from the user and executes it with eval. DebugShell reads and writes to /dev/tty so you can also use it with STDOUT redirection of your script.

DebugShell  is not allowed if STDIN is not a tty. To call a DebugShell before starting the script use the parameter -D:

[xtrnaw7@t540p /data/develop/scripts]$ ./scriptt_mini.sh -D
[06.11.2017 13:31 ] ### scriptt_mini.sh started at Mon Nov  6 13:31:00 CET 2017
 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> help

vars                      - print the runtime variable values
functions                 - list all defined functions
func fn                   - view the source code for the function fn (supported by this shell: yes)

verbose                   - toggle the verbose switch (Current value: 1)

view_debug                - view the current trace settings
clear_debug               - clear the tracing for all functions
set_debug f1 [...fn]      - enable tracing for the functions f1 ... fn
add_debug_code f1 [...fn] - enable debug code for the functions f1 ... fn (supported by this shell: yes)
                            use all for f1 to add code to all functions

exit                      - exit the shell
quit                      - end the script using die
abort                     - abort the script using kill -9

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>

The alias vars lists all runtime variables with values:


  -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> vars
*** constants
  __TRUE: "0"
  __FALSE: "1"
*** signal handling:
  BREAK_ALLOWED: "DebugShell"
  INSIDE_USR1_HANDLER: "1"
  INSIDE_USR2_HANDLER: "1"
  INSIDE_BREAK_HANDLER: "1"
  INSIDE_HUP_HANDLER: "1"
  INSIDE_EXIT_HANDLER: "1"
  INSIDE_QUIT_HANDLER: "1"
*** Parameter:
  ALL_PARAMETER: "-D"
  NOT_USED_PARAMETER: ""
  FORCE: "1"
  QUIET: "1"
  VERBOSE: "1"
  VERBOSE_LEVEL: "0"
*** Hostname variables:
  CUR_HOST: "t540p"
  CUR_SHORT_HOST: "t540p"
  CUR_OS: "Linux"
*** Scriptname and directory:
  SCRIPTNAME: "scriptt_mini.sh"
  SCRIPTDIR: "."
  REAL_SCRIPTDIR: "/data/develop/scripts"
  REAL_SCRIPTNAME: "/data/develop/scripts/scriptt_mini.sh"
*** Current environment:
  WORKING_DIR: "/data/develop/scripts"
  LOGFILE: "/var/tmp/scriptt_mini.sh.log"
  LOGFILE_FOUND: "1"
  ROTATE_LOG: "0"
  __KSH_VERSION: "93"
  __USE_ONLY_KSH88_FEATURES: "1"
  RUNNING_IN_TERMINAL_SESSION: "0"
*** User and group:
  CUR_USER_ID: "1000"
  CUR_USER_NAME: "xtrnaw7"
  CUR_GROUP_ID: "1000"
  CUR_GROUP_NAME: "xtrnaw7"
*** RCM variables:
  RCM_SERVICE: ""
  RCM_FUNCTION: ""
  RCM_HOSTID: ""
*** Housekeeping:
  CLEANUP_FUNCTIONS: ""
  FILES_TO_REMOVE: ""
  DIRS_TO_REMOVE: ""
  PROCS_TO_KILL: ""
  KILL_PROC_TIMEOUT: "2"
  FINISH_FUNCTIONS: ""
*** Debugging
  __DEBUG_CODE: ""
  __FUNCTION_INIT: "eval __settraps"
  PREFIX: ""

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>


To add variables to list for the alias vars add the variable names to list to the variable APPLICATION_VARIABLES. To add a string to print to that variable use

    #message_to_print

Blanks are not allowed in the strings for APPLICATION_VARIABLES but the alias will replace "_" with blanks

Example:

# Code:
# ----------------------------------------------------------------------
# main code starts here
#

APPLICATION_VARIABLES="
#These_are_my_application_variables
MyVAR1
MyVAR2
"

...

MyVAR1="Value aaa"
MyVAR2="Value bbb ccc"

...

# then in DebugShell

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> vars
*** constants
  __TRUE: "0"
  __FALSE: "1" 

...

*** Housekeeping:
  DIRS_TO_REMOVE: ""
  FILES_TO_REMOVE: ""
  PROCS_TO_KILL: ""
*** These are my application variables
  MyVAR1: "Value aaa"
  MyVAR2: "Value bbb ccc"

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>


The alias functions lists all defined functions:

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> functions
AskUser
check_rcm_values
cleanup
curtimestamp
DebugShell
die
executeCommandAndLog
get_rcm_userid
isNumber
LogError
LogInfo
LogMsg
LogOnly
LogRuntimeInfo
LogWarning
MyFunction
MyFunction1
print_runtime_variables
Read_APPL_PARAMS_entries
Retrieve_file_from_Jamaica
RotateLog
show_script_usage
signal_break_handler
signal_exit_handler
signal_hup_handler
signal_quit_handler
signal_usr1_handler
signal_usr2_handler

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>
 

To view the source code of a function use "func <function_name>" (This works only if "typeset -f" is supported by your shell -- see above)


You can use the aliase add_debug_code, view_debug, clear_debug, and set_debug to enable tracing (set -x) for one or more functions (see the section Tracing Functions above for the limitations of this feature)

Enabling trace for a function is done by changing the value for the variable ${__DEBUG_CODE}. Therefor every function that should use this feature should start with

function <functionname> {
  ${__DEBUG_CODE}
...

For functions not starting with this code set_debug will temporary insert ${__DEBUG_CODE} at the start of the function.

Note that the feature does only work with functions defined with

functionname() {

if the variable __FUNCTION is defined in these functions.


To temporary add the ${__DEBUG_CODE} statement to a function
manually while running the script you can use the alias add_debug_code , e.g:

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> typeset -f MyFunction1
function MyFunction1 {
  typeset THISRC=${__TRUE}

  LogMsg "Inside of MyFunction1"

  return ${THISRC}
}


 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> add_debug_code MyFunction1
Adding debug code to the function MyFunction1 ...

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> typeset -f MyFunction1
function MyFunction1 { ${__DEBUG_CODE};
  typeset THISRC=${__TRUE}

  LogMsg "Inside of MyFunction1"

  return ${THISRC}
}
 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>


Example on how to use the trace feature:


 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> view_debug
The current debug code for all functions (__DEBUG_CODE) is:


  -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> functions
AskUser
check_rcm_values
cleanup
curtimestamp
DebugShell
die
executeCommandAndLog
function_template
get_rcm_userid
isNumber
LogError
LogInfo
LogMsg
LogOnly
LogRuntimeInfo
LogWarning
MyFunction
MyFunction1
MyFunction2
print_runtime_variables
Read_APPL_PARAMS_entries
Retrieve_file_from_Jamaica
RotateLog
show_script_usage
signal_break_handler
signal_exit_handler
signal_hup_handler
signal_quit_handler
signal_usr1_handler
signal_usr2_handler

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>



 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> set_debug MyFunction LogWarning
Enabling debug code for the functions "MyFunction LogWarning" now
 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> view_debug
The current debug code for all functions (__DEBUG_CODE) is:
eval [ 0 = 1  -o "$0"x = "MyFunction"x  -o "$0"x = "LogWarning"x  ] && printf "
*** Enabling trace for the function $0 ...
" >&2 && set -x

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> MyFunction

*** Enabling trace for the function MyFunction ...
+ THISRC=0
+ typeset THISRC
+ NO_OF_LINES=0
+ typeset NO_OF_LINES
+ LogMsg 'This is my test function'
[31.10.2017 14:03 ] This is my test function
+ [ -r /data/develop/scripts/scriptt_mini.sh ]
+ wc -l /data/develop/scripts/scriptt_mini.sh
+ NO_OF_LINES='1364 /data/develop/scripts/scriptt_mini.sh'
+ LogMsg '/data/develop/scripts/scriptt_mini.sh has 1364 /data/develop/scripts/scriptt_mini.sh lines'
[31.10.2017 14:03 ] /data/develop/scripts/scriptt_mini.sh has 1364 /data/develop/scripts/scriptt_mini.sh lines
+ THISRC=0
+ return 0

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> LogWarning

*** Enabling trace for the function LogWarning ...
+ LogMsg 'WARNING: '
[31.10.2017 14:03 ] WARNING:

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>

-------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> clear_debug
Clearing the debug code now ...

 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>> view_debug
The current debug code for all functions (__DEBUG_CODE) is:


 -------------------------------------------------------------------------------
scriptt_mini.sh - debug shell - enter a command to execute ("exit" to leave the shell)
Current environment: ksh version: 93 | change function code supported: yes | tracing feature using $0 supported: yes
>>


Please note that set_debug always overwrites the variable ${__DEBUG_CODE} so you can not add additional definitions to already existing definitions.


Note that you can call the function DebugShell at any time in your script



Other functions defined


scriptt_mini.sh defines some other useful functions :

Function
Used for
Comment
isNumber
check if a value is an number

curstimestamp write the current date and time to STDOUT
AskUser
ask the user for a value
If the parameter -y is used AskUser will always return ${__TRUE} without asking the user
If the parameter -n is used AskUser will always return ${__FALSE} without asking the user

The behaviour of AskUser can be changed using the variables __USE_TTY, __NOECHO, and __DEBUG_SHELL_IN_ASKUSER

check_rcm_values
get_rcm_userid
Read_APPL_PARAMS_entries
Retrieve_file_from_Jamaica
special functions for an RCM environment -- just ignore or delete these functions if not used




User defined functions


To use all features of the template you should define new functions like this

# ----------------------------------------------------------------------
# function_template
#
# function:
#
# usage:
#
# returns: ${__TRUE} -
#          ${__FALSE} -
#
function function_template {
  typeset __FUNCTION="function_template"
  ${__FUNCTION_INIT}
  ${__DEBUG_CODE}

  typeset THISRC=${__TRUE}

# add your code her

  return ${THISRC}
}



Links



http://bnsmb.de/solaris/scriptt.html
scriptt.sh , a more complex shell script template
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
http://www.unix.com/unix-dummies-questions-answers/13018-exit-status-command-pipe-line.html#post47559
source for the function executeCommandAndLog



Download

Download scriptt_mini.sh



back top top