我可以在bash中乘以数组的分隔符

时间:2017-08-08 09:14:56

标签: arrays bash slice multiplication

我需要划分数组。我已经制作了这段代码:

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 是不允许的,或者我做错了什么?

。 。 。 。 。 。 。 。 。 。更新/解决方案/ 。 。 。 。 。 。 。 。 。

您好。我在这里写道,因为我阻止了对这个任务的回答:< 该脚本无效,因为我忘了添加:

let getBreak=$systemHostsLength/<divider>

在每个elif的开头:/ 很抱歉打扰并感谢所有的answares;)

3 个答案:

答案 0 :(得分:1)

您似乎在混合字节长度和数组大小:

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

取决于名称的长度,可以为divideBy和modulo赋予不同的值,如果是branch,则不能执行no。

算术上下文中不需要

$

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

答案 1 :(得分:0)

public class InstallViewModel
{
    public Dictionary<MyFeatureEnum, Feature> Features { get; set; }

    public InstallViewModel()
    {
        // Generate the list of features
        Features = new Dictionary<MyFeatureEnum, Feature>();
        foreach (MyFeatureEnum f in Enum.GetValues(typeof(MyFeatureEnum)))
        {
            Features.Add(f, new Feature(f));
        }
    }
}

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

答案 2 :(得分: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可以计算主机名和子列表的长度,但您可以使用任何宽度。)