An array of bash script programming and random variables

Source: Internet
Author: User
Tags array length

Variable: The memory space used to store the value of existence; The feature is that only one value can be stored in a variable.

Array: a contiguous memory space capable of accommodating multiple array elements, including two types: 1. Sparse Array (bash is a sparse array): The element numbers in the array can be no order; 2. Dense arrays: Elements in an array must be ordered in sequential numbers.

Array elements: Any storage unit in the array that holds the data, which acts as a variable;

There are two types of identity of an element: The index array ID: All array elements are numbered by numbers, using the numeric numbering method;

Usually numeric numbers start from zero, i.e.: 0,1,2 ...

Associative array ID: All array elements can be identified by name or string, (it is possible to support associative arrays in versions above bash4.0 )


Declaration and definition of an array: Declare and define an array with the DECLARE command, directly using variable assignments, or defining array elements separately.

1.declare command:

Common options:

-A: Declares an indexed array, declaring the variable name after it as an indexed array, if supported (the version above bash4.0 is possible);

-A: Declares an associative array, declaring the variable name after it as an associative array, if supported (the version above bash4.0 is possible);


Example:

To define a dense array:

Declare-a names= ("Zhang" "Wang" "Liu" "li")

To define a sparse array:

Declare-a names2= ([0]= ' Zhangsan ' [2]= ' cl ' [3]= ' Mayun ')


2. How to assign values directly using variables:

Define a dense array of indexes:

Array_name= ("value1" "value2" "value3" ...)

To define a sparse index array:

Array_name= ([0]= "value1" [1]= "value2" [2] "value3" ...)

To define an associative array:

Array_name= ([index_name1]= "value1" [index_name3]= "value2" [index_name3]= "Value3" ...)


3. Define the array elements separately:

array_name[0]= ' value1 '

array_name[1]= ' value2 '

...

Array_name[n-1]= ' Value (N) '

Example:

[Email protected] ~]# student_info[0]= ' Xiaoma '

[[email protected] ~]# student_info[1]= ' 22 '

[Email protected] ~]# student_info[2]= ' xxxxxxxxxxxx '

[[email protected] ~]# echo ${student_info[1]}

22


How to reference array elements:

${array_name[index]}

Note: If you do not give a specific index number when referencing an array element, the default number is 0, which displays the value of the first array element;


Refer to all elements in the entire array:

${array_name[*]} or ${array_name[@]}


Index number of all elements referencing the entire array:

${! Array_name[*]} or ${! Array_name[@]}


To view the number of elements in an array (array length):

${#ARRAY_NAME [*]} or ${#ARRAY_NAME [@]}


Whole Group of slices:

${#ARRAY_NAME [*]:offset}

Displays the element that contains the position of the offset value and all subsequent elements thereof;

${#ARRAY_NAME [*]:offset:number}

Displays the value of the element that contains the position of the offset value and its number elements;

Undo Array:

Unset Array_Name

The elements in the array are revoked together and are invalidated;


Random variable:

Random number variable: The random number taken out is an integer value between 0-32767;

Taking random numbers from the entropy pool;

Entropy Pool:

/dev/random

Two times the time interval of hitting the keyboard;

Two time interval of IO;

/dev/urandom (pseudo-entropy pool)

Calculate the random number by using the application;


About taking random numbers, we can't always take random numbers in random, we can't get the values we've already got, so we can use a variable to define a random number: for example, var100= $RANDOM, so we can get the original repetition through Echo $VAR 100来. of random values.





An array of bash script programming and random variables

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.