Array and string processing

Source: Internet
Author: User
Tags first string


Array: A contiguous memory space that stores multiple elements, equivalent to a collection of multiple variables

1) define the array:
Declare-a Array_Name defines an ordinary array can be omitted declare-a

] #arr = (a b c D E F)

Declare-a Array_Name defines an associative array to customize the index for characters, strings, and so on

2) Index:
Similar to the C language, the subscript of an array element is numbered starting with 0. The associated array is determined by the corresponding key

3) array element assignment:
1. An element of an attached value:
Array_name[0]=value0

2. Assign all elements at once: the middle space is separated, and if a value contains a space, quotation marks are required
Array_name= ("VAL1" "VAL2" "VAL3" ...)

3. Assigning only specific elements: a discrete assignment
Array_name= ([0]= "VAL1" [3]= "VAL2" ...)

4. Assignment of interactive array value pairs: non-piped data
Read-a ARRAY

4) The number of length elements in an array is non-indexed
${#ARRAY_NAME [*]}
${#ARRAY_NAME [@]}

5) Array Operation:
1. Refer to the elements in the array:

${array_name[index]} omit [INDEX] to refer to the first element by default, i.e. Arr[0]
Using the subscript to get the specified element in the array, the subscript can be an integer or an arithmetic expression whose value should be greater than or equal to 0.

All elements: ${array[@]}, ${array[*]}

Array slice: ${array[@]:offset:number}
Offset: Number of elements to skip
Number: How many elements to remove
All elements after the offset is taken
${array[@]:offset}

] #echo ${arr[*]:2:3}c d E

  displays an array of 3 elements after the 2nd element of arr
 
    2. Specifies that an element in the array be deleted
        unset Array[index]
   
  
Practice bubbling sort:
 

#!/bin/bash#zjjfor ((i=0;i<10;i++));d o rand[$i]= $RANDOMdoneecho ${rand[@]}echo---------------------for ((j=0;j            <10;j++));d O for ((k=j;k<9;k++));d o if [${rand[$j]}-gt ${rand[$[k+1]}];then tmp=${rand[$j]}  rand[$j]=${rand[$[k+1]} rand[$[k+1]]= $tmp else continue fi donedoneecho ${rand[@]}




======>
String processing:


Show only changes, no changes to variable values

] #var =123456

1) string slices:
${#var}: Returns the length of the string variable var

]# Echo ${#var}6

${var:offset}: Returns the string variable var after all characters from the first offset character
Offset value between 0 and ${#var}-1

]# Echo ${var:2}3456

${var:offset:number}: Returns the string variable var in specifier number characters from the first offset character

]# Echo ${var:2:3}345

${var:-lengh}: Take the rightmost few characters of a string
Note: You must have a blank character after a colon

]# echo ${var: -2}56

${var:offset:-lengh}: From the leftmost offset character to the rightmost lengh character
(negative values are allowed after bash4.2)

]# echo ${var:2: -1}345

2) sub-string based on pattern

] #var =root:x:0:0:root:/root:/bin/bash


1. From left to right:
${var#*word}: Where word can be any of the specified characters
Delete the first occurrence of Word and its preceding characters

]# Echo ${var#*root}:x:0:0:root:/root:/bin/bash

${var##*word}:
Ditto, the difference is, the deletion to the last match to the character

]# Echo ${var##*root}:/bin/bash

2. From right to left:
${var%word*}: Where word can be any of the specified characters;
Delete the first word from right to left and its subsequent characters

]# Echo ${var%root*}root:x:0:0:root:/

${var%%word*}:
Ditto, different, delete all characters from right to left last occurrence of word and its after

]# Echo ${var%%root*}

3) Find replacement, delete:
${var/pattern/substi}:
The first string to be matched to by pattern, replacing it with the Substi
${var//pattern/substi}:
All strings that are matched to the pattern, replaced by Substi
${var/#pattern/substi}:
The string to which the beginning of the line is matched by the pattern, replacing it with Substi
${var/%pattern/substi}:
The string to which the end of the line is matched by the pattern, replacing it with the Substi

Delete:
${var/pattern}: Find var value, delete the first string matched by pattern
${var//pattern}: All
${var/#pattern}: Beginning of the line
${var/%pattern}: End of line

4) Character-case conversion:
${var^^}: Converts all lowercase letters in VAR to uppercase
${var,}: Converts all uppercase letters in VAR to lowercase

Variable assignment:

${var:-value}: Returns value if Var is empty or not set, otherwise returns VAR
${var:+value}: Returns value if Var is not empty, otherwise returns a null value
${var:=value}:var is empty or not set, returns value and assigns value to Var; otherwise, the value of VAR is returned
${var:?error_info}:var is empty or not set, the current terminal prints error_info; otherwise, the value of VAR is returned


#declare [Options] Variable name
-r Set Variable to read-only property
-I defines a variable as an integral number
-a defines a variable as an array
-a defines a variable as an associative array
-F Displays all function names and their contents defined before this script
-F displays only all function names defined before this script
-X declares a variable as an environment variable
-L Converts the value of the variable to a lowercase letter declare–l var=upper

-U converts variable value to capital letter Declare–u Var=lower


Indirect variable references:
The value of the first variable is the name of the second variable,
The value of the second variable referenced from the first variable is called an indirect variable reference
Var1=var2
Var2=value
At this point the value of print var1 is var2, not value
The bash shell provides two formats for implementing indirect variable references
Eval var3=\$ $var 1 eval command to scan a variable two times
Echo ${!var1}

]# var1=var2]# var2=zjj]# Echo ${!var1}zjj]# eval var3=\$ $var 1]# echo $var 3zjj


#mktemp [OPTION] ... [TEMPLATE] creates temporary files (random filenames) to avoid conflicts
-D Create temp directory
-P DIR Specifies the location of the directory where the temporary files reside

TEMPLATE:filename.XXX
x must appear at least three, representing a few random characters


#install installation copy files are commonly used to directly grant permissions to a binary file when compiled and installed
-M MODE Specify permissions by default 755
-O Owner Designate owner
-G group designation genus








This article is from the "mediocre" blog, please be sure to keep this source http://zzjasper.blog.51cto.com/9781564/1841356

Array and string processing

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.