Linux, Mac under shell array of the repairman

Source: Internet
Author: User
label:

My tests are basically tested on Mac and Unix environments. Unless otherwise specified, the default is Mac.

No matter if you read this essay by the strangeness of the shell array, or find that the shell array can do so much in a pair of {}, but you ca n’t remember, of course, you may find that whether you use $ * Still [email protected] the array length is 1, what can I do, or nothing, I will sort out the debris from the process of bumping into the wall, and open up the second pulse of your governor

 

There are many types of shells

/ bin / sh (has been replaced by / bin / bash, bash link in Linux)
/ bin / bash (Linux default shell, bash is mainly compatible with sh, and enhanced shell version based on some user needs)
It is generally believed that it was invented by Stephen Bourne in 1979. He wrote the first major shell program for UNIX and named it Bourne Shell. Later, almost all UNIX shells originated from it. Later, a new enhanced version Gradually mature, become Bourne Again Shell (Bash), add a lot of functions on the basis of inheriting the "old" sh, generally this enhancement or expansion is completed by Brian Fox and Chet Ramey
/ bin / csh (has been replaced by / bin / tcsh)
At the same time as Bourne Shell, Bill Joy of the University of California, Berkeley proposed the idea of C Shell. Joy recognized the shortcomings of Bourne shell (cannot run in the background) and decided to design the C Shell with reserved words in C language.
/ bin / ksh (Kornshell developed by AT & T Bell lab., compatible with bash)
At the same time as csh, David Korn of Bell Labs also invented the Korn Shell to fix the deficiencies in the C Shell, and is fully compatible with the Bourne Shell, and the new version of ksh also provides window functionality.
/ bin / tcsh (integrate the C Shell, provide more features)
In 1975, Ken Greer of Carnegie Mellon University was inspired by the TENEX operating system and began to implement Tenex-style file name completion. In 1981, C Shell was integrated. Then in 1983, Mike Ellis added command completion. It was finally released in 1983.
/ bin / zsh (a more powerful shell based on ksh)
The first edition written by Paul Falstad, a student at Princeton University in 1990
 

Bash as standard

A array creation

It is nothing more than a method that almost all programming languages in 2 have, a single assignment, and a (1, 2) assignment together. There are two kinds of abnormal (3,4)

array [0] = one; array [1] = two; array [2] = three # 0-based
array = (one two three) #separated by spaces
array = ([0] = one [1] = two [2] = three) #Separated by spaces, 0-based
array = "one two three" #It is also possible, so the essence of the bash array is exposed, which is a string
B copy array

new_array = ($ old_array_name)
new_array = $ {old_array_name [@]}
new_array = $ {old_array_name [*]}
new_array = ([email protected]) #If it is in a function, it is only B.1, because [email protected] cannot be written as $ {@ [@]}
#Remember this pair of brackets, as long as the string is separated by spaces, add a pair of brackets to change the array, Perl's anonymous array and hashing are used

# The length of the array is the same without adding parentheses, but if it is not added, the first value of the array becomes a string of the real array. A useful track is that when the array is defined, array [0] is empty. From array [1] Start

C array indexing system

$ {array [index]} #The index of most languages is 0-based, Shell also supports 0-based
D array and each element length

