The shell script of Linux learning-------control flow structure

Source: Internet
Author: User

[This is my own study notes, welcome reprint, but please specify the source:http://blog.csdn.net/jesson20121020]

Start learning some shell flow control structures like other high-level languages today
Flow control Statements:1. If statement

Statement format:

If Condition1      then   command1else condition2then   command2else   Command3fi
NOTE: The If statement must be terminated with FI.

If there is no condition2, the IF statement can be simplified as follows:

If Conditionthen       command1else       Command2fi
Or:

If Conditionthen       Command1fi

Example:

if_test.sh

#!/bin/bash#if_testecho-n "Please input number (A and B):" Read a bif [$a-lt $b]then   echo "$a was less than $b" El If [$a-gt $b]then   echo "$a is greater than $b" else   echoes "$a ia equal to $b" fi

Give the executable permission to execute the script:

[Email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx if_test.sh[email protected]:~/develop/worksapce/ shell_workspace$./if_test.sh Please input the number (A and B): 1010 IA equal to 10[email protected]:~/develop/worksapce /shell_workspace$./if_test.sh Please input the number (A and B): 1210 is less than 12[email PROTECTED]:~/DEVELOP/WORKSAP ce/shell_workspace$./if_test.sh Please input the number (A and B): 8 is greater than 2
Note that this is used for comparison operators and more detailed comparison operators, please refer to man test.


2. Case Statements

Statement format:

Case value in

Mode 1)

Command1

;;

Mode 2)

Command2

;;

Esac


Attention:

The 1case value must be followed by the word in, each pattern must end with a closing parenthesis, and the value can be a variable or constant.

2 pattern Match * denotes any character;? means any single characters; [...] Represents any character in a class or range.

Example:

case_test.sh

#!/bin/bash#case_testecho-n "Enter a number from 1 to 3:" Read ncase $n in1)    echo "you select 1"    ;; 2)    echo "you select 2"    ;; 3)    echo "you select 3"    ;; *)  echo "you select *"    ;; Esac

[Email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx case_test.sh [email protected]:~/develop/worksapce /shell_workspace$./case_test.sh Enter A number from 1 to 3:1you select 1[email protected]:~/develop/worksapce/shell_work space$./case_test.sh Enter A number from 1 to 3:2you select 2[email protected]:~/develop/worksapce/shell_workspace$./cas E_test.sh Enter A number from 1 to 3:3you select 3[email protected]:~/develop/worksapce/shell_workspace$./case_test.sh En  ter a number from 1 to 3:*you select *[email protected]:~/develop/worksapce/shell_workspace$./case_test.sh Enter A number From 1 to 3:20you SELECT *
3. For Loop

Format:

For variable name in list

Do

Command 1

Command 2

Done

Example:

for_test1.sh

#!/bin/bash#for_testfor Loop in 1 2 3 4 5do    echo $loopdone
Operation Result:

[Email protected]:~/develop/worksapce/shell_workspace$./for_test1.sh 12345
for_test2.sh

#!/bin/bash#for_test2for Loop in orange red Blue Graydo   echo $loopdone
Operation Result:

[Email protected]:~/develop/worksapce/shell_workspace$./for_test2.sh Orangeredbluegray
for_test3.sh

#!/bin/bash#for_test3for loop in ' Cat name.txt ' do    echo $loopdone
Operation Result:

[Email protected]:~/develop/worksapce/shell_workspace$ cat Name.txt Jessoncherry[email protected]:~/develop/ worksapce/shell_workspace$./for_test3.sh Jessoncherry

4. Until Cycle

Format:

Until conditions

Do

Command

Done

until_test.sh

#!/bin/bash#until_testi=10until [$i-lt 0]do    echo $i let    i=i-1   # ((i= $i-1))   #i =$[i-1]done
Give permission to execute the result as follows:

[Email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx until_test.sh [email protected]:~/develop/ worksapce/shell_workspace$./until_test.sh 109876543210
  

5. While Loop

Format:

While command

Do

Command

.......

Done

Example:

while_test1.sh

#!/bin/bash#while_test1while echo-n "Enter your favorite star:" Read Mingxingdo    echo "$Mingxing is your favorite star" done
Give permission to execute:

[Email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx while_test1.sh [email protected]:~/develop/ worksapce/shell_workspace$./while_test1.sh Enter your favorite star: Zhou Jie Zhou Jie is your favorite star enter your favorite star: Sun Nan Sun Nan is the star you like
while_test2.sh

#!/bin/bash#while_testwhile Read Namedo   echo $NAMEdone <name.txt
Give permission to execute:

[Email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx while_test2.sh [email protected]:~/develop/ worksapce/shell_workspace$./while_test2.sh Jessoncherry

6. Break and Continue

Break [n]

-Exit Loop

-If you are in an embedded loop, you can specify N to jump out of the number of loops.

Continue

-Skip Cycle Step

Note that, as in other high-level languages, the Continue command is similar to break, with only a little difference, and it does not jump out of the loop, but is passing through the current loop step.

Example:

break_test.sh

#!/bin/bash#break_test.shwhile:d o    echo-n "Enter any number (1,..., 5):"    read ANS case    $ANS in    1|2|3|4| 5)        echo "You enter anumber between 1 and 5"        ;;    *)        echo "Wrong Number,bye" break        ;;        ;    Esacdone
Give permission to do the following:

[Email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx break_test.sh [email protected]:~/develop/ worksapce/shell_workspace$./break_test.sh Enter any number (1,..., 5): 1You Enter anumber between 1 and 5Enter any number (1, ..., 5): 2You Enter anumber between 1 and 5Enter any number (1,..., 5): 3You Enter anumber between 1 and 5Enter any number (1,.. ., 5): 4You Enter anumber between 1 and 5Enter any number (1,..., 5): 5You Enter anumber between 1 and 5Enter any number (1,..., 5): 6Wrong Number,bye

continue_test.sh

#!/bin/bash#continue_break_testwhile:d o     echo-n "Enter any number (1,..., 5):"     read ANS case     $ANS in     1| 2|3|4|5)         echo "You enter a number between 1 and 5"         ;;     *)         echo-n "wrong number,continue (y/n)?:"         read is_continue case         $IS _continue in         y|yes| y| Yes)                continue                ;;         *)                break                ;;         Esac     Esacdone
Similarly, give executable permission to execute

[Email protected]:~/develop/worksapce/shell_workspace$ chmod a+rx continue_test.sh [email protected]:~/develop/ worksapce/shell_workspace$./continue_test.sh Enter any number (1,..., 5): 2You Enter A number between 1 and 5Enter any Numbe R (1,..., 5): 3You Enter a number between 1 and 5Enter any number (1,..., 5): 4You Enter a number between 1 and 5Enter any Numbe R (1,..., 5): 5You Enter a number between 1 and 5Enter any number (1,..., 5): 8Wrong number,continue (y/n)?: Yenter any number (1,. .., 5): 1You Enter a number between 1 and 5Enter any number (1,..., 5): 79Wrong number,continue (y/n)?: N




The shell script of Linux learning-------control flow structure

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.