Linux shell to create a sequence array (list, array) method

  

About linux array definition, and generation methods, see: linux shell dynamically generated array series seq tips. My main point here is to efficiently generate list strings, as well as array methods.


First, the seq method generates:

[chengmo@centos5 shell]$ aNumList=$(seq 100);[chengmo@centos5 shell]$ echo $aNumList1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

aNumList gets a string, the difference is separated by: space. In Linux, you can think of it as a list. It can be read by the for…in loop.


[chengmo@centos5 shell]$ for i in $aNumList;do echo $i;done;1234……


if needed To generate an array, you only need to add $(seq 100) to "()”.

[chengmo@centos5 ~]$ aNumList=($(seq 100));[chengmo@centos5 ~]$ echo $aNumList1[chengmo@centos5 ~]$ echo ${#aNumList[@]}100

An array of length 100.


Second, generated by internal {begin..end}

This method is very convenient to generate seq. This is done by internal operators.

[chengmo@centos5 ~]$ echo {1..10}1 2 3 4 5 6 7 8 9 10

[chengmo@centos5 ~]$ for a in {1.. 10};do echo $a;done; 12345678910


Three, performance comparison

[chengmo@centos5 ~]$ time echo {1..100}1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

real 0m0.000suser 0m0.001ssys 0m0.000s


[chengmo@centos5 ~]$ time echo $(seq 100)1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100

real 0m0.003suser 0m0.002ssys 0m0.001s



As you can see from above, {begin. The .end} speed is much faster than the seq call. It can be considered to be completed by internal operators when calling later.

Copyright © Windows knowledge All Rights Reserved