Implementation of linux using shell script to process string

Time:2021-9-21

1. Intercept the first 8 bits of the string


expr substr "$string" 1 8
echo $string | awk '{print substr(,1,8)}'
echo $string | cut -c1-8
echo $string | dd bs=1 count=8 2>/dev/null

2. Split and replace string

2.1 command description

Symbol interpretation
* Wildcard, used to match the substring of the string to be deleted
. Separator, which can be any one or more characters
% Match right to left
# Match left to right
/ Represents substitution
% # / Non greedy matching, that is, matching the shortest result that matches the wildcard
% ## // Greedy matching, that is, matching the longest result that matches the wildcard
Example meaning
${#VALUE} Calculates the number of characters in the value string
${value%. *} or ${value%%. *} Delete the right character in the value string that matches the separator “.” and keep the left character
${value # *.} or ${value ## *.} Delete the left character in the value string that matches the separator “.” and keep the right character
${value / old / new} or ${value / / old / new} Replace the matching old substring in the value string with a new substring

2.2 application examples

Define variable name = odysee_ odysee

Calculates the number of characters in the value string

Echo ${#name} # output result 13

Delete the right character in the value string that matches the separator “.” and keep the left character

#Non greedy matching
Echo ${name% y *} # output result odysee_ od
#Greedy matching
Echo ${name%% y *} # output result OD

Delete the left character in the value string that matches the separator “.” and keep the right character

#Non greedy matching
Echo ${name # * y} # output result see_ odysee
#Greedy matching
Echo ${name ## * y} # output result see

Replace the matching old substring in the value string with a new substring

#Non greedy matching
Echo ${name / O / O} # output odysee_ odysee

#Greedy matching
Echo ${name // O / O} # output result odysee_ Odysee

3. String interception

3.1 command description

Example meaning
${VALUE:POSITION} In the string value, the substring is extracted from the position (matching from left to right)
${VALUE:POSITION:LENGTH} In the string value, the substring with length of length is extracted from position (matching from left to right)
${VALUE:0-OFFSET} In the string value, intercept offset characters from right to left
${VALUE:0-OFFSET:LENGTH} In the string value, intercept the first length of offset characters from right to left

3.2 application examples

Define variable name = mynameisodysee

In the string name, extract the substring from position 2 (matching from left to right)

Echo ${Name: 2} # output nameisodysee

In the string name, extract the substring of length 4 from position 2 (matching from left to right)

Echo ${Name: 2:4} # output result name

In the string name, 8 characters are truncated from right to left

Echo ${Name: 0-8} # output result isodysee

In the string name, the first two of the eight characters are truncated from right to left

Echo ${Name: 0-8:2} # output result is

Define variable string = abc12342341. The following is an example of interception

Echo ${string: 4} # intercepts all the following strings from bit 4, and the output result is 2341
Echo ${string: 3:3} # starts from the third bit and intercepts the next three bits. Output result: 123
Echo ${string: 3:6} # starts from the third bit and intercepts the next 6 bits. The output result is 123423  
Echo ${string: - 4} # intercepts the last 4 bits, output result: 2341
Echo ${string: (- 4)} # ditto  
Expr substr $string 3 3 # starts from the third bit, intercepts the next three bits, and outputs the result 123

Define the variable STR = “ABCDEF”. The following is an example of interception

Expr substr "$STR" 1 3 # take 3 characters from the first position and output the result: ABC 
Expr substr "$STR" 2 5 # take 5 characters from the second position and output the result: bcdef  
Expr substr "$STR" 4 5 # take 5 characters from the fourth position and output the result: def 
Echo ${STR: 2} # extracts the string from the second position and outputs the result: bcdef 
Echo ${STR: 2:3} # extracts 3 characters from the second position and outputs the result: BCD 
Echo ${STR: (- 6): 5} # extract the string from the penultimate position to the left, and output the result: ABCDE 
Echo ${STR: (- 4): 3} # extract 6 characters to the left from the penultimate position, and output the result: CDE

4. Judge the value of the variable

4.1 command description

Example meaning
${string-DEFAULT} If the string variable is undefined, the return value is the value of default; otherwise, the value of the variable is returned
${string:-DEFAULT} If the string variable is undefined or its value is empty, the return value is the value of default; otherwise, the value of the variable is returned
${string=DEFAULT} If the string variable is undefined, the return value is the value of default, and default is assigned to string; otherwise, the value of the variable is returned
${string:=DEFAULT} If the string variable is undefined or its value is empty, the return value is default,
And assign default to string, otherwise return the value of the variable
${string+DEFAULT} If string has been assigned, its value is replaced with default, otherwise no replacement will be made
${string:+DEFAULT} If string has been assigned, its value is replaced with default, otherwise no replacement will be made
${string?ERR_MSG} When the variable is undefined, err is set_ MSG message sent to standard error output
${string:?ERR_MSG} When the variable is not assigned, err is set_ MSG message sent to standard error output

4.2 application examples

${string default}: if the string variable is undefined, the return value is the value of default; otherwise, the value of the variable is returned

#String variable not defined
Echo ${string string variable undefined} # output result: string variable undefined

#String variable is defined, but the value is null (null value will be returned)
string=
Echo ${string string variable is undefined} # output result is null

#Define string variable and assign value
string=test
Echo ${string string variable undefined} # output result: Test

${string: – default}: if the string variable is undefined or its value is empty, the return value is the value of default; otherwise, the value of the variable is returned

#String variable not defined
Echo ${string: - string variable undefined} # output result: string variable undefined

#String variable is defined, but the value is null
string=
Echo ${string: - string variable value is null} # output result: string variable value is null

#Define string variable and assign value
string=test
Echo ${string: - string variable value is null} # output result: Test

${string = Default}: if the string variable is undefined, the return value is the value of default, and the default is assigned to string; otherwise, the value of the variable is returned

#String variable not defined
Echo ${string = 123} # output result: 123
Echo ${string} # output result: 123

#String variable is defined, but the value is null (null value will be returned)
string=
Echo ${string = 123} # output result is null
echo ${string}

#Define string variable and assign value
string=test
Echo ${string = 123} # output result: Test
Echo ${string} # output result: Test

${string: = Default}: if the string variable is undefined or its value is empty, return the value of default and assign default to string, otherwise return the value of the variable

#String variable not defined
Echo ${string: = 123} # output result: 123
Echo ${string} # output result: 123

#String variable is defined, but the value is null
string=
Echo ${string: = 123} # output result: 123
Echo ${string} # output result: 123

#Define string variable and assign value
string=test
Echo ${string: = 123} # output result: Test
Echo ${string} # output result: Test

${string + default}: if string has been assigned, its value will be replaced with default, otherwise no replacement will be made

#String variable not defined
Echo ${string + 123} # output result is null
Echo ${string} # output result is null

#String variable is defined, but the value is null
string=
Echo ${string + 123} # output result: 123
Echo ${string} # output result is null

#Define string variable and assign value
string=test
Echo ${string + 123} # output result: 123
Echo ${string} # output result: Test

${string: + default}: if string has been assigned, its value is replaced with default, otherwise no replacement will be made

#String variable not defined
Echo ${string: + 123} # output result is null
Echo ${string} # output result is null

#String variable is defined, but the value is null
string=
Echo ${string: + 123} # output result is null
Echo ${string} # output result is null

#Define string variable and assign value
string=test
Echo ${string: + 123} # output result: 123
Echo ${string} # output result: Test

${string? Err_msg}: when the variable is undefined, err_ MSG message sent to standard error output

#String variable not defined
Echo ${string? Output error message} # output result: - bash: String: output error message
Echo ${string} # output result is null

#String variable is defined, but the value is null
string=
Echo ${string? Output error message} # output result is null
Echo ${string} # output result is null

#Define string variable and assign value
string=test
Echo ${string? Output error message} # output result: Test
Echo ${string} # output result: Test

${string:? Err_msg}: when the variable is not assigned, err_ MSG message sent to standard error output

#String variable not defined
Echo ${string:? Output error message} # output result: - bash: String: output error message
Echo ${string} # output result is null

#String variable is defined, but the value is null
string=
Echo ${string:? Output error message} # output result: - bash: String: output error message
Echo ${string} # output result is null

#Define string variable and assign value
string=test
Echo ${string:? Output error message} # output result: Test
Echo ${string} # output result: Test

5. Get string length

#Define variable string = abc12341
Echo ${#string} # output result 11
Expr length $string # output result 11
Expr "$string": ". *" # semicolon should have a space on both sides. The usage of: root match here is similar

6. Get the location of the string

#Define variable STR = "ABC"
Expr index $STR "a" # output result 1 
Expr index $STR "B" # output result 2 
Expr index $STR "X" # output result 0 
Expr index $STR "" # output result 0

7. Get the maximum length from the beginning of the string to the substring

#Define variable string = abc12341
Expr match $string 'ABC. * 3' # output result 9

8. Display matching content

#Define variable string = abc12341
Expr match $string '\ ([a-c] * [0-9] * \)' # output result abc12341  
Expr $string: '\ ([a-c] * [0-9] \)' # output result ABC1  
Expr $string: '. * \ ([0-9] [0-9] [0-9] \)' # output result 341 shows the matching content in parentheses

9. Display mismatched content

#Define variable string = abc12341
Echo ${string #a * 3} # starts from the left of $string and removes the shortest matching substring. The output result is 42341 
Echo ${string #c * 3} # so nothing matches. The output result is abc12341
Echo ${string #* C1 * 3} # starts from the left of $string, removes the shortest matching substring, and outputs the result: 42341 
Echo ${string ##a * 3} # starts from the left of $string and removes the longest matching substring. Output result: 41
Echo ${string% 3 * 1} # starts from the right of $string, removes the shortest matching substring, and outputs the result: abc12342
Echo ${string%% 3 * 1} # starts from the right of $string, removes the longest matching substring, and outputs the result: abc12

#Define variables STR = "abbc, def, GHI, abcjkl" 
Echo ${str #a * C} # output result: def, GHI, abcjkl a pound sign (#) indicates that the shortest match is intercepted from the left (the abbc string is removed here) 
Echo ${str ##a * C} # output result: JKL two pound signs (##) indicate that the longest match is truncated from the left (abbc, def, GHI, ABC strings are removed here) 
Echo ${str # "a * C"} # output result: abbc, def, GHI, abcjkl because there is no "a * C" substring in str 
Echo ${str## "a * C"} # output result: abbc, def, GHI, abcjkl the same 
Echo ${str # * a * c *} # output result: null 
Echo ${str## * a * c *} # output result: null 
Echo ${str #d * f} # output results: abbc, def, GHI, abcjkl,  
Echo ${str #* d * f} # output result: GHI, abcjkl   
Echo ${STR% a * l} # output result: abbc, def, GHI a percentage sign (%) indicates the shortest match is intercepted from the right  
Echo ${STR%% B * l} # output result: a two percent signs (%%) indicate that the longest match is intercepted from the right 
Echo ${STR% a * C} # output results: abbc, def, GHI, abcjkl

#It should be noted here that it must start from the first character or the last character of the string. It can be remembered in this way. The pound sign (#) is usually used to represent a number, which is placed in front of it; Percent sign (%) after unloading number; Or remember that in the keyboard layout, the pound sign (#) is always on the left (i.e. in front) of the percent sign (%)

10. Remove the characters at the end of the string

Use DF – th to obtain disk information and assign the utilization rate to the variable to compare with the threshold value. If there is a% number that cannot be compared, you need to remove the% number in the variable. You can use the format of ${var%?} to remove the last character


#!/bin/bash

root_usage=$(df -TPh | grep -w "/" | awk '{print $6}')
echo ${root_usage%?}

This is the end of this article on the implementation of linux using shell script to process strings. For more information about shell processing strings, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!

Recommended Today

Supervisor

Supervisor [note] Supervisor – H view supervisor command help Supervisorctl – H view supervisorctl command help Supervisorctl help view the action command of supervisorctl Supervisorctl help any action to view the use of this action 1. Introduction Supervisor is a process control system. Generally speaking, it can monitor your process. If the process exits abnormally, […]