$ {# array [@]} #Number of array elements
$ {# array [*]} #Number of array elements
$ {# array [n]} #String length of the nth element
 

For example, to verify the length and subscript I said, the following 4 decibels correspond to the 4 forms of the array part of A,

1 array_str = "ubuntu deepin elementory opensuse fedora centos"
2 array_ot [0] = ubuntu; array_ot [1] = deepin; array_ot [2] = elementory; array_ot [3] = opensuse; array_ot [4] = fedora; array_ot [5] = centos
3 array_ar = (ubuntu deepin elementary opensuse fedora centos)
4 array_bl = ([0] = ubuntu [1] = deepin [2] = elementory [3] = opensuse [4] = fedora [5] = centos)
5
6 array_fstr = ($ array_str)
Then take a look at the length of the various definition arrays, and what the first and second elements are

1 echo ********************* Length *********************
2 echo -e [email protected], $ {# array_str [@]}, "\ t $ {array_str [0]} >>> $ {array_str [1]}"
3 echo -e array_fstr *, $ {# array_fstr [*]}, "\ t $ {array_fstr [0]} >>> $ {array_fstr [1]}"
4 echo -e array_ot, $ {# array_ot [@]}, "\ t $ {array_ot [0]} >>> $ {array_ot [1]}"
5 echo -e array_ar, $ {# array_ar [@]}, "\ t $ {array_ar [0]} >>> $ {array_ar [1]}"
6 echo -e array_bl, $ {# array_bl [@]}, "\ t $ {array_bl [0]} >>> $ {array_bl [1]}"
 The results are as follows, indicating that the length of the four cases is calculated normally.

It can be seen from the results that if the string is referenced as an array, it is regarded as an array of elements (array_str), and after the parentheses are added, it is converted into an array (array_fstr), indicating that all four can be used.

Then in the verification function, the situation is similar, but there are minor differences.

Let's write a function to verify the difference between $ * and [email protected] and whether the copied array is consistent

Like the entire shell script, functions can also be referenced using position variables, $ 1, $ 2, $ 3, $ 4, ...

 1 function say () {
 2 echo -e "**************** From say \ 033 [00m"
 3 echo -e "Length \ [email protected]", $ {# @}, "\ t $ 1 >>> $ 2"
 4 echo -e "Length \ $ *", $ {# *}, "\ t $ 1 >>> $ 2"
 5 args = ([email protected])
 6 echo -e "\ 033 [01; 32m >>>>>>>>> args = (\ [email protected]) \ 033 [00m"
 7 echo -e "Length \ $ args *", $ {# args [*]}, "\ t $ {args [0]} >>> $ {args [1]} >>> $ {args [2] } "
 8 echo -e "Length \ [email protected]", $ {# args [@]}, "\ t $ {args [0]} >>> $ {args [1]} >>> $ {args [2 ]} "
 9 args = [email protected]
10 echo -e "\ 033 [01; 32m >>>>>>>>> args = \ [email protected] \ 033 [00m"
11 echo -e "Length \ $ args *", $ {# args [*]}, "\ t $ {args [0]} >>> $ {args [1]} >>> $ {args [2] } "
12 echo -e "Length \ [email protected]", $ {# args [@]}, "\ t $ {args [0]} >>> $ {args [1]} >>> $ {args [2 ]} "
13 args = ($ *)
14 echo -e "\ 033 [01; 32m >>>>>>>>> args = (\ $ *) \ 033 [00m"
15 echo -e "Length \ $ args *", $ {# args [*]}, "\ t $ {args [0]} >>> $ {args [1]} >>> $ {args [2] } "
16 echo -e "Length \ [email protected]", $ {# args [@]}, "\ t $ {args [0]} >>> $ {args [1]} >>> $ {args [2 ]} "
17 args = $ *
18 echo -e "\ 033 [01; 32m >>>>>>>>> args = \ $ * \ 033 [00m"
19 echo -e "Length \ $ args *", $ {# args [*]}, "\ t $ {args [0]} >>> $ {args [1]} >>> $ {args [2] } "
20 echo -e "Length \ [email protected]", $ {# args [@]}, "\ t $ {args [0]} >>> $ {args [1]} >>> $ {args [2 ]} "
twenty one }
twenty two 
23 echo -ne "\ 033 [01; 31m ==================> \ $ {array_ar [@]}"
24 say $ {array_ar [@]}
25 echo -ne "\ 033 [01; 31m ==================> $ array_str"
26 say $ array_str
 

 

The result analysis is that whether $ * or [email protected] must be parenthesized when copying to the new array, otherwise the first element is covered by the entire array string. We therefore think that the position of the first element Wouldn't it be vacant? But i don't know how to get it

[email protected]: ~ => array_ar = ("ubuntu" "deepin" "elementary" "opensuse" "fedora" "centos"); n = $ {# array_ar [@]}; for ss in `seq 0 $ n `; do echo -e" $ ss \ t $ {array_ar [$ ss]} \ t "$ {# array_ar [$ ss]}; done
0 ubuntu 6
1 deepin 6
2 elementary 10
3 opensuse 8
4 fedora 6
5 centos 6
6 0
E array slice

Perl is accustomed to Perl, and Perl borrows a lot from Bash.

$ {array: m} #All elements starting from the mth element (included) are sliced. They are strings, and the assignments are all strings.
$ {array: m: n} #Cut the m-th element (included) and start a n-element slice, which is a string, and the assignment is a string
new_slice = ((array: m: n))
F element character (string) replacement / deletion

First
$ {array [n | @ | *] / what / replacement} #Match the n + 1th element / all elements to what Replacing what character (string) with replacement will not change the original array
new_array = $ {array [@] / what / replacement}
new_element = $ {array [n] / what / replacement}
To put it simply: the replacement of what-> replacement will be performed on each element you do not specify, but only the first occurrence of what in each element will be replaced. If an element string contains multiple what characters ( String), then only the first one is replaced
Global
$ {array [n | @ | *] // what / replacement} # Match the n + 1th element / all elements to what. Replace the what character (string) with replacement. It will not change the original array.
new_array = $ {array [@] // what / replacement}
To put it simply: it will perform what-> replacement replacement on each element you do not specify, but it will replace every occurrence of what in each element, even if an element string contains multiple what characters (string ) Will be replaced
If replacement is empty, it is deleted. Wildcard matching is not supported.
Fig G.1


 

Fig G.2

H element character (string) deleted

Can use wildcard * (meta character)
$ {array [n | @ | *] # pattern} #Remove the portion of each element that matches the pattern, and the shortest match, if the pattern is u * n, and the element is ubunbunbuntu, then cut ubun, leave bunbuntu, Fig H.1
$ {array [n | @ | *] ## pattern} #Remove the portion of each element that matches the pattern, the longest match, if the pattern is u * n, and the element is ubunbunbuntu, then the ubunbunbun is cut, and the remaining is tu, Fig H.1
$ {array [n | @ | *]% pattern} #Remove the portion of each element that matches the pattern, and the shortest match, if the pattern is n * u and the element is ubunbunbuntu, then the cut is ntu, leaving the ubunbunbu, Fig H.2
$ {array [n | @ | *] %% pattern} #Remove the portion of each element that matches the pattern, and the longest match, if the pattern is n * u and the element is ubunbunbuntu, then the nbunbuntu is cut, and the remaining is ubu, Fig H.2
Please note: the restriction is that it must match a certain head, the # must match the beginning, and the% is = must match the end, that is, you must cut continuously from one head, not a section in the middle, Fig H.3
Fig H.1

Fig H.2

Fig H.3

 

Since strings can be regarded as an array of single elements, the above set is suitable for string processing in Bash

 

Reference:

1. http://bbs.chinaunix.net.sixxs.org/thread-1779167-1-1.html

2. http://www.cnblogs.com.sixxs.org/chengmo/archive/2010/09/30/1839632.html

3. Bird's Linux Private Kitchen Basic Edition (Third Edition)

4. http://en.wikipedia.org/wiki/Tcsh

 

Repairman of Shell Array on Linux, Mac

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.