1, for loop:
Statement format:
Analyzing loop for i in
do
Loop
done
Example: multiplication table (for loop version)
#!/bin/bash # Author: Sean Martin # Blog: https://www.cnblogs.com/shy13138/ # Time: 2019-08-16 10:35:48 # Name: 99for.sh # Version: v1.0 for i in {1..9};do for j in $(seq $i);do echo -ne "$i*$j=$((i*j)) " done echo '' done
2, while circulation
Statement format:
Analyzing while loop
do
Loop
done
Example: finger-guessing game
# / bin / bash! # Author: Sean Martin # Blog: https://www.cnblogs.com/shy13138/ # Time: 2019-08-16 10:35:48 # the Name: caiquan.sh # Version: v1. 0 J =. 1 the while [$ J -le. 5] do echo "stone 1. 2. 3. scissors cloth" Read -p "Please punch 1-3:" I IF [-ne. 1 -o $ I $ I - 2 -o $ I -ne NE. 3]; the then echo "Please enter a number between 1-3" Fi Game = (Janken) NUM = $ ((the RANDOM. 3%)) echo Computer Game = $ {[$ NUM]} Case in I $ . 1) IF [0 -eq $ NUM]; the then echo "draw" elif [-eq $ NUM. 1]; the then echo "you lose" echo" you win " Fi ;; esac else echo "you win" Fi ;; 2) IF [-eq $ NUM. 1]; the then echo "draw" elif [$ 0 -eq NUM]; the then echo "you lose" the else echo "you win" Fi; ; 3) IF [2 -eq $ NUM]; the then echo "draw" elif [1 -eq $ NUM]; the then echo "you lose" the else the let J ++ DONE
3, until the cycle
until the cycle while loop
Statement format:
Analyzing until loop
do
Loop
done
For example:
99 multiplication table (until version)
#!/bin/bash # Author: Sean Martin # Blog: https://www.cnblogs.com/shy13138/ # Time: 2019-08-16 10:35:48 # Name: 99until.sh # Version: v1.0 i=1 until [[ $i -gt 9 ]] do j=1 until [[ $j -gt $i ]] do let "sum = $i*$j" echo -n -e "$i*$j=$sum\t" let "j++" done echo "" let "i++" done