Linux Basic Learning Day 13th (array, string variable handling)

Source: Internet
Author: User

2016-08-22

Content of the lesson:

Array

Advanced string manipulation


One, array

Variables: Storing the memory space of a single element

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

Array name and Index

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

The array of Bash supports sparse format (index discontinuity)


1. Declare an array:

Declare-a Array_Name (not mandatory declaration, but preferably declared by specification)

Declare-a array_name: Associative array

2. Assigning values to array elements:

(1) Assign only one element at a time;

Array_name[index]=value

[19:41 [Email protected]~]# arr= (1 2 3 4 5) [19:41 [Email protected]~]# echo ${arr[@]}1 2 3 4 5[19:41 [email protected]~]# arr[5]=88[19:41 [Email protected]~]# echo ${arr[@]}1 2 3 4 5 88[19:41 [email protected]~]# arr[10]=188[19:42 [email Protec ted]~]# Echo ${arr[@]}1 2 3 4 5 88 188

(2) Assign all elements at once:

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

[19:41 [Email protected]~]# arr= (1 2 3 4 5) [19:41 [Email protected]~]# echo ${arr[@]}1 2 3 4 5


3. Reference array:

Reference array element: ${array_name[index]}, note: Omitting [INDEX] means referencing an element with subscript 0

[19:43 [Email protected]~]# echo ${arr[@]}1 2 3 4 5 188[19:43 [email protected]~]# echo ${arr[1]}2

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

${#ARRAY_NAME [*]}

${#ARRAY_NAME [@]}

[19:44 [email protected]~] #echo ${arr[@]}1 2 3 4 5 188[19:44 [email protected]~]# echo ${#arr [@]}7[19:44] [email protect ed]~]# echo ${#arr [*]}7


4. Array data processing: slicing

Array slice: ${array[@]:offset:number}

Offset: Number of elements to skip

Number: How many elements to remove

All elements after the offset ${array[@]:offset}

[19:46 [Email protected]~]# echo ${arr[@]}1 2 3 4 5 188[19:46 [email protected]~]# echo ${arr[@]:2:3}3 4 5 #跳过两个元素, take back three An element

5. Delete an element in an array: causes sparse formatting

Unset Array[index]



Second, string processing

1. String slicing:

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

${var:offset}: Returns the string variable var starting with the character specifier (excluding the first offset character) from the first, to the last part, the value of offset is between 0 and ${#var}-1

${var:offset:number}: Returns the string variable var starting with the character specifier (excluding the first offset character) from the first, the part of the length number

${var:-lengh}: Takes the rightmost few characters of the string:

Note: You must have a blank character after a colon

[19:50 [email protected]~]# str= "Nihaolinux" [19:51 [email protected]~]# echo $strnihaolinux [19:51] [email protected]~]# echo ${#str}10 #取字符串的长度 [19:51 [email protected]~]# echo ${str:2}haolinux #从第二个字符串 (excluding the second one) start to last [19:51] [email protected]~ ]# Echo ${str:2:3}hao #从第二个字符串 (excluding the second) start fetching after three characters [19:51 [email protected]~]# echo ${str: -5}linux# takes 5 characters from the last side

2, based on the pattern to take the substring:

(1) ${var#*word}: Where word can be any of the specified characters

Features: From left to right, find the var variable stored in the string, the first occurrence of Word, delete all characters from the beginning of the string to the first occurrence of Word characters

(2) ${var##*word}

Ditto, the difference is that all the content between the beginning of the string and the last character specified by word is deleted.

(3) ${var%word*}: Where word can be any of the specified characters;

Function: From right to left, find the var variable stored in the string, the first occurrence of word, delete the last character of the string to the left to the first occurrence of all characters between word characters;

(4) ${var%%word*}

Ditto, except delete all characters from the rightmost character of the string to the left until the last occurrence of the word character;

[19:55 [email protected]~]# str= ' getent passwd root ' [19:56] [email protected]~]# echo $strroot: x:0:0:,,62985600:/root:/ bin/bash[19:56 [email protected]~]# Echo ${str#*root} #删除第一次出现root (including) string and previous characters: x:0:0:,,62985600:/root:/bin/bash[ 19:56 [email protected]~]# Echo ${str##*root} #删除最后出现root (including) string and previous characters:/bin/bash[19:56 [email protected]~]# echo ${str% root*} #删除从右到左第一次出现root (contains) string and previous character root:x:0:0:,,62985600:/[19:56 [email protected]~]# Echo ${str%%root*} # Delete Right-to-left last occurrence of root (containing) string and previous characters

3. Find and replace

(1) ${var/pattern/substi}:

Finds the string in the string represented by Var, the first time it is matched to by pattern, and replaces it with Substi

(2) ${var//pattern/substi}:

Find the string represented by Var, all strings that can be matched to by pattern, and replace it with Substi

(3) ${var/#pattern/substi}:

Finds the string in the string represented by Var, in which the beginning of the line is matched by the pattern, to replace the Substi

(4) ${var/%pattern/substi}:

Finds the string in the string represented by Var, where the end of the line is matched by the pattern, to replace the Substi

[19:57 [Email protected]~]# echo ${str/root/root}root:x:0:0:,,62985600:/root:/bin/bash[20:01 [email protected]~]# echo ${str//root/root}root:x:0:0:,,62985600:/root:/bin/bash[20:02 [email protected]~]# echo ${str/#root/root}root:x : 0:0:,,62985600:/root:/bin/bash[20:02 [email protected]~]# echo ${str/%bash/bash}root:x:0:0:,,62985600:/root:/bin/ Bash


4. Find and delete

${var/pattern}: Find the string represented by Var, delete the string that was first matched by pattern

${var//pattern}: All

${var/#pattern}: First line

${var/%pattern}: End of line

[20:02 [Email protected]~]# echo ${str/root}:x:0:0:,,62985600:/root:/bin/bash[20:05 [email protected]~]# echo ${str// root}:x:0:0:,,62985600:/:/bin/bash[20:05 [email protected]~]# echo ${str#root}:x:0:0:,,62985600:/root:/bin/bash[ 20:06 [email protected]~]# echo ${str%bash}root:x:0:0:,,62985600:/root:/bin/


Iii. Creating temporary files

Mktemp command: Create a temporary file to avoid conflicts

Mktemp[option] ... [TEMPLATE]

TEMPLATE:filename.XXX

"x must appear at least three"

OPTION:

-D: Create a temp directory

--tmpdir=/dir: Indicates the location of the directory where the temporary files are stored

Instance:

#mktemp--tmpdir=/testdirtest. Xxxxxx


This article from the "6638225" blog, reproduced please contact the author!

Linux Basic Learning Day 13th (array, string variable handling)

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.