我需要划分数组。我有此代码:我可以在bash中乘上数组的分隔符
systemHosts=(here the list on hosts which is around 700 positions)
openSSHlineLimit=1024
systemHostsLength=${#systemHosts[@]}
systemHostsByteLength=$(echo ${systemHosts[@]} | wc -c)
let divideBy=$systemHostsByteLength/$openSSHlineLimit
let modu=$systemHostsByteLength%$openSSHlineLimit
if [[ $divideBy == 0 ]] && [[ $modu != 0 ]]; then
echo "
host ${systemHosts[@]}
user system
" #> ~/.ssh/config
elif [[ $divideBy == 1 ]] && [[ $modu > 0 ]]; then
let getBreak=$systemHostsLength/2
echo "
host ${systemHosts[@]::$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak}
user system
" # >> ~/.ssh/config
elif [[ $divideBy == 2 ]] && [[ $modu > 0 ]]; then
let getBreak=$systemHostsLength/3
echo "
host ${systemHosts[@]::$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak:$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak*2}
user system
" # > ~/.ssh/config
elif [[ $divideBy == 3 ]] && [[ $modu > 0 ]]; then
echo "
host ${systemHosts[@]::$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:($getBreak:$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak*3:$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak*4}
user system
" # > ~/.ssh/config
elif [[ $divideBy == 4 ]] && [[ $modu > 0 ]]; then
echo "
host ${systemHosts[@]::$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak*2:$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak*3:$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak*4:$getBreak}
user system
" # > ~/.ssh/config
echo "
host ${systemHosts[@]:$getBreak*5}
user system
" # > ~/.ssh/config
fi
那些部分:
${systemHosts[@]:$getBreak*2:$getBreak}
${systemHosts[@]:$getBreak*3:$getBreak}
${systemHosts[@]:$getBreak*4:$getBreak}
并未真正发挥作用 然而这
${systemHosts[@]:$getBreak*2}
工作得很好
我认为表达... [@]:$ var * n:$ var不是允许或我做错了什么?
。 。 。 。 。 。 。 。 。 。 UPDATE/SOLUTION/ 。 。 。 。 。 。 。 。 。 。
嗨。我在这里写的,因为我阻止answaring这个questin:< 脚本没有工作,因为我忘了补充:
let getBreak=$systemHostsLength/<divider>
每个ELIF的开头:/ 对不起,所有的困扰和感谢很多所述answares)
我其实解决了这个问题了。你的方式更清洁更好。非常感谢 – eswues
http://mywiki.wooledge.org/XyProblem ...我可以问你是否有反对upvoting的政策? – gboffi
hehehe。感谢您的链接:P – eswues