2017-08-08 50 views
0

我需要划分数组。我有此代码:我可以在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)

回答

0

似乎你以字节为单位和阵列尺寸混合长度:

let divideBy=$systemHostsByteLength/$openSSHlineLimit 
let modu=$systemHostsByteLength%$openSSHlineLimit 

取决于名称的长度可以给分除法和模数的不同值,并且可以不执行ñ o如果分支。

$未在算术上下文需要:

## example 
arr=(host-{1..25}) 
for ((i=0;i<5;i+=1)); do echo ${arr[@]:5*i:5}; done 
0
${systemHosts[@]:getBreak*2:getBreak} 

应该更好(长度和偏移是算术表达式)。

0

你会对范例转变感兴趣吗?

$ echo host{1..25}|fmt -w40|while read line ; do echo " 
host $line 
    user system 
"; done 

host host1 host2 host3 host4 host5 host6 
    user system 


host host7 host8 host9 host10 host11 host12 
    user system 


host host13 host14 host15 host16 host17 
    user system 


host host18 host19 host20 host21 host22 
    user system 


host host23 host24 host25 
    user system 

$ 

(40的宽度选择,让你算主机名子列表的长度,但你可以使用任何你喜欢的宽度)。

+0

我其实解决了这个问题了。你的方式更清洁更好。非常感谢 – eswues

+0

http://mywiki.wooledge.org/XyProblem ...我可以问你是否有反对upvoting的政策? – gboffi

+0

hehehe。感谢您的链接:P – eswues

相关问题