Array of shell script programming

Source: Internet
Author: User


Variable: Stores the memory space of a single element;

Array: A contiguous memory space that stores multiple elements;

Array name: The entire array has only one name;

Array index: numbering starting from 0;

Array name [index],

${array_name[index]}

Associative arrays:

Note: Bash-4 and later versions support custom index formats, not just 0,1,2, ... Number format;

Such arrays are called "associative arrays".

Declaring an array:

Declare-a NAME: declares an indexed array;

Declare-a NAME: declares an associative array;

How the elements in the array are assigned:

(1) Assign only one element at a time;

Array_name[index]=value

(2) An assignment of all elements at once;

Array_name= ("VAL1" "VAL2" "VAL3" ...)

(3) assigning only specific elements;

Array_name= ([0]= "VAL1" [3]= "VAL4" ...)

NOTE: Bash supports sparse-format arrays;

(4) Read-a Array_Name

Referencing an element in an array: ${array_name[index]}

Note: When referencing, only the array name is given, which refers to the element that is labeled 0;

The length of the array (the number of elements in the array):

${#ARRAY_NAME [*]}

${#ARRAY_NAME [@]}

Example: Generate 10 random numbers and find the maximum and minimum values;

#!/bin/bash#declare-a randdeclare-i max=0for i in {0..9}; Do rand[$i]= $RANDOM echo ${rand[$i]} [${rand[$i]}-gt $max] && max=${rand[$i]}doneecho "Max: $max"

Exercise: Write a script

Defines an array in which the elements in the array are all files that end with. Log in the/var/log directory, and the sum of the number of rows in the file whose subscript is even;

#!/bin/bash#declare-a filesfiles= (/var/log/*.log) declare-i lines=0for i in $ (seq 0 $[${#files [*]}-1]); do if [$[$i%2]-eq 0]; Then let lines+=$ (Wc-l ${files[$i]} | cut-d '-f1) Fidoneecho "lines: $lines."

To reference all elements in an array:

${array_name[*]}

${array_name[@]}

Array element slice: ${array_name[@]:offset:number}

Offset: The number of elements to pass;

Numbers: The number of elements to be taken out, and the amount of all elements after the offset is omitted;

To append an element to a non-sparse format array:

array_name[${#ARRAY_NAME [*]}]=

To delete an element in an array:

Unset Array[index]

Associative arrays:

Declare-a Array_Name

Array_name= ([index_name1]= "value1" [index_name2]= "value2" ...)


This article is from the "Wang Liming" blog, make sure to keep this source http://afterdawn.blog.51cto.com/7503144/1916071

Array of shell script programming

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.