简体   繁体   English

Bash-创建一个数组并递增每个索引 - 然后是最大的索引

[英]Bash- create an array and increment each index- then the greatest index

know question is not clear at a glance, i have this table: 知道问题一目了然,我有这张表:

   ID Start End
   1  1     4
   2  2     5
   3  4     9
   4  8     10

I want to set these in an order (illustration below). 我想在订单中设置这些(如下图所示)。 I need an array that its indices will increment by one with respect to start and the end positions, and get the greatest index of all. 我需要一个数组,其索引将相对于开始和结束位置递增1,并获得所有的最大索引。 For example: 例如:

1. ####
2.  ####
3.    ######
4.        ### 

so array will be;
    array =(1,2,2,3,2,1,1,2,2,1)

i did not start to write anything because i could not figure whether that is possible with bash. 我没有开始写任何东西,因为我无法确定这是否可能与bash。 please advice.. 请指教..

Just loop over all the elements of each interval: 只需循环遍历每个区间的所有元素:

#! /bin/bash

array=()
while read id start end ; do
    for (( i=start ; i<=end ; i++ )) ; do
        let array[i]++
    done
done << EOF
1  1     4
2  2     5
3  4     9
4  8     10
EOF

echo "${array[@]}"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM