Skip to content

Instantly share code, notes, and snippets.

@ChristianKniep
Created April 30, 2014 07:22
Show Gist options
  • Save ChristianKniep/5226917cbe1cee9842f8 to your computer and use it in GitHub Desktop.
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, ...).
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