Control judgment statements in shell programming

Source: Internet
Author: User
Tags case statement

If

    • Single-format and nested
if conditional expression; then #当条件为真时执行以下语句  command list else  #为假时执行以下语句    command List  fiif  Statements can also be nested using   an if  condition expression 1;then      if  condition expression 2;then          command list       Else          if  condition expression 3;then              command list           else               command list          fi      fi  else   command list  
    • Multi-branch format
if "  $ "   ; then    LPR $1  "$"#elif with Else If    (CD $1; LPR $1)  else    " $ not a file or directory "   fi
    • Case
 Case inch 1 )          1;;      #表示第一个分支结束 2)           22;;      #第二个分支结束*)      #表示其他可能值 must be at the end, otherwise the statement behind him cannot execute the echo Unkownesac    #Case statement ends
    • While/until
 whileAn expression Docommand List done Sum=0I=0   while true#true是系统的关键词 Express the True DoI= ' Expr $i +1' Sum= ' Expr $Sum +$i 'if[$i =" -" ]; Then Break; Fi done echo $i $Sum Last this program shows - 5050  

Let's change the program a little bit later.

sum=0I=0   while[$i! =" -" ]   DoI= ' Expr $i +1' Sum= ' Expr $Sum +$i ' done echo $i $Sum The result of the program operation is the same as above, but the program is more concise than the above in this cycle can also be until as a test condition it is exactly the same as the condition of the while test, that is, when the condition is false will continue to execute the statement in the loop body, otherwise  Exit the loop body and use this example below. Sum=0I=0until [$i=" -" ]   DoI= ' Expr $i +1' Sum= ' Expr $Sum +$i ' done echo $i $Sum when I is not equal to 100 the loop is when the condition is a false loop, otherwise it exits, and the first example is when I is not equal to 100 cycles, that is, the test condition is true. 

For

    • Enumeration usage
 for inch list  of names  Do     Command List   for in {1..  Ten }do  echo $ndonefor in  a b c d e;  Do   Echo $letterdone
    • File usage
 for  in *does       echo $filedonefor in  A1 A2 A3 A4 A5  do     diff AA/$File bb/$File  
    • Cumulative format
 for ((i=1; $i <i++ ))  Do  for ((i= 1; $i < 1 '))     Do   Echo $idone
    • Other loop control statements

The break command does not perform the current loop within the body of a break, and the following statement exits from the current loop.
The Continue command is a program that ignores the following statement in this body and executes from the loop header.
 

    • Determine the properties of a file
Format:- operator filename  -e file exists return 1, otherwise return 0  -R file readable return 1, otherwise  return 0-w file can write return 1, otherwise return 0   -x file can be returned 1, otherwise 0  -o file belongs to user return 1, otherwise return 0  -z file length is 0 return 1, otherwise return 0.   -F File returns 1 for normal file, otherwise returns 0  -d file as directory file when 1 is returned, otherwise 0  
    • Test string
String 1 = string 2   is true when two strings are equal = string 2 True when     two   strings are unequal-n string      True if the length of the string is greater than 0   -Z string   is a true  string     when the length of the string is 0
    • Test two integer relationships
Number 1-eq number 22 equals True   -NE number 22 number is true   -GT number 2 digit 1 greater than the number 2 true   -GE Digital 2 
     number 1 is greater than or equal to  the number 2 is true -lt Digit 2  number 1  is less than the number 2 is true -le digit 2  digit 1 is less than or equal to the number 2 is true  
    • Logic test
-A and   -o or   ! Non -

Control judgment statements in shell 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.