Program programming
Select Execute
Sequential execution: If, case
Loop execution: For, while, until
For loop list
For i in LIST; Do loop body donefor i in Listdo loop body done
650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/A4/E9/wKioL1m0D4WwE7hkAAApDfCzWpM544.png "title=" Qq20170909235749.png "alt=" Wkiol1m0d4wwe7hkaaapdfczwpm544.png "/>
While loop
While CONDITION; Do loop body Donewhile Conditiondo loop body donecondition cycle control condition, each time the loop body is executed, it needs to be judged true again, the loop is exited when the loop is false.
Exercise: Sum of all positive integers within 100
Exercise: within 100 all even and
Exercise: Add 10 users
Exercise: Use the ping command to detect the online status of all hosts within the 172.16.250.1-254 range. Using while loop
Exercise: printing 9x9 multiplication tables
Exercise: Use random to generate 10 random numbers, output these 10 numbers, and show the largest and smallest of the two
Exercise: Sum of all positive integers within 100
#!/bin/bash#declare-i sum=0for i in {1..100}; Dolet sum= $sum +1doneecho $iecho "Summary: $sum"
#!/bin/bash#declare-i sum=0declare-i i=1while [$i-le 100]; dosum=$[$sum + $i]let i++doneecho $iecho "Summary: $sum"
Exercise: within 100 all even and
#!/bin/bash#declare-i sum=0declare-i i=2while [$i-le 100]; dosum=$[$sum + $i]let i+=2doneecho $iecho "Summary: $sum"
Exercise: Add 10 users User1-user10
#!/bin/bash#declare-i i=1declare-i users=0while [$i-le 10]; Doid user$i &>/dev/nullretval=$? [$retval-ne 0] && useradd user$i &>/dev/null[$?-eq 0] && echo "ADD user user$i finished" &am p;& let users= $users +1doneecho "Add $users users"
Exercise: Use the ping command to detect the online status of all hosts within the 172.16.250.1-254 range. Using while loop
#!/bin/bash#declare-i ip= ' 172.16.250. ' Declare-i i=1declare-i uphosts=0declare-i downhosts=0while [$i-le 254]; Doif ping-c 1-w 1 $IP $i &>/dev/null; Thenecho "$IP $i is up" let Uphosts++elseecho "$IP $i are down" let Downhosts++fidoneecho "uphosts: $uphosts" echo "Downhosts: $ Downhosts "
Exercise: printing 9x9 multiplication tables
#!/bin/bash#for i in {1..9}; Dofor j in $ (SEQ 1 1 $i); Doecho-ne "${j}x${i}=$[$i * $j]\t" j=$ (($j + 1)) Doneechodone
#!/bin/bash#declare-i I=1while [$i-le 9]; Dodeclare-i j=1while [$j-le $i]; Doecho-ne "${j}x${i}=$[$i * $j]\t" j=$ (($j + 1)) doneechoi=$ (expr $i + 1) Done
Exercise: Use random to generate 10 random numbers, output these 10 numbers, and show the largest and smallest of the two
#!/bin/bash#declare -i max=declare -i min=declare -i i=1declare -i randomwhile [ $i -le 10 ]; dorandom= $RANDOMecho $RANDOMIF [ $i -eq 1 ]; thenmax= $randommin = $maxfiif [ $i -gt 1 ]; thenif [ $random -gt $max ]; thenmax= $randomfiif [ $random -lt $ min ]; thenmin= $randomfifilet i++doneecho "max: $max" echo "min: $min" ====== ====================================#!/bin/bash#declare -i max=declare -i min=declare -i i=1declare -i randomwhile [ $i -le 10 ]; dorandom=$ randomecho $randomif [ $i -eq 1 ]; thenmax= $randommin = $maxfiif [ $random -gt $max ]; thenmax= $randomfiif [ $random -lt $min ]; thenmin= $randomfilet i++doneecho "max: $max" echo "min: $min"
This article is from the "Reading" blog, make sure to keep this source http://sonlich.blog.51cto.com/12825953/1964037
---------Linux Script Programming---for and while applications