Shell Programming learning (seven)

Knowledge and practice if conditional statement

if conditional statement

Syntax if condition statement

Single-branch structure

The first

if  <条件测试表达式>
  then 
      指令
fi

The second

if <条件测试表达式>; then
    指令
fi

[Description]
[ -f "$file1" ] && echo 1is equivalent to the following if condition

if  [ -f "$file1" ]; then
    echo 1
fi
Two-branch structure
if  <条件测试表达式>
  then 
     指令集1
esle
     指令集2
fi
Multi-branch structure
if  <条件测试表达式1>
  then 
     指令集1
elif  <条件测试表达式2>
  then 
     指令集2
esle
     指令集3
fi

[Note] Note multi-branch elifwording, each elifto be with then.

if multiple conditional statement conditional expression syntax

(1) test conditional expressions

if test 表达式;then
        指令
if

(2) [] Conditional Expression

if [ 表达式 ];then
    指令
if

(3) [[]] Conditional Expression

if [[ 表达式 ]];then
    指令
if

(4) (()) conditional expression

if (( 表达式 ));then
    指令
if

(5) command expression

if 命令;then
    指令
if

Guess you like

Origin www.cnblogs.com/vicodona/p/11223668.html