Shell condition limit

Source: Internet
Author: User
Conditional Criterion

Just talk about 『Program", Then the conditional criterion, that is, the criterion" If then ", must be learned! Because in many cases, we have to determine how the program should proceed based on certain data.

If... then

This if... then is the most common condition criterion ~ To put it simply, when a condition is met, a job is performed. The judgment of this if... then has many levels! We will introduce them as follows:

 

Single-layer, simple conditional

If you only have one sequence type, we can simply look at it as follows:

If[Conditional criterion];ThenThe command work content that can be executed when the condition criterion is set;Fi<= SetIfIn turn, it becomesFiNow! EndIfMeaning!

Example:

[Root @ WWW Scripts] # CP Sh06. Sh Sh06- 2 . Sh <= It's faster to use it! [Root @ WWW Scripts] # vi sh06 - 2 . Sh  # ! /Bin/ Bash # program: # This program shows the user  '  S choice  # History :#  2005 / 08 / 25  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport pathread -P "  Please input (y/N ):  "  YN If [ "  $ YN  " = "  Y  " ] | [ "  $ YN  " = "  Y  " ]; Then      Echo   " OK, continue  "  Exit  0  Fi  If [ "  $ YN  " = "  N  " ] | [ "  $ YN  " = "  N " ]; Then      Echo   "  Oh, interrupt!  "  Exit  0  Fi  Echo   "  I don't know what your choice is  " & Exit 0 

 

Multiple and complex condition types

Simple syntax

# One condition is used to determine whether the score is successful or fails (Else)If[Conditional criterion];ThenThe command work content that can be executed when the condition criterion is set;ElseCommand work content that can be executed when the conditional statement is invalid;Fi

In more complex cases, you can use this syntax:

# Multiple condition judgment (If...Elif...Elif...Else) Run in different situationsIf[Condition criterion 1];ThenThe command work that can be performed when the conditional statement is created;Elif[Condition method 2];ThenThe command work content that can be executed when the condition is set to condition 2;ElseCommand work content that can be executed when neither of condition 1 nor 2 is true;Fi

Example

[Root @ WWW Scripts] # CP Sh06- 2 . Sh Sh06-3 . Sh  [Root @ WWW Scripts] # vi sh06 - 3 . Sh  # ! /Bin/ Bash # program: # This program shows the user  '  S choice  # History :#  2005 / 08 / 25  Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport pathread -P "  Please input (y/N ):  "  YN  If [ "  $ YN  " = "  Y  " ] | [ "  $ YN  " ="  Y  " ]; Then      Echo   "  OK, continue  "  Elif [ "  $ YN  " = "  N  " ] | [ "  $ YN " = "  N  " ]; Then      Echo   "  Oh, interrupt!  "  Else      Echo   "  I don't know what your choice is  "  Fi 

 

Use Case... esac to judge

The syntax is as follows:

  case  $ variable name  in  <= Keyword:  case  , there is also a rich front variable font size  "   content of the first variable  " ) <=  we recommend that you enclose each variable in double quotation marks, and enclose the keyword in parentheses.) program section;  <=  each category ends with two consecutive semicolons!   "  content of the second variable  "  ) program segment;  *) <= The Last variable will use *  to indicate that all other program programs whose values do not contain the content of the first variable and the content of the second variable exit   1  ;;  esac  <= final  case  end! "Write in turn" Think about it! 

Example

[Root @ WWW Scripts] # vi sh09- 2 . Sh  # ! /Bin/ Bash # program: # Show  "  Hello  " From $ 1 ... By using Case .... Esac  # History :#  2005 / 08 / 29 Vbird first releasepath =/Bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin :~ / Binexport path  Case $ 1   In    "  Hello  "  )  Echo   "  Hello, how are you?  "  ;;  "" )  Echo   "  You must input parameters, ex> {$0 someword}  "  ;; *) # Actually, it is equivalent to bytes, 0 ~ Infinite number of arbitrary bytes!  Echo   "  Usage $0 {Hello}  "  ;;  Esac 

 

Self-http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_4.php#sh06

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.