Special usage One while
Syntax Format
while read line; Do
Loop body
Done </path/from/somefile
Function: Reads each row in the/path/from/soemfile file sequentially and assigns this line to the line variable
Example: Scan a/etc/passwd file for each row, if the Discovery gecos field is empty, populate the user name and the unit phone as 123, and prompt the user for Gecos information to be modified successfully
#!/bin/bash#file=/etc/passwdwhile read line; Do ge= ' echo $LINE | cut-d:-f5 ' user= ' echo $LINE | cut-d:-f1 ' If [[-Z $GE]]; Then Usermod-c "${user}123" $USER echo "Modify $USER finished ..." else echo "$USER can ' t modify" Fidone < $FILEunset FILE GE USER
Special usage Two for
Syntax format
For (control variable initialization; conditional judgment expression; control variable correction expression); Do
Loop body
Done
Function: The assignment judgment and the correction condition are all completed in (()), satisfies the judgment expression namely executes once the loop body, the end will carry on the control variable correction operation First, then makes the condition judgment.
This usage is similar to while and can be completely replaced, but this usage is more appropriate for the calculation of numbers.
Example: Calculating the sum of 1-10
#!/bin/bashdeclare-i sum=0for ((i=1; i<=10; i++)); Do Sum=$[sum+i]doneecho $sumunset sum i
Special usage Three Select
Syntax format
Select variable in list; Do
Circular Body Command
Done
Features: The Select loop is used primarily to create menus and display the PS3 prompt, and user input is saved in the built-in variable reply. Common with case, exit exit loop
Example: make a menu that shows the item and its price, and if you enter an error, prompt for an error message and exit.
#!/bin/bashps3= "What would do:" select menu in Noodles dumpling rice; Do case $menu in noodles) echo "Your choice is $REPLY, 10$"; Dumpling) echo "Your choice is $REPLY, 15$";; Rice) echo "Your choice is $REPLY, 20$";; *) echo "Your choice is error:" break;; Esacdone
This article is from the "dmwing" blog, make sure to keep this source http://dmwing.blog.51cto.com/11607397/1840555
Shell Special Usage