2017-08-08 7 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はありません許可されるか、私は何か間違っている?

。 。 。 。 。 。 。 。 。 。更新/解決/ 。 。 。 。 。 。 。 。 。 。

こんにちは。私はこのquestinためanswaringをブロックされて私はここに書く:< 私は追加するのを忘れているため、スクリプトが動作しませんでした:

let getBreak=$systemHostsLength/<divider> 

各ELIFの初めに:すべてのために多くを悩まと感謝のために/ は申し訳ありません; answares)

答えて

0

バイト配列のサイズの長さを混合していると思われる:divideByモジュロに対して異なる値を与えることができる名前の長さに応じ

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

、及びnを実行しなくてもよいです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

関連する問題