Shell Special Usage

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.