#!/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