The Advanced shell script

Source: Internet
Author: User

I. Condition selection, judgment (if, case)
1.1 If statement usage and examples
When we encounter the need to judge in the script, we can use the IF statement to implement. The specific syntax is as follows:

Single Branch
if judgment condition; then

Branch code with true condition
Fi

Dual Branch
if judgment condition; Then

Branch code with true condition

Else

The condition is a false branch code

Fi

Multi-Branch
if judgment condition 1; Then

Branch code with true condition
Elif judgment Condition 2; Then

Branch code with true condition
Elif judgment Condition 3; Then

Branch code with true condition

Else

The above conditions are false branch codes
Fi

In a multi-branch, the system will judge the conditions you write, the first time you encounter a "true" condition, execute the branch, and then end the entire if statement.

Note: 1. If and fi are paired up

2. If statements can be nested.

Example: Compare the size of a two number

Execution results

1.2 Case usages and examples
When it comes to multiple criteria matching, we can be very troublesome with if, at this time, we could write this script in case. The specific syntax for case is as follows:

Case variable reference in

PAT1)

Branch 1

;;

PAT2)

Branch 2

;;

...

*)

Default Branch

;;

Esac
Note: 1, case after each branch, are to two ";" End (the last one can be omitted)

       2、case和esac是成双成对的        举例子:编写一个脚本,提示用户输入信息,判断其输入的是yes或no或其他信息。        ![](http://i2.51cto.com/images/blog/201803/24/4355fb2065ecc51680a902626cd4dcb7.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)        执行结果:        ![](http://i2.51cto.com/images/blog/201803/24/ad8d2cdec31424863caf3bc06a8e6eff.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)        二、循环语句        2.1 for循环

The For loop is executed once by assigning the element in the list to the variable name, executing the loop body once each assignment, until the element in the list is exhausted, and the loop ends. There are two basic grammar types:

1) for variable name in list; Do

Loop body

Done

about how the list is generated, as follows:

① directly to the list

List of ② integers:

(a) {start...end}

(b)seq start end

③ command to return a list

$(COMMAND)

④ uses glob wildcard characters such as:

  *.sh

⑤ variable Reference

$i, $*

2) for ((EXP1; exp2; exp3)); Do

Loop body

Done

Clearer can be seen from:

Example:
Print 99 multiplication table

Execution Result:
2.2 While Loop
While loops are slightly more complex than for loops, with the following syntax:

While CONDITION; Do

  循环体

Done

Note: 1, enter the condition: condition is true; Exit Condition: Condition is false.

  2、CONDITION为循环控制条件:进入循环之前,先做一次判断;每一次循环之后会再次做判断;条件为“true”,则执行一次循环;知道条件测试状态为“false”终止循环。  3、CONDITION一般应该有循环控制变量;此变量的值会在循环体不断地被修正。

Example: Calculates the sum of all positive odd numbers within 100

Execution Result:

2.3 Until cycle
The syntax of the until loop is similar to the syntax of while, but the entry and exit conditions are just the opposite, so it is not commonly used, as long as we understand. The specific syntax is as follows:

Until CONDITION; Do

Loop body

Done

Note: 1, enter the condition: condition is false; Exit Condition: Condition is true.

         2、do和done成对出现。     举例子:循环输出1-10     ![](http://i2.51cto.com/images/blog/201803/24/770011052148f526d1dd99d373b960bf.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)     执行结果:     ![](http://i2.51cto.com/images/blog/201803/24/0561e0380be7bcc3277c839ce466b3b7.png?x-oss-process=image/watermark,size_16,text_QDUxQ1RP5Y2a5a6i,color_FFFFFF,t_100,g_se,x_10,y_10,shadow_90,type_ZmFuZ3poZW5naGVpdGk=)     4 select循环

The Select loop is used primarily to create menus, and menu items in numerical order are displayed on standard errors, and a PS3 prompt is displayed waiting for user input.

The user enters a number in the menu list and executes the corresponding command.

The user input is saved in the built-in variable reply.

The specific syntax for select is as follows:

Select variable in list; Do

Circular Body Command

Done

Note: ①select is a wireless loop, so remember to exit the loop with the break command, or terminate the script with the Exit command. You can also press CTRL + C to exit the loop.

②select are often used in conjunction with case.

③ is similar to a for loop, you can omit the in list and use positional variables.
Example: Generate a menu and display the selected price.

Execution Result:

Finally tell everyone, the script this thing must first understand the grammar rule, the practice, only then will digest.

The Advanced shell script

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.