Shell Process Control

Source: Internet
Author: User
Tags closing tag

Shell Process Control

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

<? phpif(isset($_get["Q"])){ search(q);} else{//Don't do anything }               

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

Write a line (for the terminal command prompt):

If[ $(-|  -"ssh")-1];  Then"true";  Fi              

The If Else statement is often used in conjunction with the Test command, as follows:

Num1=$[2*3]Num2=$[1+5]if TEST $  [ num1    - eq $  [   then   echo  ' two numbers equal! ' else   Echo  ' two numbers are not equal! ' fi         

Output Result:

Two numbers equal!
The For loop in list can contain replacements, strings, and file names.

For example, sequentially output the numbers in the current list:

for in1 2 3 4   5do' The value is: $loop '        Done

Output Result:

the value is: 1theis: 2the value is: 3the value is : 4the  value is: 5    

Characters in the sequential output string:

for in' the ' A string ' do echo $str done    

Output Result:

The ISstring 
While statement#!/bin/sh
int=1 while(   $int<= 5  ) does let ' int++ '          Done 

Run script, Output:

12 34 5 

Press <Ctrl-D> to end the loop.
' Press <CTRL-D> exit '-' Enter your favorite movie name: ' while Read FILM do"Yes! $FILM is a good movie " done    

Run the script with the output similar to the following:

Press  <CTRL-D> exit Enter your favorite movie name: w3cschool Beginner Tutorial Yes! w3cschool Rookie Tutorial is a good movie            
Infinite loops

Infinite loop Syntax format:

 while   :do command   Done

Or

 while   truedo command   Done

Or

For((;; ))

Until cycle

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

The Until loop and the while loop are just the opposite of the processing mode.

The general while loop is better than the until loop, but at some point-and only in rare cases-the until loop is more useful.

Until syntax format:

until  conditiondo    command   Done

The condition can be any test condition, and the test takes place at the end of the loop, so the loop executes at least once-note this.

The case value must be followed by a wordinch, each mode must beend of closing parenthesis。 Value can be considered asvariable or constant。 When a match discovery value conforms to a pattern, all commands begin to execute until;;。

once the pattern matches, the other mode is no longer resumed after the corresponding command is executed . If there is no matching pattern, use an asterisk * to capture the value and then execute the subsequent command.

The following script prompts you to enter 1 to 4 to match each pattern:

Echo' Enter a number from 1 to 4: 'Echo' The number you entered is: 'Read AnumCase$aNumInch 1)  echo  ' you selected 1 '  ;  2)  echo  ' you selected 2 '  ;;  3)  echo  ' you selected 3 '  ;;  4)  echo  ' you selected 4 '  ;;  *)  echo  ' you did not enter a number between 1 and 4 '  ; esac             

Enter different content, there will be different results, for example:

Enter a  number from 1 to 4 : The number you entered is:3 you have selected 3       
Break command

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/bashWhile :DoEcho-n  "Enter numbers from 1 to 5:"  read anum case $aNum in 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       

Execute the above code and the output is:

Input 1  to   5 3 The number you entered is  3! input  1  to  5 7  The number you entered is not  1  5 !  Game over                
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/bashWhile :DoEcho-N "Enter numbers from 1 to 5:"  read anum case $aNum in 1| 2| 3| 4| 5  echo  "The number you entered is $aNum!"  ;  *)  echo  "The number you entered is not between 1 and 5!"  continue echo  "Game Over"  Span class= "pun" >;  esacdone       

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

Esac

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.

Shell 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.