Shell Basic Learning (vi) Process Control

Source: Internet
Author: User
Tags case statement

1. If

Syntax format for IF

if Conditon  Then     Command1    command2       "    commandnfi

2. If Else

if Conditon  Then     Command1    command2       "    commandnelse    Command1    Command2       "'    commandnfi

3. If ElseIf else

if Conditon  Then     Command1    command2       "    commandnElseif  conditon    Command1    Command2 '    commandnelse    command1    command2       '    CommandNfi

4. For loop

 for inch item1 item2 item3)  Do Command1command2 ' CommandN  Done

5. While

 while condition  Do     Command1    command2       "'    commandndone    

6, until

The Until loop executes a series of commands until the condition is true.

until condition  Do     commanddone

7. Case

The Shell Case statement is a multi-select statement. A case statement can be used to match a value with a pattern, and if the match succeeds, the matching command is executed. The case statement is in the following format:

  

 Case inch mode 1)    Command1    command2 ...    CommandN    ;; Mode 2)    Command1    command2 ...    CommandN    ;; Esac

8. Break

The break command allows you to jump out of all loops (all loops after the execution is terminated).

In the following example, the script enters a dead loop until the user enters a number greater than 5. To jump out of this loop and return to the shell prompt, you need to use the break command.

  

#!/bin/Bash while : Do    Echo-N"Enter a number from 1 to 5:"Read Anum Case$aNuminch        1|2|3|4|5)Echo "the number you entered is $aNum!"        ;; *)Echo "the number you entered is not between 1 and 5! Game over"Break ;; EsacDone
Results:Input 1 to 5 3 The number you entered is Span class= "PLN" > 3! input 1 to 5 7
The number you entered is not 1 5 ! Game over

9, continue

The continue command is similar to the break command, with only a little difference, and it does not jump out of all loops and just jumps out of the current loop.

To modify the above example:

  

#!/bin/Bash while : Do    Echo-N"Enter a number from 1 to 5:"Read Anum Case$aNuminch        1|2|3|4|5)Echo "the number you entered is $aNum!"        ;; *)Echo "the number you entered is not between 1 and 5!"ContinueEcho "Game Over"        ;; Esac Done

Running code discovery, when a number greater than 5 is entered, the loop in the example does not end, and the statement echo " Game Over " is never executed.

Shell Basic Learning (vi) Process Control

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.