Shell scripting in Linux-arrays, advanced string manipulation, advanced variables, configuring user environments

Source: Internet
Author: User

Overview:

Overview:

This section describes the array of shell scripting in Linux, advanced string processing, advanced variables, and configuring the user's environment.

One, function and positional parameter extension

1. TheShift command implements the jump in the position parameter, which squeezes the leftmost parameter

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/86/5B/wKiom1e9EFvAun-AAABG0xL4ac4082.png "title=" Shift.png "alt=" Wkiom1e9efvaun-aaabg0xl4ac4082.png "/>650" this.width=650; "src=" http://s3.51cto.com/wyfs02/M01 /86/5a/wkiol1e9ehfh0q31aabeyubmj2s145.png "title=" anonymous function. png "alt=" Wkiol1e9ehfh0q31aabeyubmj2s145.png "/> 2. Use of anonymous functions

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/86/5B/wKiom1e9EJ7T2uxuAABeYUBmj2s109.png "title=" anonymous function. png "alt=" Wkiom1e9ej7t2uxuaabeyubmj2s109.png "/>


First article: Arrays

I. Definition of an array:

1. Variables: Storing The memory space of a single element

2. array: a contiguous memory space that stores multiple elements, equivalent to a collection of multiple variables .

3. array names and indexes

Index: Numbering starting from 0, which is a numeric index

Note: Indexes can support the use of custom formats, not just numeric formats, which are associated with indexes, which are supported after the bash4.0 version.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/86/5B/wKioL1e9E_iQiZQFAAARvczYo4w561.png "title=" Array. png "alt=" Wkiol1e9e_iqizqfaaarvczyo4w561.png "/> Bash array supports sparse format (index discontinuity)

4. declaring an array:

declare-a array_name

declare-a array_name: Associative array (must first be declared and reused)


Second, the assignment of array elements

1. Assign only one element at a time;

Array_name[index]=value

weekdays[0]= "Sunday"

Weekdays[4]= "Thursday"

Example:

Declare-a Menu It is best to declare the array first

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/86/5C/wKiom1e9HlvhHVb2AAAin5qq3zY822.png "title=" 1.png " alt= "Wkiom1e9hlvhhvb2aaain5qq3zy822.png"/>

2. Assign all elements at once:

Array_name= ("VAL1" "VAL2" "VAL3" ...)

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/86/5B/wKioL1e9Hm2TBcVKAAAunz6hUdY852.png "title=" 2.png " alt= "Wkiol1e9hm2tbcvkaaaunz6hudy852.png"/>

The following methods can also be used: command references, glob wildcard characters, etc.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/86/5C/wKiom1e9Ilnh0e_iAABpYx7MPjs921.png "style=" float: none; "title=" 3.png "alt=" Wkiom1e9ilnh0e_iaabpyx7mpjs921.png "/>

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/86/5B/wKioL1e9IlrSP5PiAAAT_5qok-U232.png "style=" float: none; "title=" 4.png "alt=" Wkiol1e9ilrsp5piaaat_5qok-u232.png "/>


3. Assign only specific elements:

Array_name= ([0]= "VAL1" [3]= "VAL2" ...)

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/86/5B/wKioL1e9I1bzv37pAAAS_bteqac866.png "title=" 5.png " alt= "Wkiol1e9i1bzv37paaas_bteqac866.png"/>

4. Assignment of interactive array value pairs

Read-a ARRAY

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/86/5C/wKiom1e9I2PRXyzDAAAaiWn7Ttk127.png "title=" 6.png " alt= "Wkiom1e9i2prxyzdaaaaiwn7ttk127.png"/>


Three, reference array

1. Referencing an array element:${array_name[INDEX]}

Note: Omitting [INDEX] means referencing an element with subscript 0

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/86/5C/wKioL1e9JTvCuYeEAABAEwtZ9m8632.png "title=" 7.png " alt= "Wkiol1e9jtvcuyeeaabaewtz9m8632.png"/>

2. The length of the array (the number of elements in the array):

${#array_name[*]}

${#array_name[@]}

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/86/5C/wKioL1e9J5aAPipcAAA3soR2Sus340.png "title=" 8.png " alt= "Wkiol1e9j5aapipcaaa3sor2sus340.png"/>

In fact, {} is a variable, ${#VAR} represents the length of the variable, the previous $#, indicating the number of arguments passed to the script.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/86/5C/wKioL1e9KFCjPcGvAAAjJGPE6iY237.png "title=" 9.png " alt= "Wkiol1e9kfcjpcgvaaajjgpe6iy237.png"/>


Practice :

1. Generate 10 random numbers to save in the array and find their maximum and minimum values

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/86/5F/wKiom1e9POyhvlycAAC41Y8S6-4851.png "style=" float: none; "title=" 11.png "alt=" Wkiom1e9poyhvlycaac41y8s6-4851.png "/>

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/86/5E/wKioL1e9POzy433dAAAX6h0FcFI242.png "style=" float: none; "title=" 11-1.png "alt=" Wkiol1e9pozy433daaax6h0fcfi242.png "/>

2. Write a script that defines an array in which the elements in the array are all files ending in. Log in the/var/log directory, and the sum of the number of rows in the file to be labeled even.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/86/60/wKiom1e9SPOikLH2AAB8KX52Dg4569.png "title=" 12.png "alt=" Wkiom1e9spoiklh2aab8kx52dg4569.png "/>

Shell scripting in Linux-arrays, advanced string manipulation, advanced variables, configuring user environments

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.