Shell-if and case conditional statements

1. Condition test

1. File test and integer test

1. The test command

Test whether the expression is established, if it is established, return 0, otherwise return other values
Format 1: test conditional expression
Format 2: [conditional expression]

2. File testing

File test
[Operator file or directory]
Common test operators
-d: test whether it is a directory (Directory)
-e: test whether a directory or file exists (Exist)
-f: test whether it is a file (File)
-r: test Does the current user have permission to read (Read)
-w: Test whether the current user has permission to write (Write)
-x: Test whether the current user has permission to execute (eXcute)

Commonly used test operators

[ -d /medialcdrom] 
echo $?
0                                    #0表示条件成立
[ -d lmedialcdromlServer]
echo $?
0                                    #1表示条件不成立
[ -d /medialcdrom ] && echo "YES"    #&&逻辑与,而且的意思  
YES                             

3. Integer value comparison

[Integer 1 Operator Integer 2]
Commonly used test operators
-eq: equal to (Equal)
-ne: not equal to (Not Equal)
-gt: greater than (Greater Than)
-lt: less than (Lesser Than)
-le: less than or Equal to (Lesser or Equal)
-ge: Greater or Equal
commonly used test operator

who | wc -l
1
[ $(who ] wc -l) -gt 5 ] && echo "Too many."                          #用户数是否>5
Too many.   
[ $(who | wc -l) -ge 10 ] && echo "> = 10."                           #用户数是否>=10
[root@localhost ~]# FreeCC=$(free -rli | grep "Mem: " | awk "{print $4}')
[root@localhost ~]#[ $FreeCC -lt 1024] && echo ${FreeCCMB             #空闲内存是否<1024MB
275MB

2. String test and logic test

1. String comparison

Format 1
[String 1 = String 2]
[String 1 != String 2]
Format 2
[-z String]
Commonly used test operators
=: string content is the same
! =: string content is different,! Sign Means the opposite meaning-
z: string content is empty

2. Logic test

Format 1
[Expression 1] Operator [Expression 2]...
Format 2
[Command 1 Operator command 2]...
Commonly used test operators
-a or &&: logical AND, "and" means
-o or |: logical Or, the meaning of "or" T: logical no
[-d /etc] &&[ -r /etc] && echo "You can open it"
[-d /etc] ll [-d /home] && echo "ok"

Two, if statement

1. If single branch statement

Single branch statement format
if conditional test operation
then
command sequence
fi

3种命令序列
①
if [ 3 -gt 2 ]
> then
> echo "yes"
> fi
yes
②
if [ 3 -gt 2 ]; then echo "yes"; fi
yes
③
[ 3 -gt 2 ] && echo "yes"
yes

Insert picture description here
Insert picture description here

2. If double branch statement

Double-branch statement format
if conditional test operation
then
command sequence 1
else
command sequence 2
fi

#!/bin/bash
netstat -natp | grep ":80" &> /dev/null
if [ $? -eq 0 ]
then
 echo "httpd服务已在运行"
else
 echo "httpd未运行,请启动服务"
 echo "systemctl restart httpd"
fi

Insert picture description here
Insert picture description here

3. If multi-branch statement

Multi-branch statement format
if conditional test operation 1
then command sequence 1
elif conditional test operation 2
then command sequence 2
else
command sequence 3
fi

#!/bin/bash
read -p "请输入你的分数:" score

if [ $score -ge 85 -a $score -le 100 ]then
  echo "你的分数为: ${score} ,优秀!"
  
elif [ $score -ge 70 ] &&[ $score -lt 85 ];then
  echo "你的分数为: ${score} ,一般般"
  
elif [[ score -lt 70 && $score -ge 0 ]];then
  echo "你的分数为: ${score} ,30遍!"
else
  echo "输入有误,请重新输入!"
 fi

Insert picture description here
Insert picture description here

Three, case branch statement

1. Case multi-branch structure

case 变量值 in 
模式一)
   命令序列
;;
模式二)
   命令序列 
;;
*)
   默认命令序列
esac

case语句注意事项
①case行首必须以单词“in”,每一模式必须以单个右括号")"结束
②双分号";;"表示命令序列的结束
③模式字符串中,可以用方括号表示一个连续的范围,如"[0-9]";还可以使用竖杠|表示或,比如a|b
最后的*)表示默认模式,其中的*相当于通配符
Insert picture description here

2. Examples

Prompt the user to enter the test scores, requiring scores of 0-75 to be poor; 70-85 to be average; 85 to 100 to be excellent. If you enter other characters, you will be prompted. If there is a mistake, enter it again.
method one

#!/bin/bash
read -p "请输入你的分数(0-100):" x

[ $x -ge 85 -a $x -le 100 ] && a="1" &> /dev/null
[ $x -ge 70 ] && [ $x -lt 85 ] && a="2" &> /dev/null
[[ $x -lt 70 && $x -ge 0 ]] && a="3" &> /dev/null

case $a in
1)
echo "你的分数为$x ,优秀"
;;

2)
echo "你的分数为$x,一般般"
;;

3)
echo "你的分数为$x,30遍!"
;;

*)
  echo "输入有误,请重新输入!"

esac

Insert picture description here
Insert picture description here
Method Two

#!/bin/bash
read -p "请输入你的分数(0-100):" x

case $x in
8[5-9]|9[0-9]|100)
  echo "你的分数为${x} ,优秀"
;;

8[0-4]|7[0-9])
  echo "你的分数为${x},一般般"
;;

[0-9]|0[1-6][0-9])
  echo "你的分数为${x},30遍!"
;;

*)
  echo "输入有误,请重新输入!"

esac

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/s15212790607/article/details/114329729