If is judged if
#shell#!/bin/sha=5if [[ $a > 3 ]];then echo "$a>3"fi#写成一行if [[ $a < 6 ]];then echo "$a>3";fi
5>3
5>3
If Else
#shell#!/bin/sha=5if [[ $a > 8 ]];then echo "$a>8"else echo "$a<8"fi
5<8
If elif Else
#shell#!/bin/sha=5if [[ $a > 5 ]];then echo "$a>5"elif [ $a -eq 5 ];then echo "$a=5"else echo "$a<5"fi
5=5
For loop
#shell#!/bin/shfor i in `seq 1 5`;do echo $idone
1
2
3
4
5
While statement
#shella=1while [ $a -lt 5 ];do echo "$a" let "a++" #或者 a=`expr $a + 1`done
1
2
3
4
Infinite Loop while using: substitution condition
#shell#!/bin/shwhile : ;do echo "hello"done
While condition is always true
#shell#!/bin/shwhile true;do echo "hello"done
Using a For loop
#!/bin/shfor ((;;));do echo "hello"done
Until cycle
#shell#!/bin/sha=0until [ $a -gt 10 ]; do echo $a let "a++"done
0
1
2
3
4
5
6
7
8
9
10
Case is a multi-select statement, each case statement matches a value with a pattern
#shell#!/bin/shread -p "请输入的你的名次:" numcase $num in 1) echo "武林盟主" ;; 2) echo "五岳盟主" ;; 3) echo "华山掌门" ;; *) echo "回家玩去"esac
Jump out of the loop break jump out of all loops
#shellwhile :;do read -p "请输入1到5之间的数字:" num case $num in 1|2|3|4|5) echo "你输入的数字为$num" ;; *) echo "你输入的数字不在1和5之间" break ;; esacdone
Stop the loop after entering 6
Please enter a number from 1 to 5:5
The number you entered is 5.
Please enter a number from 1 to 5:4
The number you entered is 4.
Please enter a number from 1 to 5:6
The number you entered is not between 1 and 5.
Continue jump out of this cycle
#shellwhile :;do read -p "请输入1到5之间的数字:" num case $num in 1|2|3|4|5) echo "你输入的数字为$num" ;; *) echo "你输入的数字不在1和5之间" continue echo "游戏结束" ;; esacdone
Continue the next loop after entering 7
Please enter a number from 1 to 5:7
The number you entered is not between 1 and 5.
Please enter a number from 1 to 5:3
The number you entered is 3.
Please enter a number from 1 to 5:6
The number you entered is not between 1 and 5.
Esac case ends with EASC, each case branched with;; To break
Shell 10 Process Control