Shell script if statement and case statement in detail

The structure of the if statement

Single branch structure

  • A corresponding fi must be inserted at the end of the if statement to represent the end of the statement.
  • The judgment process of if single branch structure

Insert picture description here

if  条件测试操作
        then    命令序列
fi    #结尾语句

Insert picture description here
Insert picture description here

Double branch structure

  • The judgment process of if double branch structure
    Insert picture description here
if       条件测试操作
       then    命令序列1
       else     命令序列2
 fi    #结尾语句

Insert picture description here

Multi-branch structure

  • The judgment process of if multi-branch structure

Insert picture description here

if      条件测试操作1
      then    命令序列1
elif 条件测试操作2
      then    命令序列2
else
      命令序列3
fi

Insert picture description here
Insert picture description here

case statement

case multi-branch structure

  • The end of the case line must be the word "in". Each mode must end with a closing bracket ")".
  • The double semicolon ";;" indicates the end of the command sequence.
  • In the pattern string, square brackets can be used to indicate a continuous range, such as "[0-9]"; it can also be expressed with "|", such as "A|B"
  • The last "*)" represents the default mode, and the asterisk is equivalent to a wildcard.
  • Judgment process of case multi-branch structure

Insert picture description here

case 变量值 in
模式1)
      命令序列1
      ;;
模式2)
      命令序列2
      ;;
      ......
*)
     默认命令序列
esac

Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_51614581/article/details/111294626