Jugement de l'état de la coque
Format de grammaire du jugement conditionnel
- Format 1: tester l'expression conditionnelle
- Format 2: [Expression conditionnelle]
- Format 3: [[Expression conditionnelle]] prend en charge regular = ~
Remarque
[] Et [[]] doit avoir des espaces des deux côtés
Paramètres liés au jugement conditionnel
Déterminer le type de fichier
Paramètre de jugement | signification |
---|---|
-e |
Déterminer si le fichier existe (tout type de fichier) |
-F | Déterminez si le fichier existeetEst un fichier normal |
-ré | Déterminez si le fichier existe et est un répertoire |
-L | Déterminez si le fichier existe et est un fichier de lien logiciel |
-b | Déterminez si le fichier existe et est un fichier de périphérique bloc |
-S | Déterminez si le fichier existe et est un fichier socket |
-c | Déterminez si le fichier existe et est un fichier de périphérique de caractères |
-p | Déterminez si le fichier existe et est un fichier de canal nommé |
-s |
Déterminez si le fichier existe et est un fichier non vide (avec contenu) |
Exemple
# 先判断zhu目录是否存在 存在则echo 不存在则创建
[root@maomao shell]# [ -d zhu ] && echo '存在' || mkdir zhu
# 前面加! 是非的意思
! -d 如果不存在 则创建
#!/bin/bash
back_dir=/usr/local/mysql_bcak
if [ ! -d $back_dir ];then
mkdir -p $back_dir
fi
echo "开始备份..."
# -s 可以判断文件是否非空
[root@maomao shell]# [ -s free.sh ] && echo '非空' || echo "空"
非空
[root@maomao shell]# touch 123.txt
[root@maomao shell]# [ -s 123.txt ] && echo '非空' || echo "空"
空
test -e file 只要文件存在条件为真
[ -d /shell/dir1 ] 判断目录是否存在,存在条件为真
[ ! -d /shell/dir1 ] 判断目录是否存在,不存在条件为真
[[ -f /shell/1.sh ]] 判断文件是否存在,并且是一个普通的文件
Déterminer les autorisations de fichier
Paramètre de jugement | signification |
---|---|
-r | S'il est lisible par l'utilisateur actuel |
-w | Si l'utilisateur actuel peut y écrire |
-X | Est-il exécutable par l'utilisateur actuel |
-u | Y a-t-il un risque suid de haut niveau |
-g | Si sgid, bit obligatoire d'autorité avancée |
-k | Y a-t-il un bit, un bit de permission de haut niveau |
Déterminez les nouveaux et les anciens fichiers
L'ancien et le nouveau ici se réfèrent àHeure de modification du fichier
Paramètre de jugement | signification |
---|---|
fichier1 -nt fichier2 | Comparez si fichier1 est plus récent que fichier2 |
file1 - à partir de fichier2 | Comparer si fichier1 est plus ancien que fichier2 |
fichier1 -ef fichier2 | Comparer s'il s'agit du même fichier ou utilisé pour déterminer si le lien physique pointe vers le même inode |
# 创建一个硬链接
[root@maomao shell]# ln free.sh free2.sh
[root@maomao shell]# [ free.sh -ef free2.sh ]
[root@maomao shell]# echo $?
0
Juge entier
Paramètre de jugement | signification |
---|---|
-eq | égal |
-ne | Inégal |
-gt | plus que le |
-lt | Moins que |
-donner | supérieur ou égal à |
-le | Inférieur ou égal à |
Exemple
#!/bin/bash
# 磁盘空间警报脚本
disk_use=`df -Th |grep '/$' |awk '{print $(NF-1)}' |awk -F"%" '{print $1}'`
mail_user=root
if [ $disk_use -gt 90 ];then
echo "`date +%F-%H` disk:${disk_use}%" |mail -s "disk warning..." $mail_user
fi
# 内存警报脚本
#!/bin/bash
mem_used=`free -m |grep Mem |awk '{print $3}'`
mem_total=`free -m |grep Mem |awk '{print $2}'`
mem_percent=$((mem_used*100/$mem_total))
warning_file=/shell/mem_war.txt
rm -rf $warning_file
#if [ $mem_percent -ge 80 ];then
if (($mem_percent>60));then
echo "`date +%F-%H` memory:${mem_percent}%" > $warning_file
fi
if [ -f $warning_file ];then
mail -s "mem warning..." root < $warning_file
rm -rf $warning_file
fi
Chaîne de jugement
Paramètre de jugement | signification |
---|---|
-de | Jugez si c'estairString, il est vrai si la longueur de la chaîne est 0 |
-n | Jugez si c'estnon videChaîne, la longueur de la chaîne n'est pas 0 est vrai |
chaîne1 = chaîne2 | Déterminer si les chaînes sont égales |
chaîne1! = chaîne2 | Déterminez si les chaînes ne sont pas égales |
[root@maomao shell]# [ $USER = 'root' ];echo $?
0
[root@maomao shell]# [ $USER = 'roo' ];echo $?
1
[root@maomao shell]# [ $USER = 'root' ];echo $?
0
[root@maomao shell]# [ $USER == 'root' ];echo $?
0
[root@maomao shell]# [[ $USER == 'root' ]];echo $?
0
[root@maomao shell]# [[ "$USER" == 'root' ]];echo $?
0
Jugement de conditions multiples
Symbole du jugement | signification | Par exemple |
---|---|---|
-a 和 && | Logique et | [1 -eq 1 -a 1 -ne 0] 、 [1 -eq 1] && [1 -ne 0] |
-o somme || | OU logique | [1 -eq 1 -o 1 -ne 1] 、 [1 -eq 1] || [1 -ne 1] |
Note spéciale:
&& avant l'expressionEst vrai, Exécutera le code suivant
|| L'expression précédente为假,才会执行后面的代码
; 只用于分割命令或表达式
实例
- 数值比较
[root@maomao ~]# [ $(id -u) -eq 0 ] && echo "the user is root" || echo "the user is not root"
- 类C风格的数值比较
注意:在(( ))中,=表示赋值;==表示判断
[root@maomao ~]# ((1==2));echo $?
[root@maomao ~]# ((1<2));echo $?
[root@maomao ~]# ((2>=1));echo $?
[root@maomao ~]# ((2!=1));echo $?
[root@maomao ~]# ((`id -u`==0));echo $?
[root@maomao ~]# ((a=123));echo $a
[root@maomao ~]# unset a
[root@maomao ~]# ((a==123));echo $?
- 字符串比较
注意:双引号引起来,看作一个整体;= 和 == 在 [ 字符串 ] 比较中都表示判断
[root@maomao ~]# a='hello world';b=world
[root@maomao ~]# [ $a = $b ];echo $?
[root@maomao ~]# [ "$a" = "$b" ];echo $?
[root@maomao ~]# [ "$a" != "$b" ];echo $?
[root@maomao ~]# [ "$a" !== "$b" ];echo $? 错误
[root@maomao ~]# [ "$a" == "$b" ];echo $?
[root@maomao ~]# test "$a" != "$b";echo $?
test 表达式
[ 表达式 ]
[[ 表达式 ]]
思考:[ ] 和 [[ ]] 有什么区别?
[root@maomao ~]# a=
[root@maomao ~]# test -z $a;echo $?
[root@maomao ~]# a=hello
[root@maomao ~]# test -z $a;echo $?
[root@maomao ~]# test -n $a;echo $?
[root@maomao ~]# test -n "$a";echo $?
# [ '' = $a ];echo $?
-bash: [: : unary operator expected
2
# [[ '' = $a ]];echo $?
0
[root@maomao ~]# [ 1 -eq 0 -a 1 -ne 0 ];echo $?
[root@maomao ~]# [ 1 -eq 0 && 1 -ne 0 ];echo $?
[root@maomao ~]# [[ 1 -eq 0 && 1 -ne 0 ]];echo $?
脚本实例:
批量创建用户的脚本
#!/bin/bash
# 创建用户的脚本
# by stan Z
while true
do
read -p "Please input number:" num
if [[ ! "$num" =~ ^[0-9]+$ || "$num" =~ ^0+$ ]];then
echo "error number!"
else
break
fi
done
while true
do
read -p "Please input prefix:" prefix
if [ -z "$prefix" ];then
echo "error prefix"
else
break
fi
done
for i in `seq $num`
do
user=$prefix$i
useradd $user
echo "123" |passwd --stdin $user &>/dev/null
if [ $? -eq 0 ];then
echo "$user is created."
fi
done
逻辑运算符总结
- 符号;和&&和||都可以用来分割命令或者表达式
- 分号(;)完全不考虑前面的语句是否正确执行,都会执行;号后面的内容
- &&符号,需要考虑&&前面的语句的正确性,前面语句正确执行才会执行&&后的内容;反之亦然
- ||符号,需要考虑||前面的语句的非正确性,前面语句执行错误才会执行||后内容;反之亦然
- 如果&&和||一起出现,从左往右依次看,按照以上原则