Linux uses shell scripts to process strings

Original link of this article: https://blog.csdn.net/xzk9381/article/details/110876329

1. Intercept the first 8 bits of a 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 strings


2.1 Command description

symbol Paraphrase
* Wildcard, used to match the substring of the string to be deleted
. Separator, can be any one or more characters
% Match from right to left
# Match from left to right
/ Means replacement
% # / Non-greedy matching, that is, the shortest result that matches the wildcard
% ## // Greedy matching, that is, the longest result that matches the wildcard
Example meaning
${#VALUE} Count the number of characters in the VALUE string
${VALUE%.*} 或 ${VALUE%%.*} Delete the right character matched by the separator "." in the VALUE string, and keep the left character
${VALUE#*.} 或 ${VALUE##*.} Delete the left character matched by the separator "." in the VALUE string, and keep the right character
${VALUE/OLD/NEW} 或 ${VALUE//OLD/NEW} Replace the matched OLD substring in the VALUE string with the NEW substring

2.2 Application examples

Define variablesname=odysee_odysee

  1. Count the number of characters in the VALUE string
echo ${#name}			# 输出结果 13
  1. Delete the right character matched by the separator "." in the VALUE string, and keep the left character
# 非贪婪匹配
echo ${name%y*}			# 输出结果 odysee_od
# 贪婪匹配
echo ${name%%y*}		# 输出结果 od
  1. Delete the left character matched by the separator "." in the VALUE string, and keep the right character
# 非贪婪匹配
echo ${
    
    name#*y}			# 输出结果 see_odysee
# 贪婪匹配
echo ${
    
    name##*y}		# 输出结果 see
  1. Replace the matched OLD substring in the VALUE string with the NEW substring
# 非贪婪匹配
echo ${name/o/O}		#输出结果 Odysee_odysee

# 贪婪匹配
echo ${name//o/O}		#输出结果 Odysee_Odysee

3. String interception


3.1 Command description

Example meaning
${VALUE:POSITION} In the string VALUE, extract the substring from the position POSITION (matching from left to right)
${VALUE:POSITION:LENGTH} In the string VALUE, extract the substring of length LENGTH from the position 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 variablesname=mynameisodysee

  1. In the string name, extract the substring from position 2 (matching from left to right)
echo ${name:2}		# 输出结果 nameisodysee
  1. In the string name, extract a substring of length 4 from position 2 (matching from left to right)
echo ${name:2:4}	# 输出结果 name
  1. In the string name, intercept 8 characters from right to left
echo ${name:0-8}	# 输出结果 isodysee
  1. In the string name, intercept the first two of the 8 characters from right to left
echo ${name:0-8:2}	# 输出结果 is
  1. Define variables string=abc12342341, the following is an example of interception
echo ${string:4}      	# 从第4位开始截取后面所有字符串,输出结果:2342341
echo ${string:3:3}    	# 从第3位开始截取后面3位,输出结果:123
echo ${string:3:6}    	#从第3位开始截取后面6位,输出结果:123423    
echo ${string: -4}    	#截取后4位,输出结果:2341
echo ${string:(-4)}     #同上    
expr substr $string 3 3 #从第3位开始截取后面3位,输出结果123 
  1. Define variables str="abcdef", the following is an example of interception
expr substr "$str" 1 3  # 从第一个位置开始取3个字符,输出结果:abc  
expr substr "$str" 2 5  # 从第二个位置开始取5个字符,输出结果:bcdef   
expr substr "$str" 4 5  # 从第四个位置开始取5个字符,输出结果:def  
echo ${str:2}           # 从第二个位置开始提取字符串,输出结果:bcdef  
echo ${str:2:3}         # 从第二个位置开始提取3个字符,输出结果:bcd  
echo ${str:(-6):5}      # 从倒数第二个位置向左提取字符串,输出结果:abcde  
echo ${str:(-4):3}      # 从倒数第二个位置向左提取6个字符,输出结果:cde

4. Determine the value of the variable


4.1 Command description

Example meaning
${string-DEFAULT} If the string variable is not defined, the return value is the value of DEFAULT, otherwise the value of the variable is returned
${string:-DEFAULT} If the string variable is not defined, 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 not defined, 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 not defined, or its value is empty, 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 string has been assigned, its value is replaced with DEFAULT, otherwise no replacement is performed
${string:+DEFAULT} If string has been assigned, its value is replaced with DEFAULT, otherwise no replacement is performed
${string?ERR_MSG} When the variable is not defined, send the ERR_MSG message to the standard error output
${string:?ERR_MSG} When the variable is not assigned, the ERR_MSG message is sent to the standard error output

Original link of this article: https://blog.csdn.net/xzk9381/article/details/110876329

4.2 Application examples

  1. ${string-DEFAULT}: If the string variable is not defined, the return value is the value of DEFAULT, otherwise the value of the variable is returned
# 未定义string变量
echo ${string-string 变量未定义}			# 输出结果:string 变量未定义

# 定义string变量,但值为空(会返回空值)
string=
echo ${string-string 变量未定义}			# 输出结果为空

# 定义string变量并赋值
string=test
echo ${string-string 变量未定义}			# 输出结果:test
  1. ${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变量
echo ${string:-string 变量未定义}		# 输出结果:string 变量未定义

# 定义string变量,但值为空
string=
echo ${string:-string 变量值为空}		# 输出结果:string 变量值为空

# 定义string变量并赋值
string=test
echo ${string:-string 变量值为空}		# 输出结果:test
  1. ${string=DEFAULT}: If the string variable is not defined, the return value is the value of DEFAULT, and DEFAULT is assigned to string, otherwise the value of the variable is returned
# 未定义string变量
echo ${string=123}			# 输出结果:123
echo ${string}				# 输出结果:123

# 定义string变量,但值为空(会返回空值)
string=
echo ${string=123}			# 输出结果为空
echo ${string}

# 定义string变量并赋值
string=test
echo ${string=123}			# 输出结果:test
echo ${string}				# 输出结果:test
  1. ${string:=DEFAULT}: If the string variable is not defined, or its value is empty, the return value is the value of DEFAULT, and DEFAULT is assigned to string, otherwise the value of the variable is returned
# 未定义string变量
echo ${string:=123}			# 输出结果:123
echo ${string}				# 输出结果:123

# 定义string变量,但值为空
string=
echo ${string:=123}			# 输出结果:123
echo ${string}				# 输出结果:123

#定义string变量并赋值
string=test
echo ${string:=123}			# 输出结果:test
echo ${string}				# 输出结果:test
  1. ${string+DEFAULT}: If string has been assigned, its value is replaced with DEFAULT, otherwise no replacement is performed
# 未定义string变量
echo ${string+123}			# 输出结果为空
echo ${string}				# 输出结果为空

# 定义string变量,但值为空
string=
echo ${string+123}			# 输出结果:123
echo ${string}				# 输出结果为空

# 定义string变量并赋值
string=test
echo ${string+123}			# 输出结果:123
echo ${string}				# 输出结果:test
  1. ${string:+DEFAULT}: If string has been assigned, its value is replaced with DEFAULT, otherwise no replacement is performed
# 未定义string变量
echo ${string:+123}			# 输出结果为空
echo ${string}				# 输出结果为空

# 定义string变量,但值为空
string=
echo ${string:+123}			# 输出结果为空
echo ${string}				# 输出结果为空

# 定义string变量并赋值
string=test
echo ${string:+123}			# 输出结果:123
echo ${string}				# 输出结果:test
  1. ${string?ERR_MSG}: When the variable is not defined, send the ERR_MSG message to the standard error output
# 未定义string变量
echo ${string?输出错误信息}	# 输出结果:-bash: string: 输出错误信息
echo ${string}				# 输出结果为空

# 定义string变量,但值为空
string=
echo ${string?输出错误信息}	# 输出结果为空
echo ${string}				# 输出结果为空

# 定义string变量并赋值
string=test
echo ${string?输出错误信息}	# 输出结果:test
echo ${string}				# 输出结果:test
  1. ${string:?ERR_MSG}: When the variable is not assigned, the ERR_MSG message is sent to the standard error output
# 未定义string变量
echo ${string:?输出错误信息}	# 输出结果:-bash: string: 输出错误信息
echo ${string}				# 输出结果为空

# 定义string变量,但值为空
string=
echo ${string:?输出错误信息}	# 输出结果:-bash: string: 输出错误信息
echo ${string}				# 输出结果为空

# 定义string变量并赋值
string=test
echo ${string:?输出错误信息}	# 输出结果:test
echo ${string}				# 输出结果:test

5. Get the string length


# 定义变量string=abc12342341
echo ${#string}			# 输出结果 11
expr length $string		# 输出结果 11
expr "$string" : ".*"	# 分号二边要有空格,这里的:根match的用法差不多

6. Get the position of the string


# 定义变量str="abc"
expr index $str "a"  # 输出结果 1  
expr index $str "b"  # 输出结果 2  
expr index $str "x"  # 输出结果 0  
expr index $str ""   # 输出结果 0   

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


# 定义变量string=abc12342341
expr match $string 'abc.*3'		# 输出结果 9

8. Show matching content


# 定义变量string=abc12342341
expr match $string '\([a-c]*[0-9]*\)'  # 输出结果 abc12342341    
expr $string : '\([a-c]*[0-9]\)'       # 输出结果 abc1    
expr $string : '.*\([0-9][0-9][0-9]\)' # 输出结果 341 显示括号中匹配的内容  

9. Show unmatched content


# 定义变量string=abc12342341
echo ${
    
    string#a*3}     # 从$string左边开始,去掉最短匹配子串,输出结果:42341 
echo ${
    
    string#c*3}     # 这样什么也没有匹配到,输出结果:abc12342341
echo ${
    
    string#*c1*3}   # 从$string左边开始,去掉最短匹配子串,输出结果:42341  
echo ${
    
    string##a*3}    # 从$string左边开始,去掉最长匹配子串,输出结果:41
echo ${string%3*1}     # 从$string右边开始,去掉最短匹配子串,输出结果:abc12342
echo ${string%%3*1}    # 从$string右边开始,去掉最长匹配子串,输出结果:abc12

# 定义变量str="abbc,def,ghi,abcjkl"  
echo ${
    
    str#a*c}         # 输出结果:def,ghi,abcjkl  一个井号(#) 表示从左边截取掉最短的匹配 (这里把abbc字串去掉) 
echo ${
    
    str##a*c}        # 输出结果:jkl             两个井号(##) 表示从左边截取掉最长的匹配 (这里把abbc,def,ghi,abc字串去掉)  
echo ${
    
    str#"a*c"}       # 输出结果:abbc,def,ghi,abcjkl   因为str中没有"a*c"子串  
echo ${
    
    str##"a*c"}      # 输出结果:abbc,def,ghi,abcjkl   同理  
echo ${
    
    str#*a*c*}       # 输出结果:空  
echo ${
    
    str##*a*c*}      # 输出结果:空  
echo ${
    
    str#d*f}         # 输出结果:abbc,def,ghi,abcjkl,   
echo ${
    
    str#*d*f}        # 输出结果:ghi,abcjkl     
echo ${str%a*l}         # 输出结果:abbc,def,ghi          一个百分号(%)表示从右边截取最短的匹配   
echo ${str%%b*l}        # 输出结果:a                     两个百分号表示(%%)表示从右边截取最长的匹配  
echo ${str%a*c}         # 输出结果:abbc,def,ghi,abcjkl

# 这里要注意,必须从字符串的第一个字符开始,或者从最后一个开始,可以这样记忆, 井号(#)通常用于表示一个数字,它是放在前面的;百分号(%)卸载数字的后面; 或者这样记忆,在键盘布局中,井号(#)总是位于百分号(%)的左边(即前面)

10. Remove the characters at the end of the string


Use df -Th to get the disk information, and assign the usage rate to the variable to compare with the threshold. If there is a% sign that cannot be compared, so you need to remove the% sign in the variable, you can use the format of ${var%?} to remove Last character

#!/bin/bash

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

Original link of this article: https://blog.csdn.net/xzk9381/article/details/110876329

Guess you like

Origin blog.csdn.net/xzk9381/article/details/110876329