[9-13] Shell Series 3--branching structure if and case statements

Source: Internet
Author: User
Tags case statement logical operators



The purpose of this paper is to review 3 kinds of IF statements and case multi-branch structure statements that the shell implements for branching, and to supplement the execution efficiency of if and case.



Knowledge Reserve


    • The language structure in the shell script is still the 3 kinds: sequential structure, selection structure, loop structure

    • The branching structure is the same concept as the selection structure, and the execution path is chosen according to certain conditions, rather than the physical order in which the statements appear strictly

    • The IF statement and Case statement are specific representations of the branching structure implemented in the shell, with the IF and switch statements in the C language

    • The following statement I use structure flowchart, code implementation [code format], specific examples to illustrate







If single branch structure



Flow chart



650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/7F/wKioL1X_uuLwyqasAALMPJ7eTwo772.bmp "title=" Baidushurufa_2015-9-21_15-58-15.bmp "alt=" Wkiol1x_uulwyqasaalmpj7etwo772.bmp "/>



Code implementation


1
2
3
4
5


if [Expression];then
Statement1
Statement2
......
Fi


Example: If the current year is 2015, show "You have to study hard!"



Note that there are spaces before and after the [expression] expression! Otherwise syntax error


1
2
3
4
5


#!/bin/bash

if  [ $ (date +%y)  -eq  2015  ];then
        echo  "you should study hard!"
Fi


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/82/wKiom1X_vNbA4GCTAAQUgCxN1fY292.bmp "title=" Baidushurufa_2015-9-21_16-16-13.bmp "alt=" Wkiom1x_vnba4gctaaqugcxn1fy292.bmp "/>









If dual branch structure



Flow chart



650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/82/wKiom1X_vr-zWjjcAAUPqK9EKM4986.bmp "title=" Baidushurufa_2015-9-21_16-24-18.bmp "alt=" Wkiom1x_vr-zwjjcaaupqk9ekm4986.bmp "/>



Code implementation


1
2
3
4
5
6
7


if [Expreesion];then
Statment1
......
Else
Statement2
......
Fi


Example: Read the user input two number, compare the output large number


1
2
3
4
5
6
7
8


# !/bin/bash

read  -p  "enter 2 number: "  a b
if  [  $a  -ge  $b  ];then
    echo  
else
    echo 
Fi


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/80/wKioL1X_wjjiM9X3AAN6OHVfAVU933.bmp "title=" Baidushurufa_2015-9-21_16-29-38.bmp "alt=" Wkiol1x_wjjim9x3aan6ohvfavu933.bmp "/>









If multi-branch structure



Flow chart



650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/80/wKioL1X_xZnRwFs0AAvFgMqXFhw221.bmp "title=" Baidushurufa_2015-9-21_16-43-49.bmp "alt=" Wkiol1x_xznrwfs0aavfgmqxfhw221.bmp "/>



Code implementation


1
2
3
4
5
6
7
8
9


if [Expression1];then
Statement1
elif [Expression2];then
Statement2
elif [Expression3];then
Statement3
Else
Statment4
Fi


Example: Input test score, 90 points [including] above is a, 80-90 is B, 60-80 is C, etc., 60 below is D, etc.


1
2
3
4
5
6
7
8
9
10
11
12


#!/bin/bash

Read- P"Enter Your score:"Score
if[$score-ge -];then
Echo"Grade A"
elif[$score-ge the];then
Echo"Grade B"
elif[$score-ge -];then
Echo"Grade C"
Else
Echo"Grade D"
Fi


650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/73/82/wKiom1X_xZjiBEs_AAVlqGYaVq8250.bmp "title=" Baidushurufa_2015-9-21_16-52-53.bmp "alt=" Wkiom1x_xzjibes_aavlqgyavq8250.bmp "/>






If attention point



Judging conditions can use [expression] and [[expression]] Two, the effect is usually consistent but special case, [[]] is a keyword, for the string is better judged, it is recommended to use [[]] This



[[]] The judgment condition can use logical operations to combine multiple expressions, such as


1
2
3
4
5
6
7
8
9

Span style= "color: #008000;" > #当且仅当满足exp1和exp2时候才会执行command1
if     command1
Fi

 #Common logical operators
     -a  logic with
    -o  logical OR
    -!  non-



1
2
3
4
5
6
7
8
9
10


Form one
if [Expression];then
Command1
Fi
Form two
if [Expression]
Then
Command1
Fi








Case Multi-branch structure



Flow chart



650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M00/73/81/wKioL1X_z7ejtd7nAAvRfGuWdVs731.bmp "title=" Baidushurufa_2015-9-21_17-27-9.bmp "alt=" Wkiol1x_z7ejtd7naavrfguwdvs731.bmp "/>



Code implementation


1
2
3
4
5
6
7
8
9
10
11
12
13
14


Case Value in
Mode 1)
Command1
......
;;
Mode 2)
Command1
......
;;
*)
Command1
......
;;
Esac


Example: The user enters a number 1-3, if present, returns the number, otherwise the error


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17


#!/bin/bash

Read- P"Enter A number 1-3:"num
Case $numinch
1)
Echo"1"
;;
2)
Echo"2"
;;
3)
Echo"3"
;;
*)
Echo"sure the number from 1-3"
;;
Esac





650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/73/81/wKioL1X_0Yey7LieAAWbWDwTxcg068.bmp "title=" Baidushurufa_2015-9-21_17-34-25.bmp "alt=" Wkiol1x_0yey7lieaawbwdwtxcg068.bmp "/>



Case note Point



Each case branch, that is, a number of instructions contained in pattern N, must finally be ";; "Identifies the pattern matching execution end



Start with a case and end with a ESAC. ---> Exact word write-down






Execution efficiency of IF and case



Usually there is no difference between the two, can be used instead, but the efficiency of execution is different when there are many branches.



When the computer processes the case branch, it generates a jump table, jumps to the appropriate branch based on the value of the variable, and executes over



in the processing if branch, the number of jumps is variable, according to the order structure of a single decision to jump, such as the input results show grade that example, if the input is 95 then do not jump 1 times, if it is 45 points need to jump



Summary:



When selecting branches more, choosing the CASE...ESAC structure will improve the efficiency of the program, but it is not enough to handle only the variables of character or number type.



If...else structure is more flexible, if...else structure can be used to determine whether the expression is established, such as if (A+B>C), if...else the application of a wider range.



This article is from the "Blue Warehouse" blog, be sure to keep this source http://bluebox.blog.51cto.com/8852456/1696838



[9-13] Shell Series 3--branching structure if and case statements


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.