条件控制语句
a=20b=20if [ $a -lt $b ]then echo "a < b"elif [ $a -gt $b ] then echo "a > b"else echo "a = b"fi
for 循环
# for 循环for loop in 1 3 44 55 66do echo "the value is $loop"done# for 循环数组array=(A B C D E F 33)for i in ${array[@]}do echo $idonefor j in {1..5}do echo $jdone# for 循环数组带下标# ${!var[@] 其中 ${!}表示间接引入变量并对变量进行展开array=(1 2 3 4 5)for i in ${!array[@]}do echo "下标是$i, 值是${array[$i]}"done
while 循环
# while 循环# let 是用于计算的工具, let no++ 是自增; let no-- 是自减; let no+=20 相等于 let no=no+20int=1while(( $int<=5 ))do echo $int let "int++"done# 无限循环while truedo echo "无限循环"doneset i=0set j=0for (( i=0; i<10; ))do let "j+=1" echo "----- j is $j---"done
until 循环
a=0until [ ! $a -lt 10 ]do echo $a let "a+=1"done
case多选择语句
# case ... esac 多选择语句echo "请输入你要的数字"echo "你输入的数字为:"read numcase $num in 1) echo "你选择了1" ;; 2) echo "你选择了2" ;; *) echo "你没有选择1或者2" ;;esac
break
# break while truedo echo "请输入你要的数字" echo "你输入的数字为:" read num case $num in 1|2|3|4|5|6) echo "你选择了$num" ;; *) echo "你没有选择1~6" break ;; esacdone
continue
# continuewhile :do echo -n "输入 1 到 5 之间的数字: " read aNum case $aNum in 1|2|3|4|5) echo "你输入的数字为 $aNum!" ;; *) echo "你输入的数字不是 1 到 5 之间的!" continue echo "游戏结束" ;; esacdone