I need to divide the arrays. I have made this code:
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
those parts:
${systemHosts[@]:$getBreak*2:$getBreak}
${systemHosts[@]:$getBreak*3:$getBreak}
${systemHosts[@]:$getBreak*4:$getBreak}
does not really work however this
${systemHosts[@]:$getBreak*2}
works perfectly well
I it that the expression ...[@]:$var*n:$var is not allowed or I do something wrong?
. . . . . . . . . . UPDATE / SOLUTION / . . . . . . . . . .
Hi. I write in here as I blocked answaring for this questin :< the script did not work because I forgot to add:
let getBreak=$systemHostsLength/<divider>
on the beginning of each elif :/ Sorry for bothering and thanks a lot for all the answares ;)
It seems you are mixing length in bytes and array size :
let divideBy=$systemHostsByteLength/$openSSHlineLimit
let modu=$systemHostsByteLength%$openSSHlineLimit
depending on length of names can give different values for divideBy and modulo, and may not execute no if branch.
$
is not required in arithmetic context:
## example
arr=(host-{1..25})
for ((i=0;i<5;i+=1)); do echo ${arr[@]:5*i:5}; done