Process Control of Shell script programming

Source: Internet
Author: User
Tags case statement closing tag

1. Unlike Java, PHP and other languages, SH's process control is not empty, such as (the following is the PHP Process Control notation):

You can't write this in Sh/bash, and if the Else branch doesn't have a statement execution, don't write this else.

2.if

#if statement syntax format::<<! if conditonthen   command1   command2 ....   COMMANDNFI! if " SSH " 1 " true "; fi

3.if Else:

#if Else statement syntax format::<<! if conditionthen  command1  command2  ... CommandNElse  COMMANDFI!

4.if else-if Else:

#if else-if Else statement syntax format::<<! if condition1then  command1elif condition2then  command2else  commandnfi!

Instance: The If Else statement is often used in conjunction with the test command

A=Tenb= -if[$a = =$b]then Echo"a equals b"elif [$a-GT $b]then Echo"a greater than B"elif [$a-LT $b]then Echo"A is less than B"ElseEcho"there are no conditions to meet"Finum1=$[2*3]num2=$[1+5]ifTest $[NUM1]-eq $[num2]then Echo"two numbers equal!"ElseEcho"two numbers are not equal!"fi

Operation Result:

5.for Loop:

#  for Loop General format::<<!  for var inch item1 item2 ... itemn  Do   command1  command2  ... Commandndone! #写成一行:  for var  in Item1 item2 ... itemn;  Do Command1;command2. Done; #当变量值在列表里, the For loop executes all commands once, using the variable name to get the current value in the list. The Life  > order can be any valid shell command and statement. The in list can contain replacements, strings, and file names. 

For loop instance:

 for inch 1 2 3 4 5  Do  "Thevalue is: $loop"do for in" This was a string" do echo $strdone

Operation Result:

6.While statement: The whileloop is used to continuously execute a series of commands and to read data from the input file; the command is usually a test condition

The # while statement is formatted as::<<!  while condition  Do Commanddone! Int=1while (($int <= 5)) do  echo $int let  "int++"         //using the Bash let command, which is used to execute one or more expressions, the variable calculation does not need to add $ To represent the variable done

Operation Result:

7.while loops can be used to read keyboard information, examples:

" Press <CTRL-D> Exit "  " Enter your favorite site name:" while the read FILMdo"  Oh, yes! $FILM is a good site " Done

Operation Result:

8. Infinite Loop:

#无限循环语法格式::<<!  while  Do Commanddone
or whiletruedo commanddone
Or
for (( ; ;))

9.until loop: The Until loop executes a series of commands until the condition is true, while the general while loop is better than the until loop, but at some point-and in rare cases, the until loop is more useful

#until Syntax format::<<! until condition  Do The   commanddone# condition can be any test condition, and the test takes place at the end of the loop, so the loop is executed at least once-please note this. !

10.Case: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!

The case works as shown above: The value must be followed by the word in, and each pattern must end with a closing parenthesis. the value can be either a variable or a constant.  after the match discovery value conforms to a pattern, all commands begin to execute until;; So far

The value will detect each pattern that matches, and once the pattern match is successful, the other mode is no longer followed by the corresponding command after the match mode is executed, and if there is no matching pattern, use an asterisk * to capture the value and then execute the subsequent command

The syntax of the case differs greatly from the C family language, which requires a ESAC (which is case, in turn) as the closing tag, each case branch with a closing parenthesis and a two semicolon for break.

Case Example:

Echo"Enter a number from 1 to 4:"Echo"the number you entered is: '"Read Anum Case$aNuminch  1) echo"you chose 1."  ;; 2) echo"you chose 2."  ;; 3) echo"you chose 3."  ;; 4) echo"you chose 4."  ;; *) echo"you didn't enter a number from 1 to 4."  ;; Esac

Operation Result:

11. Jump out of the loop: in the loop, sometimes you need to force out of the loop when the loop end condition is not reached, the shell uses two commands to implement the function: Break and Continue.

(1) Break command: The breakcommand allows you to jump out of all loops (all loops after execution is terminated)

 while : DoEcho-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

Operation Result:

(2) Continue: Thecontinue command is similar to the break command with only a little difference, it does not jump out of all loops, just jumps out of the current loop

 while : DoEcho-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";; Esacdone

Run Result: Run 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.

Process Control of Shell script programming

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.