Use shell array, good, keep learning later

Source: Internet
Author: User

I don't know when I wrote something, But I was found by archaeology when I sorted out the documents. I just smiled at those people who were bored. If the errors in this article cause all mental losses to you, contact the insurance company! Of course you can tell me (Talk To Me)

Array as a special {
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Tagshow (Event)
}
}
}
} "> Data structures have a place in any programming language. Of course, bash {
Function onclick ()
{
Function onclick ()
{
Function onclick ()
{
Tagshow (Event)
}
}
}
} "> Shell is no exception. This article makes a small summary of the shell array.
Here we will only discuss the situation of one-dimensional arrays, with regard to multi-dimensional arrays (in fact, you have to use a one-dimensional array method to simulate), not involved. This includes array copying, calculation, deletion, and replacement.

Array declaration:

  1. 1) array [Key] = value # array [0] = one, array [1] = two

Copy code

  1. 2) declare-A array # array is treated as the array name

Copy code

  1. 3) array = (value1 value2 value3 ...)

Copy code

  1. 4) array = ([1] = one [2] = two [3] = three ...)

Copy code

  1. 5) array = "One Two Three" # echo $ {array [0 | @ | *]}. The array variable is treated as an array, but the array element only contains the string itself.

Copy code

Array access:

  1. 1) $ {array [Key] }#$ {array [1]}

Copy code

Delete An Array

  1. 1) unset array [1] # Delete the first element in the array

Copy code

  1. 2) unset array # Delete the entire array

Copy code

Calculate the length of the array:

  1. 1) $ {# array}

Copy code

  1. 2) $ {# array [0]} # Same as above. $ {# Array [*]} and $ {# array [@]}. Note the difference between # {array: 0} and # {array: 0 }.

Copy code

Array Extraction
Extract from the tail:
Array = ([0] = one [1] = two [2] = three [3] = Four)
$ {Array [@]: 1} # two three four. After removing all elements from the first element, $ {array [@]: 0} indicates all elements.
$ {Array [@]: 0: 2} # One Two
$ {Array [@]: 1: 2} # two three

Delete a substring

  1. [Root @ localhost Dev] # echo $ {array [@]: 0}
  2. One two three four

Copy code

  1. [Root @ localhost Dev] # echo $ {array [@] # T * e} # Start the shortest match on the left: "T * E", which matches "thre"
  2. One Two e four

Copy code

  1. [Root @ localhost Dev] # echo $ {array [@] # T * e} # The longest match from the left, which matches "three"

Copy code

  1. [Root @ localhost Dev] # array = ([0] = one [1] = two [2] = three [3] = Four)

Copy code

  1. [Root @ localhost Dev] # echo $ {array [@] % O} # minimum matching from the end of a string
  2. One TW three four

Copy code

  1. [Root @ localhost Dev] # echo $ {array [@] % O} # longest match starting from the end of the string
  2. One TW three four

Copy code

Substring replacement

  1. [Root @ localhost Dev] # array = ([0] = one [1] = two [2] = three [3] = Four)

Copy code

The first matched one will be deleted.

  1. [Root @ localhost Dev] # echo $ {array [@]/O/m}
  2. Mne twm three fmur

Copy code

All matched items will be deleted.

  1. [Root @ localhost Dev] # echo $ {array [@] // o/m}
  2. Mne twm three fmur

Copy code

If the child string is not specified, the matched child string is deleted.

  1. [Root @ localhost Dev] # echo $ {array [@] // o /}
  2. Ne TW three fur

Copy code

Replace the first terminal string

  1. [Root @ localhost Dev] # echo $ {array [@]/# O/k}
  2. Kne two three four

Copy code

Terminal string after replacement string

  1. [Root @ localhost Dev] # echo $ {array [@]/% O/k}
  2. One twk three four
Copy code $ Arr = (123 34 3 5)
$ Echo $ arr // obtain the first element by default.
> 123
$ Echo $ {arr [1]} // access through subscript
> 34
$ Echo $ {arr [@]} // access the entire array, @ or * get the entire array
> 123 34 3 5
$ Echo $ {# arr [@]} // obtain the length of the array (maximum subscript), # obtain the length array is the last subscript
> 3
$ Echo $ {# arr [3]} // obtain the string length
> 1
$ Echo $ {arr [@]: 1: 2} // obtain part of the array content by Slicing
> 34 3
$ Echo $ {arr [@]: 2} // starts with the second element.
> 3 5
$ Echo $ {arr [@]: 2} // to the second element
> 123 34
Reference http://www.tech-recipes.com/rx/642/bash-shell-script-accessing-array-variables/
Array simulation operation
Http://www.tech-recipes.com/rx/911/queue-and-stack-using-array/
Push:
Array = ("$ {array [@]}" $ new_element)
Pop:
Array = ($ {array [@]: 0 :$ ($ {# array [@]}-1 ))})
Shift:
Array = ($ {array [@]: 1 })
Unshift
Array = ($ new_element "$ {array [@]}")
Function del_array {
Local I
For (I = 0; I <$ {# array [@]}; I ++ ))
DoS
If ["$1" = "$ {array [$ I]}"]; then
Break
Fi
Done
Del_array_index $ I
}
Function del_array_index {
Array = ($ {array [@]: 0: $1 }$ {array [@]: $($1 + 1 ))})
}
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.