randomizearray_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_randomizeArray_k93 {
print -u 2 -- "
Program: randomizeArray_k93
This script will accept an indexed array of values and randomize the
index positions of the values in the array. It will also read a file
into the specified index array, one line per array element, then
randomize those values and store them in a second user specified indexed
array.
Usage: ${1##*/} [-?] [-vV] [-f file] [-p] SourceArray RandomizedArray
Where:
-f = File from which to read SourceArray values
-p = Print each array value of the RandomizedArray to STDOUT
-v = Verbose Mode
-V = Very Verbose Mode
SourceArray = The name of the array containing the original values
RandomizedArray = The name of the array to contain the randomized values
Author: Dana French (dfrench@mtxia.com)
Copyright 2012 by Dana French: All Rights Reserved
\"AutoContent\" enabled
"
}
################################################################
####
#### Description:
####
#### This script will accept an indexed array of values and
#### randomize the index positions of the values in the
#### array. It will also read a file into the specified
#### index array, one line per array element, then randomize
#### those values and store them in a second user specified
#### indexed array.
####
#### Assumptions:
####
#### It is assumed the user specified source array name and
#### randomized array name are index arrays, NOT associative
#### arrays.
####
#### Dependencies:
####
#### This script is NOT dependent upon any UNIX utilities, all commands
#### are korn shell built-ins.
####
#### Products:
####
#### Configured Usage:
####
#### This script can be run from the command line or can be
#### called as a function from a function library.
####
#### A command line example of executing this script.
####
#### ./randomizeArray_k93 -f file -p SourceArray RandomizeArray
####
#### Example output from the example execution.
####
#### Details:
####
################################################################
function randomizeArray_k93 {
typeset VERSION="1.0"
typeset TRUE="0"
typeset FALSE="1"
typeset VERBOSE="${FALSE}"
typeset VERYVERB="${FALSE}"
typeset RNDPRT="${FALSE}"
typeset SRCFILE=""
typeset CNT
typeset DENOM
typeset IDX
typeset RAND
typeset POS
typeset VAL
while getopts ":vVf:p" OPTION
do
case "${OPTION}" in
'f') SRCFILE="${OPTARG}";;
'p') RNDPRT="${TRUE}";;
'v') VERBOSE="${TRUE}";;
'V') VERYVERB="${TRUE}";;
[?:#]) usagemsg_randomizeArray_k93 "${0}" && return 1 ;;
esac
done
shift $(( ${OPTIND} - 1 ))
(( VERBOSE == TRUE )) && print -u 2 -- "# Version................................: ${VERSION}"
(( VERBOSE == TRUE )) && print -u 2 -- "# Source File............................: ${SRCFILE}"
(( VERYVERB == TRUE )) && set -x
################################################################
#### Check to determine if there are two command line arguments remaining
#### after all command line options have been processed. If so then use
#### these two arguments as the source array name and the randomized array
#### name. If there are not two command line arguments remaining, then
#### notify the user of the error and return from this function.
if (( ${#@} == 2 ))
then
nameref SRCARY=${1}
nameref RNDARY=${2}
(( VERBOSE == TRUE )) && print -u 2 -- "# Source Array Name......................: ${1}"
(( VERBOSE == TRUE )) && print -u 2 -- "# Randomized Array Name......... ........: ${2}"
else
(( VERBOSE == TRUE )) && print -u 2 -- "# Invalid number of arguments"
usagemsg_randomizeArray_k93 "${0}"
return 2
fi
#### If the "-f" option was specified on the command line, read each line
#### from the file into the source array.
if [[ -f "${SRCFILE}" ]]
then
IFS=$'\n'
SRCARY=( $( ⩽ "${SRCFILE}" ) )
IFS=$' \t\n'
fi
#### If the -v option was specified on the command line, output the source
#### array values to standard error and identify each associated array index
#### position.
if (( VERBOSE == TRUE ))
then
for IDX in "${!SRCARY[@]}"
do
print -u 2 -- "# SRC[${IDX}]:${SRCARY[IDX]}"
done
fi
#### Determine the number of array elements in the source array, if it is
#### less than 65535 then set the denominator to use when calculating a
#### random index position, to 65535. If it is greater than 65535, use it as
#### the denominator.
CNT="${#SRCARY[@]}"
DENOM="${CNT}"
(( CNT ⩽ 65535 )) && DENOM="65535"
#### Loop through each element of the source array values, and assign each
#### value to a randomized index position in the randomized array. Using a
#### "while" loop, check to see if the selected randomized index position is
#### already filled. If so, select a new index position.
for IDX in "${!SRCARY[@]}"
do
RAND="${RANDOM}${RANDOM}"
(( POS = RAND % DENOM ))
while [[ "_${RNDARY[POS]}" != "_" ]]
do
RAND="${RANDOM}${RANDOM}"
(( POS = RAND % DENOM ))
done
RNDARY[POS]="${SRCARY[IDX]}"
done
#### If the -v or the -p option was specified on the command line, loop
#### through each element of the randomized array and output the value to
#### standard error (-v) or standard output (-p).
if (( VERBOSE == TRUE )) || (( RNDPRT == TRUE ))
then
for IDX in "${!RNDARY[@]}"
do
(( VERBOSE == TRUE )) && print -u 2 -- "# RND[${IDX}]:${RNDARY[IDX]}"
(( RNDPRT == TRUE )) && print "${RNDARY[IDX]}"
done
fi
return 0
}
################################################################
randomizeArray_k93 "${@}"
|