Lien vers le texte original de cet article: https://blog.csdn.net/xzk9381/article/details/110876329
1. Intercepter les 8 premiers bits d'une chaîne
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. Fractionner et remplacer des chaînes
2.1 Description de la commande
symbole | Paraphrase |
---|---|
* | Caractère générique, utilisé pour correspondre à la sous-chaîne de la chaîne à supprimer |
. | Séparateur, peut être un ou plusieurs caractères |
% | Match de droite à gauche |
# | Match de gauche à droite |
/ | Signifie remplacement |
% # / | Correspondance non gourmande, c'est-à-dire le résultat le plus court qui correspond au caractère générique |
% ## // | Correspondance gourmande, c'est-à-dire le résultat le plus long qui correspond au caractère générique |
Exemple | sens |
---|---|
$ {# VALUE} | Comptez le nombre de caractères dans la chaîne VALUE |
$ {VALUE%. *} 或 $ {VALUE %%. *} | Supprimez le caractère de droite correspondant au séparateur "." Dans la chaîne VALUE et conservez le caractère de gauche |
$ {VALUE # *.} 或 $ {VALUE ## *.} | Supprimez le caractère de gauche correspondant au séparateur "." Dans la chaîne VALUE et conservez le caractère de droite |
$ {VALUE / OLD / NEW} 或 $ {VALUE // OLD / NEW} | Remplacez la sous-chaîne OLD correspondante dans la chaîne VALUE par la nouvelle sous-chaîne |
2.2 Exemples d'application
Définir des variablesname=odysee_odysee
- Comptez le nombre de caractères dans la chaîne VALUE
echo ${#name} # 输出结果 13
- Supprimez le caractère de droite correspondant au séparateur "." Dans la chaîne VALUE et conservez le caractère de gauche
# 非贪婪匹配
echo ${name%y*} # 输出结果 odysee_od
# 贪婪匹配
echo ${name%%y*} # 输出结果 od
- Supprimez le caractère de gauche correspondant au séparateur "." Dans la chaîne VALUE et conservez le caractère de droite
# 非贪婪匹配
echo ${
name#*y} # 输出结果 see_odysee
# 贪婪匹配
echo ${
name##*y} # 输出结果 see
- Remplacez la sous-chaîne OLD correspondante dans la chaîne VALUE par la nouvelle sous-chaîne
# 非贪婪匹配
echo ${name/o/O} #输出结果 Odysee_odysee
# 贪婪匹配
echo ${name//o/O} #输出结果 Odysee_Odysee
3. Interception de chaînes
3.1 Description de la commande
Exemple | sens |
---|---|
$ {VALUE: POSITION} | Dans la chaîne VALUE, extrayez la sous-chaîne de la position POSITION (correspondant de gauche à droite) |
$ {VALUE: POSITION: LENGTH} | Dans la chaîne VALUE, extrayez la sous-chaîne de longueur LENGTH de la position POSITION (correspondant de gauche à droite) |
$ {VALUE: 0-OFFSET} | Dans la chaîne VALUE, intercepter les caractères OFFSET de droite à gauche |
$ {VALUE: 0-OFFSET: LENGTH} | Dans la chaîne VALUE, intercepter la première LENGTH des caractères OFFSET de droite à gauche |
3.2 Exemples d'application
Définir des variablesname=mynameisodysee
- Dans le nom de la chaîne, extrayez la sous-chaîne de la position 2 (correspondant de gauche à droite)
echo ${name:2} # 输出结果 nameisodysee
- Dans le nom de la chaîne, extrayez une sous-chaîne de longueur 4 à partir de la position 2 (correspondant de gauche à droite)
echo ${name:2:4} # 输出结果 name
- Dans le nom de la chaîne, intercepter 8 caractères de droite à gauche
echo ${name:0-8} # 输出结果 isodysee
- Dans le nom de la chaîne, interceptez les deux premiers des 8 caractères de droite à gauche
echo ${name:0-8:2} # 输出结果 is
- Définissez des variables
string=abc12342341
, ce qui suit est un exemple d'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
- Définissez des variables
str="abcdef"
, ce qui suit est un exemple d'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. Déterminez la valeur de la variable
4.1 Description de la commande
Exemple | sens |
---|---|
$ {string-DEFAULT} | Si la variable chaîne n'est pas définie, la valeur de retour est la valeur de DEFAULT, sinon la valeur de la variable est retournée |
$ {chaîne: -DEFAULT} | Si la variable chaîne n'est pas définie ou si sa valeur est vide, la valeur de retour est la valeur de DEFAULT, sinon la valeur de la variable est renvoyée |
$ {string = DEFAULT} | Si la variable chaîne n'est pas définie, la valeur de retour est la valeur de DEFAULT et DEFAULT est affectée à string, sinon la valeur de la variable est renvoyée |
$ {chaîne: = DEFAULT} | Si la variable chaîne n'est pas définie ou si sa valeur est vide, la valeur de retour est la valeur de DEFAULT et DEFAULT est affectée à string, sinon la valeur de la variable est renvoyée |
$ {chaîne + DEFAULT} | Si une chaîne a été affectée, sa valeur est remplacée par DEFAULT, sinon aucun remplacement n'est effectué |
$ {chaîne: + DEFAULT} | Si une chaîne a été affectée, sa valeur est remplacée par DEFAULT, sinon aucun remplacement n'est effectué |
$ {chaîne? ERR_MSG} | Lorsque la variable n'est pas définie, envoyer le message ERR_MSG à la sortie d'erreur standard |
$ {chaîne:? ERR_MSG} | Lorsque la variable n'est pas affectée, le message ERR_MSG est envoyé à la sortie d'erreur standard |
Lien vers le texte original de cet article: https://blog.csdn.net/xzk9381/article/details/110876329
4.2 Exemples d'application
- $ {string-DEFAULT}: Si la variable chaîne n'est pas définie, la valeur de retour est la valeur de DEFAULT, sinon la valeur de la variable est retournée
# 未定义string变量
echo ${string-string 变量未定义} # 输出结果:string 变量未定义
# 定义string变量,但值为空(会返回空值)
string=
echo ${string-string 变量未定义} # 输出结果为空
# 定义string变量并赋值
string=test
echo ${string-string 变量未定义} # 输出结果:test
- $ {string: -DEFAULT}: Si la variable chaîne n'est pas définie, ou si sa valeur est vide, la valeur de retour est la valeur de DEFAULT, sinon la valeur de la variable est retournée
# 未定义string变量
echo ${string:-string 变量未定义} # 输出结果:string 变量未定义
# 定义string变量,但值为空
string=
echo ${string:-string 变量值为空} # 输出结果:string 变量值为空
# 定义string变量并赋值
string=test
echo ${string:-string 变量值为空} # 输出结果:test
- $ {string = DEFAULT}: Si la variable chaîne n'est pas définie, la valeur de retour est la valeur de DEFAULT et DEFAULT est affectée à string, sinon la valeur de la variable est retournée
# 未定义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
- $ {string: = DEFAULT}: Si la variable chaîne n'est pas définie ou si sa valeur est vide, la valeur de retour est la valeur de DEFAULT et DEFAULT est affectée à string, sinon la valeur de la variable est renvoyée
# 未定义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
- $ {string + DEFAULT}: Si une chaîne a été affectée, sa valeur est remplacée par DEFAULT, sinon aucun remplacement n'est effectué
# 未定义string变量
echo ${string+123} # 输出结果为空
echo ${string} # 输出结果为空
# 定义string变量,但值为空
string=
echo ${string+123} # 输出结果:123
echo ${string} # 输出结果为空
# 定义string变量并赋值
string=test
echo ${string+123} # 输出结果:123
echo ${string} # 输出结果:test
- $ {string: + DEFAULT}: Si une chaîne a été affectée, sa valeur est remplacée par DEFAULT, sinon aucun remplacement n'est effectué
# 未定义string变量
echo ${string:+123} # 输出结果为空
echo ${string} # 输出结果为空
# 定义string变量,但值为空
string=
echo ${string:+123} # 输出结果为空
echo ${string} # 输出结果为空
# 定义string变量并赋值
string=test
echo ${string:+123} # 输出结果:123
echo ${string} # 输出结果:test
- $ {string? ERR_MSG}: Lorsque la variable n'est pas définie, envoyer le message ERR_MSG à la sortie d'erreur standard
# 未定义string变量
echo ${string?输出错误信息} # 输出结果:-bash: string: 输出错误信息
echo ${string} # 输出结果为空
# 定义string变量,但值为空
string=
echo ${string?输出错误信息} # 输出结果为空
echo ${string} # 输出结果为空
# 定义string变量并赋值
string=test
echo ${string?输出错误信息} # 输出结果:test
echo ${string} # 输出结果:test
- $ {string:? ERR_MSG}: Lorsque la variable n'est pas affectée, le message ERR_MSG est envoyé à la sortie d'erreur standard
# 未定义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. Obtenez la longueur de la chaîne
# 定义变量string=abc12342341
echo ${#string} # 输出结果 11
expr length $string # 输出结果 11
expr "$string" : ".*" # 分号二边要有空格,这里的:根match的用法差不多
6. Obtenez la position de la chaîne
# 定义变量str="abc"
expr index $str "a" # 输出结果 1
expr index $str "b" # 输出结果 2
expr index $str "x" # 输出结果 0
expr index $str "" # 输出结果 0
7. Obtenez la longueur maximale du début de la chaîne à la sous-chaîne
# 定义变量string=abc12342341
expr match $string 'abc.*3' # 输出结果 9
8. Afficher le contenu correspondant
# 定义变量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. Afficher un contenu inégalé
# 定义变量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. Supprimez les caractères à la fin de la chaîne
Utilisez df -Th pour obtenir des informations sur le disque et affectez le taux d'utilisation à une variable à comparer avec le seuil. S'il existe un signe% qui ne peut pas être comparé, vous devez donc supprimer le signe% dans la variable, vous pouvez utiliser le format de $ {var%?} pour supprimer le dernier caractère
#!/bin/bash
root_usage=$(df -TPh | grep -w "/" | awk '{print $6}')
echo ${root_usage%?}
Lien vers le texte original de cet article: https://blog.csdn.net/xzk9381/article/details/110876329