Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to manipulate shell arrays

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article focuses on "how to operate the shell array", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to manipulate shell arrays.

1. Array declaration:

The code is as follows:

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

2) declare-an array # array is used as the array name

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

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

5) array= "one two three" # echo ${array [0 | @ | *]}, treat the array variable as an array, but the array element is only the string itself

two。 Array access:

The code is as follows:

1) ${array [key]} # ${array [1]}

3. Deletion of array

The code is as follows:

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

2) unset array # delete the entire array

4. Calculate the length of the array:

The code is as follows:

1) ${# array}

2) ${# array [0]} # Ibid. ${# array [*]}, ${# array [@]} Note the difference with # {array:0}

5. Extraction of array

Extract from the tail:

The code is as follows:

Array= ([0] = one [1] = two [2] = three [3] = four)

${array [@]: 1} # two three four, all elements after the first element is removed, then ${array [@]: 0} represents all elements

${array [@]: 0:2} # one two

${array [@]: 1:2} # two three

6. Substring deletion

The code is as follows:

[root@localhost dev] # echo ${array [@]: 0}

One two three four

[root@localhost dev] # echo ${array [@] # tweee} # the shortest match begins on the left: "tweee", which will match to "thre"

One two e four

[root@localhost dev] # echo ${array [@] # # tweee} # the longest match begins on the left, which will match to "three"

[root@localhost dev] # array= ([0] = one [1] = two [2] = three [3] = four)

[root@localhost dev] # echo ${array [@]% o} # shortest match starting at the end of the string

One tw three four

[root@localhost dev] # echo ${array [@]% o} # longest match starting at the end of the string

One tw three four

7. Substring substitution

The code is as follows:

[root@localhost dev] # array= ([0] = one [1] = two [2] = three [3] = four) [code]

The first match will be deleted.

[code] [root@localhost dev] # echo ${array [@] / oago}

Mne twm three fmur

All matches will be deleted.

The code is as follows:

[root@localhost dev] # echo ${array [@] / / obox}

Mne twm three fmur

If no replacement substring is specified, the matching substring is deleted

The code is as follows:

[root@localhost dev] # echo ${array [@] / / o /}

Ne tw three fur

Replace the front terminal string of the string

The code is as follows:

[root@localhost dev] # echo ${array [@] / # oplink}

Kne two three four

Replace the string after the terminal string

The code is as follows:

[root@localhost dev] # echo ${array [@] /% oplink}

One twk three four

At this point, I believe you have a deeper understanding of "how to manipulate shell arrays". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report