Created
April 30, 2014 07:22
-
-
Save ChristianKniep/5226917cbe1cee9842f8 to your computer and use it in GitHub Desktop.
Create a rangeset out of a list of integer (used within clustershell, slurm.conf, ...).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function create_rangeset { | |
# consumes a list of integers and creates a rangeset | |
#### | |
# $ create_rangeset 0 1 2 5 6 {10..50} | |
# 0-2,5-6,10-50 | |
RSET="" | |
CNT="-1" | |
PREV_CNT="-1" | |
for INT in $*;do | |
if [ ${CNT} -ne ${INT} ];then | |
if [ ${CNT} -eq -1 ];then | |
CNT=0 | |
fi | |
if [ "X${RSET}" != "X" ];then | |
RSET="${RSET}-${PREV_CNT}," | |
fi | |
CNT=${INT} | |
RSET="${RSET}${CNT}" | |
fi | |
PREV_CNT=${CNT} | |
CNT=$(echo "${CNT} + 1"|bc) | |
done | |
RSET="${RSET}-${PREV_CNT}" | |
echo ${RSET} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment