# SccsId[] = "%W% (USL function) %G%"
RNM_name="RANGE_NUMS"
if [ ".${SECONDS}" = "." ]; then # Bourne function already loaded?
[ ."`set|egrep '^$RNM_name\(\)\{$'`" != . ] && RNM_loaded=1
else # Korn or Bash shell and function already loaded?
if [ `expr "\`uname -s\`" : "[Ll][Ii][Nn][Uu][Xx]"` -eq 0 ]; then
[ ."`typeset +f|awk '/^'$RNM_name'[=\(]?/'`" != . ] && RNM_loaded=1
else # Linux
[ ."`typeset -F|awk '/^'$RNM_name'[=\(]?/'`" != . ] && RNM_loaded=1
fi
fi
if [ 0${RNM_loaded} -eq 0 ]; then
#----------------------------------------------------------------------#
RANGE_NUMS() # Function documentation located at bottom. #
#----------------------------------------------------------------------#
{ [ ."${AWK}" = . ] && { { [ -x /usr/bin/nawk ] && AWK=/usr/bin/nawk; } \
|| { [ -x /bin/gawk ] && AWK=/bin/gawk ; } \
|| { [ -x /usr/bin/awk ] && AWK=/usr/bin/awk ; }; }
if [ $# -gt 0 ]; then
#-------------------------------------------------------------#
# Awk's BEGIN {FS = "-"; RS = " "} changes the input field #
# field separator to a dash and the input record separator to #
# a space (unfortunately, changing output record separator to #
# a space has problems). #
#-------------------------------------------------------------#
echo "$@"|$AWK \
'BEGIN { FS = "-"; RS = " "}
{ # Iterates for each range passed as arguments.
# Print numbers with trailing blank but no newlines.
if (($1 ~ /^[0-9]+$/) && ($1 ~ /^[0-9]+$/))
if ($1 <= $2) #-------------------#
for (i=$1; i<=$2; i++) # Ascending order #
printf("%d ", i) #-------------------#
else #-------------------#
for (i=$1; i>=$2; i--) # Descending order #
printf("%d ", i) #-------------------#
else
exit 1 # Non-numeric values!
} # E.O.Action section
END {exit 0}' 2>&1
[ $? -eq 0 ] && return 0
fi
#------------------------------------------#
# Reaching here means something is amiss. #
#------------------------------------------#
if [ .${SHLIB} = . ]; then SHLIB=/usr/local/scripts; export SHLIB; fi
. $SHLIB/email_msg.sh # Calls $SHLIB/exit.sh
#------------------------------------------------------------#
# If the following variables are not set, use these defaults.#
#------------------------------------------------------------#
: ${script_name:=`basename $0`}
: ${sp:=" "}
RNM_ID="$script_name($RNM_name)"
RNM_usage=`$AWK -v sp="$sp" \
-v RNM_name="$RNM_name" \
-v script_name="$script_name" \
'BEGIN \
{ print sp"Usage: "RNM_name" range [ranges...]\n" ,
"\n"sp"Purpose: Convert one or more numeric ranges, like 12-15," ,
"\n"sp" into expanded strings of numbers (e.g. 12 13 14 15).",
"\n"sp" (Ranges may be ascending or descending.)\n"
}'`
if [ $# -eq 0 ]; then
EMAIL_MSG "ERROR (Function): $RNM_ID" \
"$RNM_usage"
else
EMAIL_MSG "ERROR: $RNM_ID" \
"${sp}Non-numeric range value!\n$RNM_usage"
fi
return 1
} # "RNM_" prefix identifies this function's variables.
fi
#======================================================================#
# D O C U M E N T A T I O N #
#======================================================================#
# #
# Author: Unknown (taken from "UNIX POWER TOOLS," O'Reilly & #
# Associates/Random House Book, 1994, ISBN 0-679-79073-X. #
# #
# Implementor: Bob Orlando #
# #
# Date: January 24, 1997 #
# #
# Program ID: range_nums.sh (formerly derange.num) #
# #
# Usage: RANGE_NUMS p-q [...] #
# #
# Purpose: Convert numeric ranges, like 1-5, into individual #
# numbers. #
# #
# Description: Function takes one or more ranges of numbers as #
# command-line arguments and splits them into the single #
# numbers that make up the full range range (it's much #
# faster than a while-loop doing the same thing), and is #
# most useful when used like this: #
# #
# for num in `RANGE_NUMS 1-100` #
# do #
# do-something-with $num #
# done #
# #
# Example: RANGE_NUMS 1-5 5-1 #
# returns 1 2 3 4 5 5 4 3 2 1 #
# #
# Exit_status: On error, failure (1) for function error (e.g. user #
# supplies invalid arguments). Otherwise returns #
# success (0) echoing the de-ranged numbers to stdout. #
# #
# Calls: NOYIFY_ANALYSTS library function and nawk. #
# #
# Notes: If you want some other separator than a blank, #
# use the translate (tr) command to get it. #
# #
# Modified: 2004-04-02 Bob Orlando #
# v1.9 * Expand $AWK testing and assignment. #
# #
# 2004-03-03 Bob Orlando #
# v1.8 * Change set|egrep|awk to just set|egrep. #
# #
#----------------------------------------------------------------------#
|