Mt Xia: Technical Consulting Group

Business Continuity / Disaster Recovery / High Availability
Data Center Automation / Audit Response / Audit Compliance

-
Current Location
-

css
  Downloads
    Scripts
      Korn
        K93_Unix

-

digg Digg this page
del.icio.us Post to del.icio.us
Slashdot Slashdot it!


Business Web Site Hosting
$3.99 / month includes Tools,
Shopping Cart, Site Builder

www.siteox.com

FREE Domain Registration
included with Web Site Hosting
Tools, Social Networking, Blog

www.siteox.com



#!/usr/bin/ksh93
################################################################
#### Program: head_k93
#### 
#### Description: Emulation of the Unix "head" command
#### 
#### Author: Dana French (dfrench@mtxia.com)
#### 
#### Date: 07/22/2004
#### 
################################################################
function head_k93 {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset VERYFALSE="2"
  typeset CAT="${FALSE}"
  typeset LINES="${TRUE}"
  typeset BYTES="${FALSE}"
  typeset OLDPREV=""
  typeset FNAME=""
  typeset HEADCNT=""

  typeset STDIN="${1:+${FALSE}}"
  STDIN="${STDIN:-${TRUE}}"

  while getopts ":0123456789c#n#vV" OPTION
  do
    case "${OPTION}" in
        'c') BYTES="${TRUE}"
             HEADCNT="${OPTARG}"
             LINES="${FALSE}";;
        'n') LINES="${TRUE}"
             HEADCNT="${OPTARG}"
             BYTES="${FALSE}";;
        'v') VERBOSE="${TRUE}";;
        'V') VERYVERB="${TRUE}";;
        '?') print "Syntax: head [-Count|-c bytes|-n lines] [FILE]..." && exit 1 ;;
    esac

    let "${OPTION} + 0" > /dev/null 2>&1 &&
        (( CAT == FALSE )) &&
        CAT="${TRUE}"

    ! let "${OPTION} + 0" > /dev/null 2>&1 &&
        (( CAT == TRUE )) &&
        CAT="${VERYFALSE}"

    let "${OPTION} + 0" > /dev/null 2>&1 &&
        (( OPTION == 0 )) &&
        (( CAT == VERYFALSE )) &&
        CAT="${TRUE}"

    (( CAT == TRUE )) && HEADCNT="${HEADCNT}${OPTION}"
  done
 
  shift $(( ${OPTIND} - 1 ))

  typeset STDIN="${1:+${FALSE}}"
  STDIN="${STDIN:-${TRUE}}"

  if (( STDIN == TRUE ))
  then
          getHeadLine_k93 "${HEADCNT}" "${BYTES}" "${LINES}"
  else
    for FNAME in "${@}"
    do
      if [[ -f "${FNAME}" ]]
      then      
        exec 0<"${FNAME}"
          getHeadLine_k93 "${HEADCNT}" "${BYTES}" "${LINES}"
        exec 0<&-
      else
        print -u 2 "ERROR: \"${FNAME}\" does not exist or is not a regular file."
      fi
    done
  fi
  return 0
}
################################################################
function getHeadLine_k93 {
  typeset HEADCNT="${1}"
  typeset BYTES="${2}"
  typeset LINES="${3}"

  [[ "_${HEADCNT}" = "_" ]] && HEADCNT="10"
  for (( CNT=0; CNT<HEADCNT; ++CNT ))
  do
    if (( BYTES == TRUE ))
    then
      if IFS="" read -r -- LINE
      then
        for (( IND=0; IND<${#LINE}; ++IND ))
        do
          (( IND <= HEADCNT )) && print -r -n -- "${LINE:IND:1}"
          (( CNT++ ))
        done           
        print
        (( CNT++ ))
      else
        break
      fi
    fi

    if (( LINES == TRUE ))
    then
      if IFS="" read -r -- LINE
      then
        print -r -- "${LINE}"
      else
        break
      fi
    fi

  done

  return 0
}
################################################################

head_k93 "${@}"

-
head_k93
-
 


FREE Domain Registration
included with Web Site Hosting
Tools, Social Networking, Blog

www.siteox.com

Business Web Site Hosting
$3.99 / month includes Tools,
Shopping Cart, Site Builder

www.siteox.com