Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Create a rangeset out of a list of integer (used within clustershell, slurm.conf, ...).
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
You can’t perform that action at this time.