本代码能处理形如:compute[1-64], compute[01-11], compute[11-99], compute[010-021], compute[1-021], cu[01-64]-ib等多种情况,能满足基本上99%的这种循环输入字符串。
#!/bin/bash
declare -i iFrom
declare -i iEnd
declare -i iFromLength
declare -i iEndLength
declare -i i
iFromLength=-1
iEndLength=-1
tmpstring="compute[10-64]-ib"
# validate the string syntax
echo $tmpstring | grep -E "[0-9a-zA-Z_-]+\[[0-9]+-[0-9]+\][0-9a-zA-Z_-]*"
if [ $? -ne 0 ]; then
echo "illegal string..."
exit
else
echo "correct..."
fi
# split the string to get what we want
mainpart_pre=${tmpstring%%\[*\]*}
mainpart_suf=${tmpstring##*\[*\]}
fromindex_tmp=${tmpstring##*\[}
fromindex=${fromindex_tmp%%-*\]*}
endindex_tmp=${tmpstring#*\[*-}
endindex=${endindex_tmp%%\]*}
echo "mainpartpre is $mainpart_pre"
echo "mainpartsuf is $mainpart_suf"
echo "fromindex is $fromindex"
echo "endindex is $endindex"
# if fromindex begins with zero, then fromindex and endindex must have same length
if echo $fromindex | grep -q "^0"; then
iFromLength=${#fromindex}
iEndLength=${#endindex}
if [ $iFromLength -ne $iEndLength ]; then
echo "$fromindex and $endindex should have the same length, exiting..."
exit
fi
# prefixzero to indicate that we should add "0" to our hostnames
prefixzero="y"
fi
# get rid of the prefix zeros
fromindex=$(echo "$fromindex" | grep -o "[^0][0-9]*")
endindex=$(echo "$endindex" | grep -o "[^0][0-9]*")
iFrom=fromindex
iEnd=endindex
if [ $iFrom -ge $iEnd ]; then
echo "illegal iFrom and iEnd..."
exit
fi
for (( ; iFrom<=iEnd; iFrom++ ))
do
if [ "$prefixzero" == "y" ]; then
# fromindex begins with 0
echo -n ${mainpart_pre}
for (( i=0; i<(iEndLength-${#iFrom}); i++ ))
do
echo -n "0"
done
echo -n $iFrom
echo $mainpart_suf
else
# fromindex doesn't begin with 0, we can generate the hostname directly
echo ${mainpart_pre}${iFrom}${mainpart_suf}
fi
done
declare -i iFrom
declare -i iEnd
declare -i iFromLength
declare -i iEndLength
declare -i i
iFromLength=-1
iEndLength=-1
tmpstring="compute[10-64]-ib"
# validate the string syntax
echo $tmpstring | grep -E "[0-9a-zA-Z_-]+\[[0-9]+-[0-9]+\][0-9a-zA-Z_-]*"
if [ $? -ne 0 ]; then
echo "illegal string..."
exit
else
echo "correct..."
fi
# split the string to get what we want
mainpart_pre=${tmpstring%%\[*\]*}
mainpart_suf=${tmpstring##*\[*\]}
fromindex_tmp=${tmpstring##*\[}
fromindex=${fromindex_tmp%%-*\]*}
endindex_tmp=${tmpstring#*\[*-}
endindex=${endindex_tmp%%\]*}
echo "mainpartpre is $mainpart_pre"
echo "mainpartsuf is $mainpart_suf"
echo "fromindex is $fromindex"
echo "endindex is $endindex"
# if fromindex begins with zero, then fromindex and endindex must have same length
if echo $fromindex | grep -q "^0"; then
iFromLength=${#fromindex}
iEndLength=${#endindex}
if [ $iFromLength -ne $iEndLength ]; then
echo "$fromindex and $endindex should have the same length, exiting..."
exit
fi
# prefixzero to indicate that we should add "0" to our hostnames
prefixzero="y"
fi
# get rid of the prefix zeros
fromindex=$(echo "$fromindex" | grep -o "[^0][0-9]*")
endindex=$(echo "$endindex" | grep -o "[^0][0-9]*")
iFrom=fromindex
iEnd=endindex
if [ $iFrom -ge $iEnd ]; then
echo "illegal iFrom and iEnd..."
exit
fi
for (( ; iFrom<=iEnd; iFrom++ ))
do
if [ "$prefixzero" == "y" ]; then
# fromindex begins with 0
echo -n ${mainpart_pre}
for (( i=0; i<(iEndLength-${#iFrom}); i++ ))
do
echo -n "0"
done
echo -n $iFrom
echo $mainpart_suf
else
# fromindex doesn't begin with 0, we can generate the hostname directly
echo ${mainpart_pre}${iFrom}${mainpart_suf}
fi
done