Record a few simple examples of loop-related
1. Wait for the expected content to be entered
#!/bin/Bashuntil["$yn"=="Yes"-O"$yn"=="YES" ] DoRead-P"Please input yes/yes to stop this progras:"yn DoneEcho "ok! You input the correct answer."
Wait to enter expected content before exiting
Also available while implementation
2. Multi-digit summation
While implementation
i=0s=0 while"$i"" ]do i1)) s=$ (($s + $i))done echo""
Implemented with for
#!/bin/bashs=0"" nu for ((i=1; I<= $nu; i=i+1 ) do- s=$ (($s + $i))doneEcho " "
Execution results
3. String processing
A For loop handles file names individually
#!/bin/bash for on 'ls'do echo $var Done
Execution results are as follows
For loop processing user information
#!/bin/bashusers=$ (cut -D:-f1/etc/passwd) for in $users do ID $username finger $username Done
For loop find connectivity for specified IP address
#!/bin/bashnetwork="192.168.1" forSitenuinch$(seq the the) Do Ping-C1-W 1${network}.${sitenu} &>/dev/NULL&& result=0|| result=1 if["$result"==0]; Then Echo "Server ${network}.${sitenu} is up" Else Echo "Server ${network}.${sitenu} is down" fi Done
Execution Result:
Shell simple Use (b) loop