Using linux shell arrays

introduction

  Work on the Linux platform, we often need to use the shell to write some useful and meaningful scripts. Sometimes, often using shell arrays. So, shell in the array is how the performance of it, how is it defined? Then one by one were to explain the array, shell in.

Array definition

  What is an array? Studied computer programming language students know the characteristics of the array is a group of the same set of data types (not including some programming language concepts put forward by the associative array). Then the shell in the array is how to define it, we look at two types of data: one numeric type, the second is a string type; although the shell itself is kind of weak, but could be so distinguished.

  An array of numeric types: a pair of parentheses represents the array, using the "space" between the array elements to separate.

  For Liezi: 

  arr_number=(1 2 3 4 5);

  Type String Array: Similarly, a pair of parentheses array, where the array elements in double or single quotes comprising, using the same "space" to separate.

  arr_string=("abc" "edf" "sss"); 或者 arr_string=('abc' 'edf' 'sss');

Operation array

  We use the value type array arr_number = (1 2 3 4 5) as the source array related explanation: acquiring the array length, to read a target value, the assignment of an index, deletions, and substitutions and allocation traversal. To mention a knowledge point, we want to get the value of a variable, using the beginning of the $ symbol in the shell, such as: $ a or $ {a} can be.

  Gets an array of length

  arr_length = $ {# arr_number [*]} or $ {# arr_number [@]} can, i.e. the form: $ {# array name [@ / *]} of the length of the array can be obtained.

  Read a subscript value

  arr_index2 = $ {arr_number [2]}, i.e. the form: $ {array name [index]}

  Assigned to one of the indexes

  We should ask two questions:

    The first question is if the index elements already exist, what will happen?

    A: modifies the value of the subject under the new designation.

    For example: arr_number [2] = 100, the array is modified (1210045)

    The second problem is that if the specified current index has exceeded the size of the array, as described above arr_number size is 5, any specified index value greater than 10, or 11, or of how 5?

    A: The value of the new Fu is appended to the end of the array.

    For example: arr_number [13] = 13, the array is modified (121,004,513)

  Deletion

    Clear an element: unset arr_number [1], where subscript 1 is cleared to the array;

    Empty the entire array: unset arr_number;

  Fragmentation access

    Access fragment form: $ {array name [@ or *]: Start index:} superscript end, note that the end value is not included in the standard elements.

    For example: $ {arr_number [@]: 1: 4}, where access from slice beginning at index 1, the number of elements is four.

  Replace mode

    The form: $ {array name [@ * or] / pattern / new value}

    For example: $ {arr_number [@] / 2/98}

  Iterate

    We use the array traversal for statement to demonstrate:

    for v in ${arr_number[@]}; do

      echo $v;

    done

 

Guess you like

Origin www.cnblogs.com/ZFBG/p/10985209.html