1 for statement
Syntax format
For variable in value (or loop condition)
Do
Command
Done
Send mail to multiple users
#! /bin/bash
Domain=163.com
For user in Tom Hom Jem
Do
Mail-s "Happy New Year" [email protected] $domain </var/log/messages
Done
To print 9*9 multiplication formulas
#!/bin/bash
For i in {1..9}
Do
For ((j=1,j<=i,j++))
Do
printf "%-8s" $j * $i =$ ((j*i))
Done
Echo
Done
2 While statement
Syntax format
While condition do command done
While read-r command done < file
Add 20 users in bulk, user name usern,n to 1 to 20 digits
#!/bin/bash
U=1
While [$u-le 20]
Do
Useradd user${$u}
u=$ ((u+1))
Done
Read the print NIC profile by line
#!/bin/bash
File=/etc/sysconfig/network-scripts/ifcfg-eth0
While Read-r line
Do
Echo $line
Done < $FILE
3 until statements
Syntax format
Until condition do command done
Bulk delete users, user name Usern,n 1 to 20 digits
#!/bin/bash
U=20
Until [$u-eq 0]
Do
Userdel user${$u}
u=$ ((u-1))
Done
4 SELECT statement
Generate a Query menu with SELECT
#! /bin/bash
echo "Where is You"
Select Var in "Shenzhen" "Guangzhou" "Meiguo" "Xianggang"
Do
Break
Done
echo "You is from $var"
5 Control statements
Shift shifts the position parameter one bit to the left, that is, after shift is executed, it becomes $
Countinue interrupts the current loop and enters the next loop
Break ends the entire loop
Exit to end the run of the script
This article is from the "Practical Linux knowledge and Skills sharing" blog, please be sure to keep this source http://superleedo.blog.51cto.com/12164670/1889347
Shell Loop Statement Application Example