if [ $a -gt $b ]; then echo "a>b" fi if [ $a -gt $b ]; then echo "a>b" echo "a!=b" echo "true" fi
if-else语句
if-else 语句语法格式
if [ $a -gt $b ]; then echo "a>b" else echo "a<=b" fi if [ $a -gt $b ]; then echo "a>b" echo "a!=b" echo "true" else echo "a<=b" fi if ((a>b)); then echo "true" echo "a>b" else echo "a<=b" fi
if else语法格式
if [ $a -gt $b ]; then echo "a>b" elif [$a -eq $b] then echo "a=b" else echo "a
for 循环
for循环一般格式为
for m in 1 2 3 4; do echo "values is $m" done for n in This is a dong; do echo $n done
while 语句
while 循环语法格式
int=5 while(($int>=2)); do echo "$int" let "int--" done echo "按下CTRL+D退出" echo "输入你喜欢的电影" while read film; do echo "${film} is ok" done
无限循环
while True; do echo "ok" done for ((::))
until循环
until 循环执行一系列命令直至条件为 true 时停止
a=0 until [ ! $a -lt 10 ]; do echo $a a=$((a+1)) done
case … esac
echo "输入1到4之间的数字" read -p "数字:" num case $num in 1) echo '1' ;; 2) echo '2' ;; 3) echo '3' ;; 4) echo '4' ;; esac s1='iambot' case $s1 in 'iambot') echo 'bot' ;; 'iampm') echo 'pm' ;; 'iampl') echo 'pl' ;; esac
跳出循环
break 命令允许跳出所有循环
echo "welcome to Amusement park" echo "please inter a number between 1-4" while : do read -p "please inter your number: " num case $num in 1|2|3|4) echo "you choose 1-4 you are great" ;; *) echo "you choose others you are bad" break ;; esac done
continue 命令不会跳出所有循环,仅仅跳出当前循环。
echo "welcome to Amusement park" echo "please inter a number between 1-4" while : do read -p "please inter your number: " num case $num in 1|2|3|4) echo "you choose 1-4 you are great" ;; *) echo "you choose others you are bad" continue echo 'game over' ;; esac done #运行代码发现,当输入大于5的数字时,该例中的循环不会结束,语句 echo "游戏结束" 永远不会被执行。