Home About BC DR HA Support Training Download
You are here: Home/ Downloads/ Scripts/ Korn/ Please Login or Register

-
Current Location
-

js
  Downloads
    Scripts
      Korn



-
 Mt Xia Inc.
-
BOOK:Advanced Shell Scripting



 Join our LinkedIn Group
AIX Advanced Technical Experts
Contract Opportunities

www.LinkedIn.com
-
 Train By Tweet
Training courses Tweet By Tweet
Various Subjects and Products
TrainByTweet.com


AIX Admin Methodology
Global Consolidation Project
All AIX admins should join
www.aixexpert.com
-

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


AIX FREE DEMO UNIX
Automated AIX Deployment
Reduce your DC costs

www.siteox.com

LPAR Leasing
Lease an AIX / i5 LPAR
Reduce your costs

www.mtxia.com

Server Leasing
Lease a Server off-site
Reduce your costs

www.mtxia.com

Data Center Automation
Business Continuity and DR
Virtualization/Consolidation

www.mtxia.com

HMC Service
Hardware Management Console
Manage Remote AIX / i5 LPARs

www.siteox.com

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

www.siteox.com

Disaster Recovery
Small Business Oriented
Off-Site Facilities

www.mtxia.com

IBM pSeries / iSeries
Reduce your Costs
Off-Site Server Hosting

www.mtxia.com

mkArrayFromFile_k93

We have expert shell programmers currently available to assist your organization with complex shell scripting projects. We have developed business continuity, disaster recovery, high availability and virtualization systems entirely in shell scripts and we have the advanced technical skills to solve your problems as well. Please Contact Us for more information.



#!/usr/bin/ksh93
################################################################
function usagemsg_mkArrayFromFile {
  print "
Program: mkArrayFromFile

Description: Reads a file and constructs the shell commands needed to
define an INDEX or ASSOCIATIVE array from the contents of the file.

The output of this function are the shell commands needed to define an
array, NOT an array.  The shell commands generated by this script must
be executed separately from this function to define the array contents.
See the example usage below.

Usage: ${1##*/} [-?vV] -f FileName [-d FieldDelimeter]
                [-A AssociativeArrayName] [-I IndexedArrayName]
                [-x IndexFieldPosition] [-e ArraryElementValuePostion]
  Where:
    -v = Verbose mode
    -V = Very Verbose Mode
    -f FileName = Name of a file to read and contruct shell commands to
                define an array to contain the contents of the file.
                (Default:None)
    -d FieldDelimeter = The character used as a field delimeter in the file
                (Default:whitespace)
    -A AssociativeArraryName = The name of an associative array in which to
                store the file contents as an array. (Default:ARYNAME)
    -I IndexedArraryName = The name of an indexed array in which to
                store the file contents as an array. (Default:ARYNAME)
    -x IndexFieldPosition = The numeric position of the field to use as 
                the index in an Associative array. (Default:0)
    -e IndexFieldPosition = The numeric position of the field to use as 
                the value for each element of the array. (Default:0)
Example Usage:
    typeset -A ARY
    eval \$( mkArrayFromFile -f /etc/passwd -d \":\" -A ARY )

Author: Dana French (dfrench@mtxia.com) Copyright 2013
\"AutoContent\" enabled
"
}

################################################################
#### Program: mkArrayFromFile
#### 
#### Description: 
#### 
#### Author: Dana French (dfrench@mtxia.com)
#### 
#### Date: 09/11/2013
#### 
################################################################
function mkArrayFromFile {
  typeset TRUE="0"
  typeset FALSE="1"
  typeset VERBOSE="${FALSE}"
  typeset VERYVERB="${FALSE}"
  typeset DELIM="${IFS}"
  typeset FILENAME=""
  typeset ARYNAME="ARYNAME"
  typeset INDEXFLD="0"
  typeset VALUE="0"
  typeset IDX="0"
  typeset VARDEF='${ARYNAME}[${IDX}]'
  typeset VALDEF='\"${TMPARY[*]}\"; '

  while getopts ":vVd:f:A:I:x:e:" OPTION
  do
    case "${OPTION}" in
        'd') DELIM="${OPTARG}";;
        'f') FILENAME="${OPTARG}";;
        'A') ARYNAME="${OPTARG}"
             VARDEF='${ARYNAME}[\"${TMPARY[${INDEXFLD}]}\"]';;
        'I') ARYNAME="${OPTARG}";;
        'x') INDEXFLD="${OPTARG}";;
        'e') VALUEFLD="${OPTARG}";;
        'v') VERBOSE="${TRUE}";;
        'V') VERYVERB="${TRUE}";;
        '?') usagemsg_mkArrayFromFile && exit 1;;
    esac
  done
 
  shift $(( ${OPTIND} - 1 ))

################################################################
  trap "usagemsg_mkArrayFromFile ${0}" EXIT
      
  if [[ "_${FILENAME}" == "_" ]]
  then
      print -u 2 -- "# ERROR: Filename not specified."
      return 1      
  fi

  trap "-" EXIT

  (( VERYVERB == TRUE )) && VERBOSE="${TRUE}"
  (( VERBOSE  == TRUE )) && print -u 2 -- "###################################"
  (( VERYVERB == TRUE )) && set -x

################################################################

  (( INDEXFLD != VALUEFLD )) && VALDEF='\"${TMPARY[${VALUEFLD}]}\"; '

  while IFS="" read -r -- LINE
  do
    IFS="${DELIM}"
    TMPARY=( ${LINE//\"/} )
    (( VERBOSE == TRUE )) && eval print -r -u 2 -- "\"# ${VARDEF}=${VALDEF}\""
    eval print -r -- "\"${VARDEF}=${VALDEF}\""
    IFS=$' \t\n'

    (( ++IDX ))

  done < "${FILENAME}"

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

mkArrayFromFile "${@}"

####
####  Delete the following lines to utilize this function in scripts
####

# The following provides an example of using the mkArrayFromFile
# function and then outputs the results of both indexed and associative
# arrays.

typeset -A ASSOC

eval $( mkArrayFromFile -v -d ":" -f /etc/passwd -A ASSOC -x 0 -e 5 )

for IDX in "${!ASSOC[@]}"
do
   print ${IDX} ${ASSOC[${IDX}]}
done

####

set -A IARY

eval $( mkArrayFromFile -v -d ":" -f /etc/passwd -I IARY -x 0 -e 0 )

for IDX in "${!IARY[@]}"
do
   print ${IDX} ${IARY[${IDX}]}
done

-
Generate Array from File
-
 


AIX FREE DEMO UNIX
Automated AIX Deployment
Reduce your DC costs

www.siteox.com

LPAR Leasing
Lease an AIX / i5 LPAR
Reduce your costs

www.mtxia.com

Server Leasing
Lease a Server off-site
Reduce your costs

www.mtxia.com

Data Center Automation
Business Continuity and DR
Virtualization/Consolidation

www.mtxia.com

HMC Service
Hardware Management Console
Manage Remote AIX / i5 LPARs

www.siteox.com

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

www.siteox.com

Disaster Recovery
Small Business Oriented
Off-Site Facilities

www.mtxia.com

IBM pSeries / iSeries
Reduce your Costs
Off-Site Server Hosting

www.mtxia.com