簡體   English   中英

Bash-創建一個數組並遞增每個索引 - 然后是最大的索引

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

知道問題一目了然,我有這張表:

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

我想在訂單中設置這些(如下圖所示)。 我需要一個數組,其索引將相對於開始和結束位置遞增1,並獲得所有的最大索引。 例如:

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

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

我沒有開始寫任何東西,因為我無法確定這是否可能與bash。 請指教..

只需循環遍歷每個區間的所有元素:

#! /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