eg:
Expect:
1. Environment variables randomly generates a random number RANDOM within a 100
2.read read the current input
3. Compare current input randomly generated number
4. SU loop out when the two numbers, and counted (n times the comparison results are equal)
1 #!/bin/bash 2 i=0 3 num=$(expr $RANDOM % 100) 4 echo $num 5 while true 6 do 7 let i++ 8 read -p "Please input number(1-100):" digit 9 if [ $digit -eq $num ]; then 10 echo $digit 11 echo "一共$i 次!!" 12 break 13 elif [ $digit -lt $num ]; then 14 echo "The number is less!" 15 elif [ $digit -gt $num -o $digit -gt 100 ]; then 16 echo "The number is bigger" 17 #elif [ $digit -gt 100 ]; then 18 # echo "not in the range!! Please reinput." 19 else 20 continue 21 fi 22 done
eg:
Review a simple example, simply enter the value of n to obtain a secondary, and finally print out all the values obtained
1 #!/bin/bash 2 attr=() 3 num=0 4 while true 5 do 6 read -p ">>input:" name 7 attr[$num]=$name 8 echo ${attr[$num]} 9 let num++ 10 if [ $num -eq 3 ]; then 11 echo ${attr[*]} 12 exit 13 fi 14 done
eg: for loop
The difference continue and break: eg
continue the end of this cycle, the next cycle be
Skip break this cycle
1 #!/bin/bash 2 for i in `seq 10` 3 do 4 if [ $i -eq 3 ]; then 5 continue 6 else 7 echo $i 8 fi 9 done 10 11 #!/bin/bash 12 int=0 13 while : 14 do 15 let int++ 16 if [ $int -lt 10]; The then . 17 echo $ int 18 is the else . 19 BREAK 20 is Fi 21 is DONE 22 is ==================== 23 is for the use of loop 24 # / bin /! The bash 25 for ((A = . 1 ; A < 10 ; ++ A )) 26 is do 27 echo $ A 28 DONE 29 ==================== 30 nested loops 31 # /! bin / the bash 32 for ((A = 0 ; A < 10 ; A ++)) 33 do 34 echo "outer loop: $a" 35 for ((b=1;b<10;b++)) 36 do 37 if [ $b -eq 4 ]; then 38 continue 39 else 40 echo "inner loop: $b" 41 fi 42 done 43 done 44 ====================