100% Spam Free Email | Contact Us | Products/Services | Korn Shell | MicroEMACS


Mt Xia:
Technical Consulting Group

 

watch_k93


Contact Mt Xia for help with your shell scripting projects at 615.556.0456 or dfrench@mtxia.com

Current:watch_k93
Previous:
Home Page



#!/usr/bin/ksh93
################################################################
function usagemsg_watch_k93 {
  print "
Program: watch_k93

The watch_k93 Korn Shell command/function runs a user specified
"command" repeatedly, displaying its output (the first screen full).  
This allows the user to watch the program output change over time. By
default, the program is run every 2 seconds.

Usage: ${1##*/} [-n SECONDS] [-i ITERATIONS] [-e ][-v] [-V] [-?] <command>

    Where:
      '-n SECONDS'    = Interval between running command (seconds)
                        (default: 2 seconds)
      '-i ITERATIONS' = Number of iterations to run loop
                        (default: continuous)
                 '-e' = Display the end of the output from the command
                        instead of the beginning
                 '-v' = Verbose mode
                 '-V' = Very Verbose Mode

Author: Dana French (dfrench@mtxia.com)    Copyright 2004

\"AutoContent\" enabled"
return 0
}
################################################################
#### 
#### Description:
#### 
#### The watch_k93 Korn Shell command/function runs a user
#### specified "command" repeatedly, displaying its output
#### (the first screen full).   This allows the user to watch
#### the program output change over time. By default, the
#### program is run every 2 seconds.
#### 
#### Assumptions:
#### 
#### Dependencies:
#### 
#### Products:
#### 
#### Configured Usage:
#### 
#### Details:
#### 
################################################################
function watch_k93 {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset ENDOFOUT="${FALSE}"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset INTERVAL="2"
  typeset ITERATIONS="-1"
  typeset SCRNLEN="24"
  typeset LINENBR="0"
  typeset -L79 HEADLINE
  typeset ITTEXT

  while getopts ":n#i#evV" OPTION
  do
      case "${OPTION}" in
          'n') INTERVAL="${OPTARG}";;
          'i') ITERATIONS="${OPTARG}";;
          'e') ENDOFOUT="${TRUE}";;
          'v') VERBOSE="${TRUE}";;
          'V') VERYVERB="${TRUE}";;
          '?') usagemsg_watch_k93 "${0}" && return 1;;
      esac
  done
   
  shift $(( ${OPTIND} - 1 ))
  
  trap 'usagemsg_watch_k93 ${0}' EXIT
  (( INTERVAL < 0 )) && print -u 2 "ERROR: Invalid Interval" && exit 2
  trap '-' EXIT

  (( VERYVERB == TRUE )) && set -x
  
################################################################
  
#### Initialize the iteration counter to zero, also the loop counter.
#### If the number of iterations is less than zero, reset the iteration
#### counter to be 1 less than the number of iterations ( for an 
#### endless loop ).

  CNT="0"
  LOOPCNT="0"
  (( ITERATIONS < 0 )) && (( CNT = ITERATIONS - 1 ))

#### Execute the command specified by the user for the number of iterations
#### specified.  If the number of iterations was not specified the loop
#### will continue endlessly until interrupted by the user.

  while (( CNT < ITERATIONS ))
  do

#### Unset the array that contains the output from the command at every
#### iteration so there are no leftovers from the previous iteration.
#### Run the command with all specified parameters and options, and save
#### the output to an array, one output line per array element. Count
#### the number of lines in the output and save it to a variable.

    unset WATCHOUTPUT
    IFS=$'\n' WATCHOUTPUT=( $( "${@}" ) )
    ARYEND="${#WATCHOUTPUT[*]}"

    clear    
    (( LOOPCNT++ ))

#### If the number of iterations was specified by the user, indicate
#### that number on the screen header line. Otherwise just
#### show the current iteration number.

    ITTEXT="Iteration ${LOOPCNT} of ${ITERATIONS}"
    (( ITERATIONS < 0 )) && ITTEXT="Iteration ${LOOPCNT}"
    HEADLINE="Every ${INTERVAL}s: ${ITTEXT}: ${@}: $( date )"
    print -r -- "${HEADLINE}"

#### Determine the last array position of the output. If the number
#### of output lines is less than the number of lines on the screen, 
#### set the last line to the number of lines in the output array, minus
#### one line.

    (( ENDLINE = SCRNLEN - 2 ))
    (( ARYEND < SCRNLEN )) && (( ENDLINE = ARYEND - 1 ))
    BEGLINE=0

#### If the option to display the end of the output from the command was
#### used, then set the beginning line to display equal to the last array
#### position of the output, minus the length of the screen less one line.
#### Then set the last line to the number of lines in the output array, 
#### minus one line.

    if (( ENDOFOUT == TRUE ))
    then
        (( BEGLINE = ARYEND - ( SCRNLEN - 1 ) ))
        (( BEGLINE < 0 )) && BEGLINE=0
        (( ENDLINE = ARYEND - 1 ))
    fi

#### Print one screenful of the the output from the command 
#### to the screen, except for the last line.

    for (( LINENBR=BEGLINE; LINENBR < ENDLINE; ++LINENBR ))
    do
      print -r -- "${WATCHOUTPUT[LINENBR]:0:79}"
    done

#### Print the last line of output but not with a newline
#### at the end.

    print -r -n -- "${WATCHOUTPUT[LINENBR]}"

#### Increment the iteration counter.

    (( CNT++ ))

#### If the number of iterations is less than zero, meaning
#### to continuously output to the screen, then reset the iteration counter
#### to one less than the number of iterations so the while loop will
#### continue endlessly

    (( ITERATIONS < 0 )) && (( CNT = ITERATIONS - 1 ))

#### If the iteration counter is not equal to the number of iterations 
#### allowed then sleep for the specified time period.  If they are 
#### equal this means it is the last iteration, so don't sleep, just 
#### return to the calling function.

    (( CNT != ITERATIONS )) && sleep ${INTERVAL}
  done
  return 0

}
################################################################

watch_k93 "${@}"

 

For information regarding this page, contact Dana French ( dfrench@mtxia.com )
Copyright 2008 by Mt Xia Inc, All Rights Reserved