Shell programming conditional statements: if and case statements

test command

  • Test whether the expression is established, if it is established, return 0, otherwise return other values
格式1:test 条件表达式
格式2[ 条件表达式 ]

File test

[ 操作符 文件或目录 ]

Commonly used test operators

  • -d: test whether it is a directory
  • -e: test whether the directory or file exists
  • -f: test whether it is a file
  • -r: Test whether the current user has permission to read
  • -w: Test whether the current user has permission to write
  • -x: Test whether the current user has permission to execute
[root@localhost ~]#[-d lmedialcdrom ]
[root@localhost ~]# echo $? //返回0表示条件成立
0
[root@localhost ~]# [ -d lmedialcdrom/Server ]
[root@localhost ~]# echo $? //返回1表示条件不成立
1
[root@localhost ~]#[-d lmedialcdrpm ] && echo "YES" //逻辑与,“而且”的意思
YES

Integer value comparison

[ 整数 操作符 整数2 ]

Commonly used test operators

  • -eq: equal to
  • -ne: not equal
  • -gt: greater than
  • -lt: less than
  • -le: less than or equal to
  • -ge: greater than or equal to
[root@localhost ~]# who | wc -l
7
[root@localhost ~]# [ $(who | wc -l) -gt 5 | && echo "Too many." //用户数是否>5
Too many.
[root@localhost ~]#[$(who | wc -l) -ge 10 | && echo ">= 10. //用户数是否>=10
[root@localhost ~]# FreecC=$(free -m | grep "Mem: ' | awk "{
    
    print $6y}')
[root@localhost ~]#[$FreecC -lt 1024]&& echo ${
    
    FreecC}MB //空闲内存是否<1024MB
275MB

String comparison

  • Format 1
格式1[ 字符串1 = 字符串2 ]
      [ 字符串1= 字符串2 ]
格式2[ -z 字符串 ]

Commonly used test operators

  • =: The string content is the same
  • ! =: The string content is different,! Sign means the opposite
  • -z: The string content is empty
[root@localhost ~]# echo $LANG
zh_CN.UTF-8
[root@localhost ~]# [ $LANG!="en.US" ] &&echo “Not en.US"
Not en.US

Logic test

格式1[ 表达式1 ] 操作符 [ 表达式2 ]...
格式2:命令1 操作符 命令2 ...

Commonly used test operators

  • -a or &&: logical and, meaning "and"
  • -c or ||: logical or, meaning "or"
  • ! : Logical no
[root@localhost ~]#[-d letc ] &&[-r letc ] &&echo "You can open it"
[root@localhost ~]#【-d letc ] [ -d home ] &&echo "ok"

Single branch structure

Insert picture description here
Insert picture description here

Double branch structure

Insert picture description here
Insert picture description here

Multi-branch structure

Insert picture description here
Insert picture description here

  • Judge the range of scores, and divide into three gears of excellent, qualified and unqualified'
[root@localhost ~]# cat gradedlv.sh
read -p"请输入您的分数(O-100);GRADE
if [ $GRADE-ge 85 ]&& [ $GRADE -le 100 ]
then
   echo "$GRADE分.优秀!"
elif [ $GRADE -ge 70]&&[ $GRADE-le 84 ]
then
   echo "$GRADE分.合格!"
else
   echo "$GRADE分.不合格!"
fi

case multi-branch structure

Insert picture description here
Insert picture description here

[root@localhost ~]# cat hitkey.sh
#!/bin/bash
read -p"请输入一个字符,并按Enter键确认:"KEY
case "$KEY" in
 [a-z][A-Z])
  echo "您输入的是字母."
  ;;
 [0-9])
  echo "您输入的是数字."
  ;;
 *)
  echo"您输入的是空格.功能键或其他控制字符."
esac

Guess you like

Origin blog.csdn.net/weixin_49343462/article/details/109496739