Array of Shell programming

Source: Internet
Author: User



Array, arrays

Variable: Stores the memory space of a single element;
Array: Memory space for storing multiple elements;

Array name: The entire array has only one name;
Array index: numbering starting from 0;
Array name [index]
Array[index]

Value:
${array[index]}
If index number is not specified, the element with index 0 is returned by default;

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:
#help Declare

-A to make NAMEs indexed arrays. declares an indexed array;
-A to make NAMEs associative arrays. declares an associative array;

How elements in an array are assigned: (no prior declaration required)

(1) Assign only one element at a time;
Array_name[index]=value
#animals [0]=pig
#animals [1]=dog
#echo a $animals #若不加索引号, the value of the first element is returned by default;
#echo ${animals[1]} #返回第二个元素的值;

(2) Assign all elements at once (continuous assignment)
Array_name= ("value1" "value2" "value3" ...)
#weekdays = ("Monday" "Tuesday" "Wednesday")
#echo ${weekdays[2]}

Connect an array, splicing several arrays:
#arrayN = (${array1[@]} ${array2[@] ...})

(3) Assign only specific elements: (sparse format array, discontinuous assignment)
Array_name= ([0]= "value1" [3]= "Value4" ...)
#sword = ([0]= "Yitian" [3]= "Longquan")
#echo ${sword[3]}

(4) Read-a Array_Name
#read-A Jianghu
Yuebuqun Dongfangbubai Qqianchaotaijian
#echo ${jianghu[1]}
Dongfangbubai

The length of the array (the number of elements in the array):
${#ARRAY_NAME [*]}
${#ARRAY_NAME [@]}

#echo ${#world [*]}
#echo ${#world [@]}

If index number is not indexed, the character length of the first element is returned:
#echo ${#animals}
3

If you do not add #, all elements in the array are returned:
#echo ${animals[*]}
#echo ${animals[@]}
Pig Dog

${array[@]} outputs an element value separated by whitespace;
${ARRAY[*]} outputs an entire string;

Example: Generate 10 random numbers to find the maximum value;
#vim rand.sh
#!/bin/bash
#
Declare-a Rand
Declare-i max=0
For i in {0..9};d o
rand[$i]= $RANDOM
echo ${rand[$i]}
[${rand[$i]}-gt $max] && max=${rand[$i]}
Done
echo "MAX: $max"

Defines an array in which the elements in the array are all files that end with. Log in the/var/log directory;
Count the sum of the rows of the file whose index is even;
#vim array1.sh
#!/bin/bash
#
declare-a files
Files= (/var/log/*.log)
Declare-i lines=0
For i in ' seq 0 $[${#files [*]}-1] ';d o
If [$[$i%2]-eq 0];then
Let lines+= ' Wc-l ${files[$i]} | awk ' {print '} '
Fi
Done
echo "Lines: $lines"

Array element slices:

${array_name[@]:offset:number}
Offset: The number of elements to skip;
Numbers: The number of elements to be taken out, and the amount of all elements after the offset is omitted;

#files = (/etc/[pp]*)
#echo ${files[*]}
/etc/packagekit/etc/pam.d/etc/passwd/etc/passwd-/etc/php.d/etc/php.ini/etc/phpmyadmin/etc/php-tcpdf/etc/ Pinforc/etc/pkcs11/etc/pki/etc/plymouth/etc/pm/etc/polkit-1/ETC/POPT.D/ETC/POSTFIX/ETC/PPP/ETC/PRELINK.CONF.D/ Etc/printcap/etc/profile/etc/profile.d/etc/protocols/etc/pulse/etc/puppet/etc/python
#echo ${files[@]:2:3} #跳过2个, fetch three;
/etc/passwd/etc/passwd-/ETC/PHP.D
#echo ${files[@]:5} #跳过5个, take back all;

Append an element to the array:
array_name[${#ARRAY_NAME [@]}]=value

To delete an element in an array:
Unset Array[index]

Delete the entire array:
Unset Array

Associative arrays, which need to be declared beforehand;
Declare-a Array_Name
Array_name= ([index_name1]= "value1" [index_name2]= "value2" ...)

#declare-A World
#world [us]= "America"
#echo "${world[us]}"
#world [uk]= "Kingdom"
#echo "${world[uk]}"

Array of Shell 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.