+# This enables variables in cfg/"setup".sh files to support the pdsh HOSTLIST
+# expressions format. As a bonus we can then just pass in those variables
+# to pdsh. What this function does is take a HOSTLIST type string and
+# expand it into a space deliminated list for us.
+hostlist_expand() {
+ local hostlist=$1
+ local offset=$2
+ local myList
+ local item
+ local list
+
+ [ -z "$hostlist" ] && return
+
+ # Translate the case of [..],..,[..] to [..] .. [..]
+ list="${hostlist/],/] }"
+ front=${list%%[*}
+ [[ "$front" == *,* ]] && {
+ new="${list%,*} "
+ old="${list%,*},"
+ list=${list/${old}/${new}}
+ }
+
+ for item in $list; do
+ # Test if we have any []'s at all
+ if [ "$item" != "${item/\[/}" ]; then {
+ # Expand the [*] into list
+ name=${item%%[*}
+ back=${item#*]}
+
+ if [ "$name" != "$item" ]; then
+ group=${item#$name[*}
+ group=${group%%]*}
+
+ for range in ${group//,/ }; do
+ begin=${range%-*}
+ end=${range#*-}
+
+ # Number of leading zeros
+ padlen=${#begin}
+ padlen2=${#end}
+ end=$(echo $end | sed 's/0*//')
+ [[ -z "$end" ]] && end=0
+ [[ $padlen2 -gt $padlen ]] && {
+ [[ $padlen2 -eq ${#end} ]] && padlen2=0
+ padlen=$padlen2
+ }
+ begin=$(echo $begin | sed 's/0*//')
+ [ -z $begin ] && begin=0
+
+ for num in $(seq -f "%0${padlen}g" $begin $end); do
+ value="${name#*,}${num}${back}"
+ [ "$value" != "${value/\[/}" ] && {
+ value=$(hostlist_expand "$value")
+ }
+ myList="$myList $value"
+ done
+ done
+ fi
+ } else {
+ myList="$myList $item"
+ } fi
+ done
+ myList=${myList//,/ }
+ myList=${myList:1} # Remove first character which is a space
+
+ # Filter any duplicates without sorting
+ list="$myList "
+ myList="${list%% *}"
+
+ while [[ "$list" != ${myList##* } ]]; do
+ list=${list//${list%% *} /}
+ myList="$myList ${list%% *}"
+ done
+ myList="${myList%* }";
+
+ # We can select an object at a offset in the list
+ [ $# -eq 2 ] && {
+ cnt=0
+ for item in $myList; do
+ let cnt=cnt+1
+ [ $cnt -eq $offset ] && {
+ myList=$item
+ }
+ done
+ [ $(get_node_count $myList) -ne 1 ] && myList=""
+ }
+ echo $myList
+}
+