Linux bash shell (16): Array

Source: Internet
Author: User

This article is also the sixth chapter of "learning the bash shell" 3rd edition, command-line options and typed varilables, but we will not limit this article. The array element can be a string or number, the same as the variable. The index of the array starts from 0. The maximum value is 599147937791. Basically, there are no restrictions.

Definition Method

You can assign values directly.

Name [2] = two

Name [0] = zero

Name [1] = one

As in the preceding method, we do not assign values in order.Name = ([2] = alice [0] = hatter [1] = duchess)

. If we assign values in order, we do not need to describe the index,Name = (a B c)

. If one of us does not assign values in turn, you can specify, as shown in figureName = (a [3] = B c)

, B is assigned to the Index = 3 element, and C is assigned to the next one in order, that is, Index = 4.

If we define an empty array, we can useDeclare-Name

. Variable$ {Array

[I

]}

If no index is specified, the system returns 0th elements.

Reset and cancel

We use the combination method to define, that is, array = (...) If an array has been defined before, the entire array will be reset.

If you want to cancel an element, you can useUnsetArray

[I]

To display the entire array, you can useUnsetArray

, OrUnsetArray

[@]

AndUnsetArray

[*]

.

@, * And #

Like the location parameter, you can also use @ and *. When using *, the interval is ifs. In the preceding example, Echo $ {name [@]} is Zero One Two. This can be done in the For Loop:

For entry in "$ {name [@]}"; do

Echo -- $ entry --

Done

If the index does not exist and no value is assigned, null is returned. These elements are not included in $ {array [@]} and only valid values are returned. For example:

Table = ([1] = one [3] = three)
#$ {Array

[@]} Get all Element Content

Echo $ {table [@]}
#$ {!Array

[@]} Obtains the sequence numbers of all elements.

Echo $ {! Table [@]}

For entry in $ {table [@]}; do

Echo = $ entry =

Done
The running result is as follows:

One three

1 3

= One =

= Three =

You can useFor (I = 0; I <= 3; I ++ ))

.

# Is the length operation,$ {#Array

[I

]}

Returns the length of the string of the I element. While$ {#Array

[@]}

Returns the format of valid elements. For example, in the preceding example, $ {# table [@]} is 2.

Example 1

: Use/etc/passwd to obtain the user name through the user ID:

For entry in $ (cut-f 1, 3-d:/etc/passwd); do

# $ {Entry # *:} is the preceding user ID, and $ {entry %: *} is the user name

Echo "set array [$ {entry # *:}] =$ {entry % :*}"

Array [$ {entry # * :}] =$ {entry % :*}

Done

Echo "User ID $1 is $ {array [$1]}."

Echo "there are currently $ {# array [@]} user accounts on the system ."

Example 2

: Bubble Algorithm

# Setting the array values

Values = (39 5 36 12 9 3 2 30 4 18 22 1 28 25)

# Obtain the number of values

Numvalues =$ {# values [@]}

# Display the value of the current array for tracking

Function showvalues

{

For (k = 0; k <numvalues; k ++); do

Echo-ne "$ {values [$ K]}"

Done

Echo

}

# Bubble Algorithm

: Vertically sorts the sorted record array R [1. n]. Each record R is considered as a bubble with the weight of R. key. According to the principle that a Light Bubble cannot be under a heavy bubble, scan the array R from the bottom up: Any Light Bubble scanned to a violation of this principle will make it "float" up ". This is repeated until the last two bubbles are light and heavy.

Function bubble

{

For (I = 0; I <numvalues; I ++); do

Ischanged = false

For (j = numvalues-1; j> I; j --); do



# If the switch is light, every cycle of I can see the lightest place at the top, and adjust other positions to make it more orderly

If [$ {Values [j]}-lt $ {values [J-1]}

]; Then

Temp =$ {values [j]}

Values [j] =$ {values [J-1]}

Values [J-1] = $ temp

Ischanged = true

Fi

Done

Showvalues
# If no adjustment is required, that is, the Order will jump out of the loop.

If [ischanged = "false"]; then

Break;

Fi

Done

}

Related Links: My articles on Linux operations

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.