Scripting for the special use of the while statement under the Linux Bash Program (10)
1. Loop control:
Break: Interrupts the entire loop statement, which is the statement following the execution of the script after exiting the loop
Continue: Interrupt the current cycle, advance to the next round cycle
Exit: End Script Run
2.while dead Loop, that is, when you do not know how many times the loop
Format: while:; Do
Looping statements
Done
3.while reads and assigns a value to the read variable from each line in the input redirection file
Format: while read Line;do
Looping statements
Done < path files
4. Instance Scripts
4.1. Find the user login in the/etc/passwd file is bash,uid greater than 500 of the top 3 users, and display their user name and UID
#!/bin/bash
Let I=1
While read line; Do
If [' Echo $LINE | cut-d:-f3 '-lt 500]; Then determine if the UID is less than 500
Continue
Fi
[' Echo $LINE | awk-f: ' {print $7} ' = = '/bin/bash '] && echo $LINE | Awk-f: ' {print $1$3} ' && let ++i
[$I-gt 3] && break
Done < /etc/passwd
~
4.2. Check if the willow user is logged into the system, unless you enter Q or Q to exit the script, otherwise enter the dead loop
#!/bin/bash
While :;d o
Read-p "q| Q is Exit,other show does Willow come this system? "CHOICE
if [$CHOICE = = "Q"-o $CHOICE = = "Q"]; Then
echo "quiting this script"
Exit 3
Fi
If who | grep Willow &>/dev/null; Then
echo "Willow is loginnig the system at ' date '"
Else
echo "Willow is not comming at ' date '"
Fi
Done
This article is from the "Xavier Willow" blog, please be sure to keep this source http://willow.blog.51cto.com/6574604/1767492
Scripting for special usage of Bash programming loop statements under Linux (10)